test: add mock requestUrl method by @Fevol
This commit is contained in:
parent
395bad135b
commit
8accba7da6
1 changed files with 32 additions and 0 deletions
32
__mocks__/obsidian.ts
Normal file
32
__mocks__/obsidian.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { RequestUrlParam, RequestUrlResponse } from "obsidian";
|
||||||
|
|
||||||
|
export function requestUrl(request: RequestUrlParam): Promise<RequestUrlResponse> {
|
||||||
|
return fetch(request.url, {
|
||||||
|
method: request.method,
|
||||||
|
headers: request.headers,
|
||||||
|
body: request.body,
|
||||||
|
}).then(async (response) => {
|
||||||
|
if (response.status >= 400 && request.throw) {
|
||||||
|
throw new Error(`Request failed, ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn response headers into Record<string, string> object
|
||||||
|
const headers: Record<string, string> = {};
|
||||||
|
response.headers.forEach((value, key) => {
|
||||||
|
headers[key] = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
const arraybuffer = await response.arrayBuffer();
|
||||||
|
const text = arraybuffer ? new TextDecoder().decode(arraybuffer) : '';
|
||||||
|
const json = text ? JSON.parse(text) : {};
|
||||||
|
|
||||||
|
let response_body: RequestUrlResponse = {
|
||||||
|
status: response.status,
|
||||||
|
headers: headers,
|
||||||
|
arrayBuffer: arraybuffer,
|
||||||
|
json: json,
|
||||||
|
text: text,
|
||||||
|
};
|
||||||
|
return response_body;
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue