Skip to content

Commit

Permalink
Iterate
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
t3chguy committed Feb 27, 2025
1 parent a614183 commit 9776eaf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
25 changes: 13 additions & 12 deletions spec/unit/http-api/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("FetchHttpApi", () => {
).resolves.toBe(text);
});

it("should send token via query params if useAuthorizationHeader=false", () => {
it("should send token via query params if useAuthorizationHeader=false", async () => {
const fetchFn = jest.fn().mockResolvedValue({ ok: true });
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), {
baseUrl,
Expand All @@ -134,19 +134,19 @@ describe("FetchHttpApi", () => {
accessToken: "token",
useAuthorizationHeader: false,
});
api.authedRequest(Method.Get, "/path");
await api.authedRequest(Method.Get, "/path");
expect(fetchFn.mock.calls[0][0].searchParams.get("access_token")).toBe("token");
});

it("should send token via headers by default", () => {
it("should send token via headers by default", async () => {
const fetchFn = jest.fn().mockResolvedValue({ ok: true });
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), {
baseUrl,
prefix,
fetchFn,
accessToken: "token",
});
api.authedRequest(Method.Get, "/path");
await api.authedRequest(Method.Get, "/path");
expect(fetchFn.mock.calls[0][1].headers["Authorization"]).toBe("Bearer token");
});

Expand All @@ -163,7 +163,7 @@ describe("FetchHttpApi", () => {
expect(fetchFn.mock.calls[0][1].headers["Authorization"]).toBeFalsy();
});

it("should ensure no token is leaked out via query params if sending via headers", () => {
it("should ensure no token is leaked out via query params if sending via headers", async () => {
const fetchFn = jest.fn().mockResolvedValue({ ok: true });
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), {
baseUrl,
Expand All @@ -172,12 +172,12 @@ describe("FetchHttpApi", () => {
accessToken: "token",
useAuthorizationHeader: true,
});
api.authedRequest(Method.Get, "/path", { access_token: "123" });
await api.authedRequest(Method.Get, "/path", { access_token: "123" });
expect(fetchFn.mock.calls[0][0].searchParams.get("access_token")).toBeFalsy();
expect(fetchFn.mock.calls[0][1].headers["Authorization"]).toBe("Bearer token");
});

it("should not override manually specified access token via query params", () => {
it("should not override manually specified access token via query params", async () => {
const fetchFn = jest.fn().mockResolvedValue({ ok: true });
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), {
baseUrl,
Expand All @@ -186,11 +186,11 @@ describe("FetchHttpApi", () => {
accessToken: "token",
useAuthorizationHeader: false,
});
api.authedRequest(Method.Get, "/path", { access_token: "RealToken" });
await api.authedRequest(Method.Get, "/path", { access_token: "RealToken" });
expect(fetchFn.mock.calls[0][0].searchParams.get("access_token")).toBe("RealToken");
});

it("should not override manually specified access token via header", () => {
it("should not override manually specified access token via header", async () => {
const fetchFn = jest.fn().mockResolvedValue({ ok: true });
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), {
baseUrl,
Expand All @@ -199,16 +199,16 @@ describe("FetchHttpApi", () => {
accessToken: "token",
useAuthorizationHeader: true,
});
api.authedRequest(Method.Get, "/path", undefined, undefined, {
await api.authedRequest(Method.Get, "/path", undefined, undefined, {
headers: { Authorization: "Bearer RealToken" },
});
expect(fetchFn.mock.calls[0][1].headers["Authorization"]).toBe("Bearer RealToken");
});

it("should not override Accept header", () => {
it("should not override Accept header", async () => {
const fetchFn = jest.fn().mockResolvedValue({ ok: true });
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), { baseUrl, prefix, fetchFn });
api.authedRequest(Method.Get, "/path", undefined, undefined, {
await api.authedRequest(Method.Get, "/path", undefined, undefined, {
headers: { Accept: "text/html" },
});
expect(fetchFn.mock.calls[0][1].headers["Accept"]).toBe("text/html");
Expand Down Expand Up @@ -501,6 +501,7 @@ describe("FetchHttpApi", () => {
const prom1 = api.authedRequest(Method.Get, "/path1");
const prom2 = api.authedRequest(Method.Get, "/path2");

await jest.advanceTimersByTimeAsync(10); // wait for requests to fire
expect(fetchFn).toHaveBeenCalledTimes(2);
fetchFn.mockResolvedValue({
ok: true,
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/http-api/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ describe("MatrixHttpApi", () => {
xhr.onreadystatechange?.(new Event("test"));
});

it("should fall back to `fetch` where xhr is unavailable", () => {
it("should fall back to `fetch` where xhr is unavailable", async () => {
globalThis.XMLHttpRequest = undefined!;
const fetchFn = jest.fn().mockResolvedValue({ ok: true, json: jest.fn().mockResolvedValue({}) });
const api = new MatrixHttpApi(new TypedEventEmitter<any, any>(), { baseUrl, prefix, fetchFn });
upload = api.uploadContent({} as File);
await upload;
expect(fetchFn).toHaveBeenCalled();
});

Expand Down
3 changes: 2 additions & 1 deletion spec/unit/matrix-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2753,12 +2753,13 @@ describe("MatrixClient", function () {
// WHEN we call `setAccountData` ...
const setProm = client.setAccountData(eventType, content);

await jest.advanceTimersByTimeAsync(10);
// THEN, the REST call should have happened, and had the correct content
const lastCall = fetchMock.lastCall("put-account-data");
expect(lastCall).toBeDefined();
expect(lastCall?.[1]?.body).toEqual(JSON.stringify(content));

// Even after waiting a bit, the method should not yet have returned
// Even after waiting a bit more, the method should not yet have returned
await jest.advanceTimersByTimeAsync(10);
let finished = false;
setProm.finally(() => (finished = true));
Expand Down
2 changes: 0 additions & 2 deletions src/http-api/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ export class FetchHttpApi<O extends IHttpOpts> {
doNotAttemptTokenRefresh: true,
});
}

throw err;
}

// otherwise continue with error handling
Expand Down

0 comments on commit 9776eaf

Please sign in to comment.