Skip to content

Commit

Permalink
test: add mock requestUrl method by @Fevol
Browse files Browse the repository at this point in the history
  • Loading branch information
AB1908 committed Sep 17, 2022
1 parent 395bad1 commit 8accba7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions __mocks__/obsidian.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}

0 comments on commit 8accba7

Please sign in to comment.