Created
March 27, 2021 05:43
-
-
Save snsinfu/0f0e00fbe6c5e88b93053972ec028d32 to your computer and use it in GitHub Desktop.
Deno HTTP mock server for testing HTTP requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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