mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 17:43:25 +00:00
add basic categories to tests
This commit is contained in:
39
src/__test__/standard/fetchers/body.test.ts
Normal file
39
src/__test__/standard/fetchers/body.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { serializeBody } from "@/fetchers/body";
|
||||
import FormData from "form-data";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("serializeBody()", () => {
|
||||
it('should work with standard text', () => {
|
||||
expect(serializeBody("hello world")).toEqual({
|
||||
headers: {},
|
||||
body: "hello world"
|
||||
})
|
||||
})
|
||||
|
||||
it('should work with objects', () => {
|
||||
expect(serializeBody({ hello: "world", a: 42 })).toEqual({
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ hello: "world", a: 42 })
|
||||
})
|
||||
})
|
||||
|
||||
it('should work x-www-form-urlencoded', () => {
|
||||
const obj = new URLSearchParams()
|
||||
obj.set("a", "b");
|
||||
expect(serializeBody(obj)).toEqual({
|
||||
headers: {},
|
||||
body: obj
|
||||
})
|
||||
})
|
||||
|
||||
it('should work multipart/form-data', () => {
|
||||
const obj = new FormData()
|
||||
obj.append("a", "b");
|
||||
expect(serializeBody(obj)).toEqual({
|
||||
headers: {},
|
||||
body: obj
|
||||
})
|
||||
})
|
||||
})
|
48
src/__test__/standard/fetchers/common.test.ts
Normal file
48
src/__test__/standard/fetchers/common.test.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { makeFullUrl } from "@/fetchers/common";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("makeFullUrl()", () => {
|
||||
it('should pass normal url if no options', () => {
|
||||
expect(makeFullUrl('https://example.com/hello/world')).toEqual("https://example.com/hello/world")
|
||||
expect(makeFullUrl('https://example.com/hello/world?a=b')).toEqual("https://example.com/hello/world?a=b")
|
||||
expect(makeFullUrl('https://example.com/hello/world?a=b#hello')).toEqual("https://example.com/hello/world?a=b#hello")
|
||||
expect(makeFullUrl('https://example.com/hello/world#hello')).toEqual("https://example.com/hello/world#hello")
|
||||
})
|
||||
|
||||
it('should append baseurl correctly', () => {
|
||||
const correctResult = "https://example.com/hello/world";
|
||||
expect(makeFullUrl(correctResult, { baseUrl: '' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('/hello/world', { baseUrl: 'https://example.com' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('/hello/world', { baseUrl: 'https://example.com/' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('hello/world', { baseUrl: 'https://example.com/' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('hello/world', { baseUrl: 'https://example.com' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('/world', { baseUrl: 'https://example.com/hello' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('/world', { baseUrl: 'https://example.com/hello/' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('world', { baseUrl: 'https://example.com/hello/' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('world', { baseUrl: 'https://example.com/hello' })).toEqual(correctResult)
|
||||
expect(makeFullUrl('world?a=b', { baseUrl: 'https://example.com/hello' })).toEqual("https://example.com/hello/world?a=b")
|
||||
})
|
||||
|
||||
it('should throw with invalid baseurl combinations', () => {
|
||||
expect(() => makeFullUrl('example.com/hello/world', { baseUrl: '' })).toThrowError()
|
||||
expect(() => makeFullUrl('/hello/world', { baseUrl: 'example.com' })).toThrowError()
|
||||
expect(() => makeFullUrl('/hello/world', { baseUrl: 'tcp://example.com' })).toThrowError()
|
||||
expect(() => makeFullUrl('/hello/world', { baseUrl: 'tcp://example.com' })).toThrowError()
|
||||
})
|
||||
|
||||
it('should add/merge query parameters', () => {
|
||||
expect(makeFullUrl('https://example.com/hello/world', { query: { a: 'b' } })).toEqual("https://example.com/hello/world?a=b")
|
||||
expect(makeFullUrl('https://example.com/hello/world/', { query: { a: 'b' } })).toEqual("https://example.com/hello/world/?a=b")
|
||||
expect(makeFullUrl('https://example.com', { query: { a: 'b' } })).toEqual("https://example.com/?a=b")
|
||||
expect(makeFullUrl('https://example.com/', { query: { a: 'b' } })).toEqual("https://example.com/?a=b")
|
||||
|
||||
expect(makeFullUrl('https://example.com/hello/world?c=d', { query: { a: 'b' } })).toEqual("https://example.com/hello/world?c=d&a=b")
|
||||
expect(makeFullUrl('https://example.com/hello/world?c=d', { query: {} })).toEqual("https://example.com/hello/world?c=d")
|
||||
expect(makeFullUrl('https://example.com/hello/world?c=d')).toEqual("https://example.com/hello/world?c=d")
|
||||
expect(makeFullUrl('https://example.com/hello/world?c=d', {})).toEqual("https://example.com/hello/world?c=d")
|
||||
})
|
||||
|
||||
it('should work with a mix of multiple options', () => {
|
||||
expect(makeFullUrl('/hello/world?c=d', { baseUrl: 'https://example.com/', query: { a: 'b' } })).toEqual("https://example.com/hello/world?c=d&a=b")
|
||||
})
|
||||
})
|
138
src/__test__/standard/fetchers/simpleProxy.test.ts
Normal file
138
src/__test__/standard/fetchers/simpleProxy.test.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { makeSimpleProxyFetcher } from "@/fetchers/simpleProxy";
|
||||
import { DefaultedFetcherOptions, FetcherOptions } from "@/fetchers/types";
|
||||
import { Headers } from "node-fetch";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
describe("makeSimpleProxyFetcher()", () => {
|
||||
const fetch = vi.fn();
|
||||
const fetcher = makeSimpleProxyFetcher("https://example.com/proxy", fetch);
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
function setResult(type: "text" | "json", value: any) {
|
||||
if (type === 'text') return fetch.mockResolvedValueOnce({
|
||||
headers: new Headers({
|
||||
"content-type": "text/plain",
|
||||
}),
|
||||
status: 204,
|
||||
url: "test123",
|
||||
text() {
|
||||
return Promise.resolve(value);
|
||||
},
|
||||
});
|
||||
if (type === 'json') return fetch.mockResolvedValueOnce({
|
||||
headers: new Headers({
|
||||
"content-type": "application/json",
|
||||
}),
|
||||
status: 204,
|
||||
url: "test123",
|
||||
json() {
|
||||
return Promise.resolve(value);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) {
|
||||
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();
|
||||
}
|
||||
|
||||
it('should pass options through', () => {
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com",
|
||||
input: {
|
||||
method: "GET",
|
||||
query: {},
|
||||
readHeaders: [],
|
||||
headers: {
|
||||
"X-Hello": "world",
|
||||
},
|
||||
},
|
||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||
output: {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Hello": "world",
|
||||
},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com",
|
||||
input: {
|
||||
method: "GET",
|
||||
headers: {},
|
||||
readHeaders: [],
|
||||
query: {
|
||||
"a": 'b',
|
||||
}
|
||||
},
|
||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/?a=b')}`,
|
||||
output: {
|
||||
method: "GET",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com",
|
||||
input: {
|
||||
method: "GET",
|
||||
query: {},
|
||||
readHeaders: [],
|
||||
headers: {},
|
||||
},
|
||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||
output: {
|
||||
method: "GET",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
});
|
||||
|
||||
it('should parse response correctly', () => {
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com/",
|
||||
input: {
|
||||
method: "POST",
|
||||
query: {},
|
||||
readHeaders: [],
|
||||
headers: {},
|
||||
},
|
||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||
output: {
|
||||
method: "POST",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
setResult("json", { hello: 42 });
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com/",
|
||||
input: {
|
||||
method: "POST",
|
||||
query: {},
|
||||
readHeaders: [],
|
||||
headers: {},
|
||||
},
|
||||
outputUrl: `https://example.com/proxy?destination=${encodeURIComponent('https://google.com/')}`,
|
||||
output: {
|
||||
method: "POST",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: { hello: 42 }
|
||||
})
|
||||
});
|
||||
});
|
139
src/__test__/standard/fetchers/standard.test.ts
Normal file
139
src/__test__/standard/fetchers/standard.test.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { makeStandardFetcher } from "@/fetchers/standardFetch";
|
||||
import { DefaultedFetcherOptions } from "@/fetchers/types";
|
||||
import { Headers } from "node-fetch";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
describe("makeStandardFetcher()", () => {
|
||||
const fetch = vi.fn();
|
||||
const fetcher = makeStandardFetcher(fetch);
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
function setResult(type: "text" | "json", value: any) {
|
||||
if (type === 'text') return fetch.mockResolvedValueOnce({
|
||||
headers: new Headers({
|
||||
"content-type": "text/plain",
|
||||
}),
|
||||
status: 204,
|
||||
url: "test123",
|
||||
text() {
|
||||
return Promise.resolve(value);
|
||||
},
|
||||
});
|
||||
if (type === 'json') return fetch.mockResolvedValueOnce({
|
||||
headers: new Headers({
|
||||
"content-type": "application/json",
|
||||
}),
|
||||
status: 204,
|
||||
url: "test123",
|
||||
json() {
|
||||
return Promise.resolve(value);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function expectFetchCall(ops: { inputUrl: string, input: DefaultedFetcherOptions, outputUrl?: string, output: any, outputBody: any }) {
|
||||
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();
|
||||
}
|
||||
|
||||
it('should pass options through', () => {
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com",
|
||||
input: {
|
||||
method: "GET",
|
||||
query: {},
|
||||
readHeaders: [],
|
||||
headers: {
|
||||
"X-Hello": "world",
|
||||
},
|
||||
},
|
||||
outputUrl: "https://google.com/",
|
||||
output: {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Hello": "world",
|
||||
},
|
||||
body: undefined,
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com",
|
||||
input: {
|
||||
method: "GET",
|
||||
headers: {},
|
||||
readHeaders: [],
|
||||
query: {
|
||||
"a": 'b',
|
||||
}
|
||||
},
|
||||
outputUrl: "https://google.com/?a=b",
|
||||
output: {
|
||||
method: "GET",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com",
|
||||
input: {
|
||||
query: {},
|
||||
headers: {},
|
||||
readHeaders: [],
|
||||
method: "GET"
|
||||
},
|
||||
outputUrl: "https://google.com/",
|
||||
output: {
|
||||
method: "GET",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
});
|
||||
|
||||
it('should parse response correctly', () => {
|
||||
setResult("text", "hello world");
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com/",
|
||||
input: {
|
||||
query: {},
|
||||
headers: {},
|
||||
readHeaders: [],
|
||||
method: "POST"
|
||||
},
|
||||
outputUrl: "https://google.com/",
|
||||
output: {
|
||||
method: "POST",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: "hello world"
|
||||
})
|
||||
setResult("json", { hello: 42 });
|
||||
expectFetchCall({
|
||||
inputUrl: "https://google.com/",
|
||||
input: {
|
||||
query: {},
|
||||
headers: {},
|
||||
readHeaders: [],
|
||||
method: "POST"
|
||||
},
|
||||
outputUrl: "https://google.com/",
|
||||
output: {
|
||||
method: "POST",
|
||||
headers: {},
|
||||
},
|
||||
outputBody: { hello: 42 }
|
||||
})
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user