Skip to content

Commit

Permalink
Enforce using fetch in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ggalmazor committed Oct 9, 2024
1 parent adc8eb0 commit 9e8568b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion test/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from "fs";
import { DNSimple } from "../lib/main";
import { DNSimple, TimeoutError } from "../lib/main";

function hasJsonContent(lines: string[]) {
for (let line of lines) {
Expand Down Expand Up @@ -29,6 +29,29 @@ function parseBody(lines: string[]) {
export function createTestClient() {
return new DNSimple({
accessToken: process.env["TOKEN"] ?? "bogus",
fetcher: async (params: {
method: string;
url: string;
headers: { [name: string]: string };
timeout: number;
body?: string;
}) => {
const abortController = new AbortController();
setTimeout(() => abortController.abort(), DNSimple.DEFAULT_TIMEOUT);
try {
const response = await fetch(params.url, {
method: params.method,
headers: params.headers,
body: params.body,
signal: abortController.signal,
});
return { status: response.status, body: await response.text() };
} catch (error) {
if (abortController && abortController.signal.aborted) throw new TimeoutError();

throw error;
}
},
});
}

Expand Down

0 comments on commit 9e8568b

Please sign in to comment.