Spaces:
Running
Running
File size: 1,416 Bytes
914a36d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /**
* A `Response` for the search services' tests: the body is a string, and the
* fields the services read are the only ones filled in.
*/
export function mockResponse(
body: string,
{
status,
ok = status === undefined ? true : status >= 200 && status < 300,
headers = {},
contentType,
unreadBody = null,
}: {
ok?: boolean;
status?: number;
headers?: Record<string, string>;
/** Omitted leaves the `content-type` header off, as a headerless reply. */
contentType?: string;
/** The `body` the service sees, so a test can watch its reader cancel. */
unreadBody?: { cancel: () => Promise<void> } | null;
} = {},
): Response {
const resolvedStatus = status ?? (ok ? 200 : 503);
return {
ok,
status: resolvedStatus,
statusText: ok ? "OK" : "Error",
headers: new Headers(
contentType === undefined
? headers
: { "content-type": contentType, ...headers },
),
redirected: false,
type: "basic" as ResponseType,
url: "http://test.com",
clone: function () {
return this;
},
body: unreadBody,
bodyUsed: false,
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
blob: () => Promise.resolve(new Blob()),
formData: () => Promise.resolve(new FormData()),
json: () => Promise.resolve(JSON.parse(body)),
text: () => Promise.resolve(body),
} as unknown as Response;
}
|