Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114// Copyright 2018-2022 the oak authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any
import { assertEquals, unreachable } from "./test_deps.ts";
import { HttpServer, type IncomingMessage, NodeRequest, type ServerResponse,} from "./http_server_node.ts";
import { Application } from "./application.ts";// import { isNode } from "./util.ts";
const destroyCalls: any[][] = [];const setHeaderCalls: any[][] = [];const writeCalls: any[][] = [];const writeHeadCalls: any[][] = [];
function createMockReqRes( url = "/", headers: Record<string, string> = {}, method = "GET", address = "127.0.0.1",): [req: IncomingMessage, res: ServerResponse] { destroyCalls.length = 0; setHeaderCalls.length = 0; writeCalls.length = 0; writeHeadCalls.length = 0; const req = { headers, method, socket: { address() { return { addr: { address, }, }; }, }, url, on(_method: string, _listener: (arg?: any) => void) {}, }; const res = { destroy(...args: any[]) { destroyCalls.push(args); }, end(callback?: () => void) { if (callback) { callback(); } }, setHeader(...args: any[]) { setHeaderCalls.push(args); }, write(chunk: unknown, callback?: (err: Error | null) => void) { writeCalls.push([chunk, callback]); if (callback) { callback(null); } }, writeHead(...args: any[]) { writeHeadCalls.push(args); }, }; return [req, res];}
Deno.test({ name: "NodeRequest", async fn() { const nodeRequest = new NodeRequest( ...createMockReqRes("/", {}, "POST", "127.0.0.1"), ); assertEquals(nodeRequest.url, `/`); const response = new Response("hello deno"); await nodeRequest.respond(response); assertEquals(writeHeadCalls, [[200, ""]]); },});
Deno.test({ name: "HttpServer closes gracefully after serving requests", // TODO(@kitsonk) this is failing locally for me, figure out what is wrong. ignore: true, async fn() { const app = new Application(); const listenOptions = { port: 4508 };
const server = new HttpServer(app, listenOptions); server.listen();
const expectedBody = "test-body";
(async () => { for await (const nodeRequest of server) { nodeRequest.respond(new Response(expectedBody)); } })();
try { const response = await fetch(`http://localhost:${listenOptions.port}`); assertEquals(await response.text(), expectedBody); } catch { unreachable(); } finally { server.close(); } },});