From 4eaae64e4afc290a22d41375821f505bcb55be18 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Tue, 26 Dec 2023 16:02:25 +0100 Subject: [PATCH] Fix fetcher tests --- src/__test__/fetchers/simpleProxy.test.ts | 15 ++++++++++++++- src/__test__/fetchers/standard.test.ts | 16 +++++++++++++++- src/fetchers/standardFetch.ts | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/__test__/fetchers/simpleProxy.test.ts b/src/__test__/fetchers/simpleProxy.test.ts index 5066e63..fc1137a 100644 --- a/src/__test__/fetchers/simpleProxy.test.ts +++ b/src/__test__/fetchers/simpleProxy.test.ts @@ -16,6 +16,8 @@ describe("makeSimpleProxyFetcher()", () => { headers: new Headers({ "content-type": "text/plain", }), + status: 204, + url: "test123", text() { return Promise.resolve(value); }, @@ -24,6 +26,8 @@ describe("makeSimpleProxyFetcher()", () => { headers: new Headers({ "content-type": "application/json", }), + status: 204, + url: "test123", json() { return Promise.resolve(value); }, @@ -31,7 +35,11 @@ describe("makeSimpleProxyFetcher()", () => { } function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) { - expect(fetcher(ops.inputUrl, ops.input)).resolves.toEqual(ops.outputBody); + const prom = fetcher(ops.inputUrl, ops.input); + expect((async () => (await prom).body)()).resolves.toEqual(ops.outputBody); + expect((async () => (await prom).headers.entries())()).resolves.toEqual((new Headers()).entries()); + expect((async () => (await prom).statusCode)()).resolves.toEqual(204); + expect((async () => (await prom).finalUrl)()).resolves.toEqual("test123"); expect(fetch).toBeCalledWith(ops.outputUrl ?? ops.inputUrl, ops.output); vi.clearAllMocks(); } @@ -43,6 +51,7 @@ describe("makeSimpleProxyFetcher()", () => { input: { method: "GET", query: {}, + readHeaders: [], headers: { "X-Hello": "world", }, @@ -62,6 +71,7 @@ describe("makeSimpleProxyFetcher()", () => { input: { method: "GET", headers: {}, + readHeaders: [], query: { "a": 'b', } @@ -79,6 +89,7 @@ describe("makeSimpleProxyFetcher()", () => { input: { method: "GET", query: {}, + readHeaders: [], headers: {}, }, outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`, @@ -97,6 +108,7 @@ describe("makeSimpleProxyFetcher()", () => { input: { method: "POST", query: {}, + readHeaders: [], headers: {}, }, outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`, @@ -112,6 +124,7 @@ describe("makeSimpleProxyFetcher()", () => { input: { method: "POST", query: {}, + readHeaders: [], headers: {}, }, outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`, diff --git a/src/__test__/fetchers/standard.test.ts b/src/__test__/fetchers/standard.test.ts index 6bad99a..6753500 100644 --- a/src/__test__/fetchers/standard.test.ts +++ b/src/__test__/fetchers/standard.test.ts @@ -16,6 +16,8 @@ describe("makeStandardFetcher()", () => { headers: new Headers({ "content-type": "text/plain", }), + status: 204, + url: "test123", text() { return Promise.resolve(value); }, @@ -24,6 +26,8 @@ describe("makeStandardFetcher()", () => { headers: new Headers({ "content-type": "application/json", }), + status: 204, + url: "test123", json() { return Promise.resolve(value); }, @@ -31,7 +35,11 @@ describe("makeStandardFetcher()", () => { } function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) { - expect(fetcher(ops.inputUrl, ops.input)).resolves.toEqual(ops.outputBody); + const prom = fetcher(ops.inputUrl, ops.input); + expect((async () => (await prom).body)()).resolves.toEqual(ops.outputBody); + expect((async () => (await prom).headers.entries())()).resolves.toEqual((new Headers()).entries()); + expect((async () => (await prom).statusCode)()).resolves.toEqual(204); + expect((async () => (await prom).finalUrl)()).resolves.toEqual("test123"); expect(fetch).toBeCalledWith(ops.outputUrl ?? ops.inputUrl, ops.output); vi.clearAllMocks(); } @@ -43,6 +51,7 @@ describe("makeStandardFetcher()", () => { input: { method: "GET", query: {}, + readHeaders: [], headers: { "X-Hello": "world", }, @@ -53,6 +62,7 @@ describe("makeStandardFetcher()", () => { headers: { "X-Hello": "world", }, + body: undefined, }, outputBody: "hello world" }) @@ -62,6 +72,7 @@ describe("makeStandardFetcher()", () => { input: { method: "GET", headers: {}, + readHeaders: [], query: { "a": 'b', } @@ -79,6 +90,7 @@ describe("makeStandardFetcher()", () => { input: { query: {}, headers: {}, + readHeaders: [], method: "GET" }, outputUrl: "https://google.com/", @@ -97,6 +109,7 @@ describe("makeStandardFetcher()", () => { input: { query: {}, headers: {}, + readHeaders: [], method: "POST" }, outputUrl: "https://google.com/", @@ -112,6 +125,7 @@ describe("makeStandardFetcher()", () => { input: { query: {}, headers: {}, + readHeaders: [], method: "POST" }, outputUrl: "https://google.com/", diff --git a/src/fetchers/standardFetch.ts b/src/fetchers/standardFetch.ts index a17dd92..9fb6afa 100644 --- a/src/fetchers/standardFetch.ts +++ b/src/fetchers/standardFetch.ts @@ -32,7 +32,7 @@ export function makeStandardFetcher(f: FetchLike): Fetcher { let body: any; const isJson = res.headers.get('content-type')?.includes('application/json'); if (isJson) body = await res.json(); - else body = res.text(); + else body = await res.text(); return { body,