Add all arm request related files to Matser (#373)

* “minor changes”
* 'changes for unit test'
This commit is contained in:
Chris-MS-896
2021-01-08 21:56:29 -06:00
committed by GitHub
parent 983c9201bb
commit a4a367a212
3 changed files with 55 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
import { armRequest } from "./request";
import fetch from "node-fetch";
import { updateUserContext } from "../../UserContext";
import { AuthType } from "../../AuthType";
interface Global {
Headers: unknown;
@@ -8,6 +10,11 @@ interface Global {
((global as unknown) as Global).Headers = ((fetch as unknown) as Global).Headers;
describe("ARM request", () => {
window.authType = AuthType.AAD;
updateUserContext({
authorizationToken: "some-token"
});
it("should call window.fetch", async () => {
window.fetch = jest.fn().mockResolvedValue({
ok: true,
@@ -48,4 +55,24 @@ describe("ARM request", () => {
).rejects.toThrow();
expect(window.fetch).toHaveBeenCalledTimes(2);
});
it("should throw token error", async () => {
window.authType = AuthType.AAD;
updateUserContext({
authorizationToken: undefined
});
const headers = new Headers();
headers.set("location", "https://foo.com/operationStatus");
window.fetch = jest.fn().mockResolvedValue({
ok: true,
headers,
status: 200,
json: async () => {
return { status: "Failed" };
}
});
await expect(() =>
armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" })
).rejects.toThrow("No authority token provided");
});
});

View File

@@ -30,7 +30,7 @@ export class ARMError extends Error {
Object.setPrototypeOf(this, ARMError.prototype);
}
public code: string | number;
public code?: string | number;
}
interface ARMQueryParams {
@@ -63,6 +63,10 @@ export async function armRequest<T>({
queryParams.metricNames && url.searchParams.append("metricnames", queryParams.metricNames);
}
if (!userContext.authorizationToken) {
throw new Error("No authority token provided");
}
const response = await window.fetch(url.href, {
method,
headers: {
@@ -98,6 +102,10 @@ export async function armRequest<T>({
}
async function getOperationStatus(operationStatusUrl: string) {
if (!userContext.authorizationToken) {
throw new Error("No authority token provided");
}
const response = await window.fetch(operationStatusUrl, {
headers: {
Authorization: userContext.authorizationToken