Fix fetcher tests

This commit is contained in:
mrjvs
2023-12-26 16:02:25 +01:00
parent ffe7ae0985
commit 4eaae64e4a
3 changed files with 30 additions and 3 deletions

View File

@@ -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/')}`,

View File

@@ -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/",

View File

@@ -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,