Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async initialize() {
const accessToken = await authService.login();
if (!accessToken) {
throw new Error("Failed to acquire access token");
}
const tokenCredentials = new TokenCredentials(accessToken);
const clientConfig = {
baseUri: `${window.location.origin}/api/proxy`
};
// Add token and server url to service instance
this.client = new AzureDigitalTwinsAPI(tokenCredentials, clientConfig);
const { appAdtUrl } = await configService.getConfig();
this.clientOptions = { customHeaders: { "x-adt-host": new URL(appAdtUrl).hostname } };
}
it("with no AzureServiceClient.longRunningOperationRetryTimeout value and spam retry-after header", function () {
const azureServiceClient = new AzureServiceClient(new TokenCredentials("my-fake-token"));
const previousResponse: HttpOperationResponse = {
request: new WebResource(),
status: 200,
headers: new HttpHeaders({ "retry-after": "spam" })
};
assert.strictEqual(getDelayInSeconds(azureServiceClient, previousResponse), 30);
});
it ("adds user agent header that looks similar to \"ms-rest-azure-js/0.1.0 ms-rest-js/0.1.0 Node/v10.11.0 OS/(x64-Windows_NT-10.0.18267)\"", async () => {
const client = new AzureServiceClient(new TokenCredentials("my-fake-token"));
const request = new WebResource("https://microsoft.com");
await client.sendRequest(request);
const telemetry = request.headers.get("user-agent")!;
const parts = telemetry.split(" ");
assert(parts[0].includes("ms-rest-azure-js/"));
assert(parts[1].includes("ms-rest-js/"));
assert(parts[2].includes("Node/"));
assert(parts[3].includes("OS/"));
});
});
it ("adds custom user agent if specified", async () => {
const client = new AzureServiceClient(new TokenCredentials("fake-token"), { userAgent: "custom-ua" });
const request = new WebResource("https://microsoft.com");
await client.sendRequest(request);
const telemetry = request.headers.get("user-agent")!;
assert.equal(telemetry, "custom-ua");
});
function createServiceClient(responses: HttpResponse[]): AzureServiceClient {
return new AzureServiceClient(new TokenCredentials("my-fake-token"), {
httpClient: {
sendRequest(httpRequest: WebResource): Promise {
const response: HttpResponse | undefined = responses.shift();
if (!response) {
throw new Error("Not enough responses provided for test.");
}
return Promise.resolve({
request: httpRequest,
status: response.status,
headers: response.headers || new HttpHeaders(),
bodyAsText: typeof response.body === "string" ? response.body : JSON.stringify(response.body),
parsedBody: response.body
});
}
},
longRunningOperationRetryTimeout: 0