Skip to content

Instantly share code, notes, and snippets.

@snsinfu
Created March 27, 2021 05:43
Show Gist options
  • Select an option

  • Save snsinfu/0f0e00fbe6c5e88b93053972ec028d32 to your computer and use it in GitHub Desktop.

Select an option

Save snsinfu/0f0e00fbe6c5e88b93053972ec028d32 to your computer and use it in GitHub Desktop.
Deno HTTP mock server for testing HTTP requests
import * as http from "https://deno.land/std@0.91.0/http/mod.ts";
interface RequestData {
method: string;
path: string;
headers: Headers;
body: ArrayBuffer;
}
interface MockData {
server: http.Server;
requests: RequestData[];
}
function mock(hostname: string, port: number): MockData {
const server = http.serve({ hostname, port });
const requests: RequestData[] = [];
(async () => {
for await (const request of server) {
requests.push({
method: request.method,
path: request.url,
headers: request.headers,
body: await Deno.readAll(request.body),
});
request.respond({ status: 200 });
}
})();
return { server, requests };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment