mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 14:53:24 +00:00
Fix fetcher tests
This commit is contained in:
@@ -16,6 +16,8 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
"content-type": "text/plain",
|
"content-type": "text/plain",
|
||||||
}),
|
}),
|
||||||
|
status: 204,
|
||||||
|
url: "test123",
|
||||||
text() {
|
text() {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
},
|
},
|
||||||
@@ -24,6 +26,8 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
}),
|
}),
|
||||||
|
status: 204,
|
||||||
|
url: "test123",
|
||||||
json() {
|
json() {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
},
|
},
|
||||||
@@ -31,7 +35,11 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) {
|
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);
|
expect(fetch).toBeCalledWith(ops.outputUrl ?? ops.inputUrl, ops.output);
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
}
|
}
|
||||||
@@ -43,6 +51,7 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
query: {},
|
query: {},
|
||||||
|
readHeaders: [],
|
||||||
headers: {
|
headers: {
|
||||||
"X-Hello": "world",
|
"X-Hello": "world",
|
||||||
},
|
},
|
||||||
@@ -62,6 +71,7 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {},
|
headers: {},
|
||||||
|
readHeaders: [],
|
||||||
query: {
|
query: {
|
||||||
"a": 'b',
|
"a": 'b',
|
||||||
}
|
}
|
||||||
@@ -79,6 +89,7 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
query: {},
|
query: {},
|
||||||
|
readHeaders: [],
|
||||||
headers: {},
|
headers: {},
|
||||||
},
|
},
|
||||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||||
@@ -97,6 +108,7 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
query: {},
|
query: {},
|
||||||
|
readHeaders: [],
|
||||||
headers: {},
|
headers: {},
|
||||||
},
|
},
|
||||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||||
@@ -112,6 +124,7 @@ describe("makeSimpleProxyFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
query: {},
|
query: {},
|
||||||
|
readHeaders: [],
|
||||||
headers: {},
|
headers: {},
|
||||||
},
|
},
|
||||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||||
|
@@ -16,6 +16,8 @@ describe("makeStandardFetcher()", () => {
|
|||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
"content-type": "text/plain",
|
"content-type": "text/plain",
|
||||||
}),
|
}),
|
||||||
|
status: 204,
|
||||||
|
url: "test123",
|
||||||
text() {
|
text() {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
},
|
},
|
||||||
@@ -24,6 +26,8 @@ describe("makeStandardFetcher()", () => {
|
|||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
}),
|
}),
|
||||||
|
status: 204,
|
||||||
|
url: "test123",
|
||||||
json() {
|
json() {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
},
|
},
|
||||||
@@ -31,7 +35,11 @@ describe("makeStandardFetcher()", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) {
|
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);
|
expect(fetch).toBeCalledWith(ops.outputUrl ?? ops.inputUrl, ops.output);
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
}
|
}
|
||||||
@@ -43,6 +51,7 @@ describe("makeStandardFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
query: {},
|
query: {},
|
||||||
|
readHeaders: [],
|
||||||
headers: {
|
headers: {
|
||||||
"X-Hello": "world",
|
"X-Hello": "world",
|
||||||
},
|
},
|
||||||
@@ -53,6 +62,7 @@ describe("makeStandardFetcher()", () => {
|
|||||||
headers: {
|
headers: {
|
||||||
"X-Hello": "world",
|
"X-Hello": "world",
|
||||||
},
|
},
|
||||||
|
body: undefined,
|
||||||
},
|
},
|
||||||
outputBody: "hello world"
|
outputBody: "hello world"
|
||||||
})
|
})
|
||||||
@@ -62,6 +72,7 @@ describe("makeStandardFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {},
|
headers: {},
|
||||||
|
readHeaders: [],
|
||||||
query: {
|
query: {
|
||||||
"a": 'b',
|
"a": 'b',
|
||||||
}
|
}
|
||||||
@@ -79,6 +90,7 @@ describe("makeStandardFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
query: {},
|
query: {},
|
||||||
headers: {},
|
headers: {},
|
||||||
|
readHeaders: [],
|
||||||
method: "GET"
|
method: "GET"
|
||||||
},
|
},
|
||||||
outputUrl: "https://google.com/",
|
outputUrl: "https://google.com/",
|
||||||
@@ -97,6 +109,7 @@ describe("makeStandardFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
query: {},
|
query: {},
|
||||||
headers: {},
|
headers: {},
|
||||||
|
readHeaders: [],
|
||||||
method: "POST"
|
method: "POST"
|
||||||
},
|
},
|
||||||
outputUrl: "https://google.com/",
|
outputUrl: "https://google.com/",
|
||||||
@@ -112,6 +125,7 @@ describe("makeStandardFetcher()", () => {
|
|||||||
input: {
|
input: {
|
||||||
query: {},
|
query: {},
|
||||||
headers: {},
|
headers: {},
|
||||||
|
readHeaders: [],
|
||||||
method: "POST"
|
method: "POST"
|
||||||
},
|
},
|
||||||
outputUrl: "https://google.com/",
|
outputUrl: "https://google.com/",
|
||||||
|
@@ -32,7 +32,7 @@ export function makeStandardFetcher(f: FetchLike): Fetcher {
|
|||||||
let body: any;
|
let body: any;
|
||||||
const isJson = res.headers.get('content-type')?.includes('application/json');
|
const isJson = res.headers.get('content-type')?.includes('application/json');
|
||||||
if (isJson) body = await res.json();
|
if (isJson) body = await res.json();
|
||||||
else body = res.text();
|
else body = await res.text();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
body,
|
body,
|
||||||
|
Reference in New Issue
Block a user