Testing Handler

ResponseRecorderis dummy request that records response from HttpHandler.

It behaves as an actual HTTP request for handlers and is useful for unit testing.

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { createRecorder, ServeHandler } from "https://servestjs.org/@v1.1.0/mod.ts";

const handleRequest: ServeHandler = async (req) => {
  const body = await req.text();
  return req.respond({ status: 200, body: "Hello! " + body });
};

Deno.test("handler should respond with 200", async () => {
  // Create dummy request for handlers
  const recorder = createRecorder({ url: "/", method: "POST", body: "Deno" });
  await handleRequest(recorder);
  // Obtain recorded response
  const resp = await recorder.response();
  assertEquals(resp.status, 200);
  assertEquals(await resp.text(), "Hello! Deno");
});