From 618452682d131f0184e5c3bd26dcbcab1236c14c Mon Sep 17 00:00:00 2001 From: brandon-schabel Date: Wed, 13 Sep 2023 22:10:16 -0700 Subject: [PATCH] updated unit tests --- .../server-factory/request-helpers.test.ts | 32 +++++++++---------- modules/server-factory/request-helpers.ts | 27 +++++++--------- modules/server-factory/route-handler.ts | 2 +- modules/utils/http-types.ts | 2 +- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/modules/server-factory/request-helpers.test.ts b/modules/server-factory/request-helpers.test.ts index eba7cae..84ff74c 100644 --- a/modules/server-factory/request-helpers.test.ts +++ b/modules/server-factory/request-helpers.test.ts @@ -1,15 +1,16 @@ import { describe, expect, it } from "bun:test"; import { - htmlRes, - jsonRes, - parseQueryParams, - parseRequestHeaders, + htmlRes, + jsonRes, + parseQueryParams, + parseRequestHeaders, } from "./request-helpers"; describe("parseQueryParams", () => { it("should parse query params from a request", () => { const request = new Request("https://example.com/path?foo=bar&baz=qux"); const params = parseQueryParams(request); + expect(params).toEqual({ foo: "bar", baz: "qux" }); }); }); @@ -23,6 +24,7 @@ describe("parseRequestHeaders", () => { }, }); const headers = parseRequestHeaders(request); + expect(headers).toEqual({ "content-type": "application/json", authorization: "Bearer abc123", @@ -31,28 +33,24 @@ describe("parseRequestHeaders", () => { }); describe("jsonRes", () => { - it("should return a JSON response with the given body and headers", () => { + it("should return a JSON response with the given body and headers", async () => { const body = { foo: "bar" }; const response = jsonRes(body, { status: 200, headers: { "X-Foo": "bar" }, }); - expect(response.status).toBe(200); - expect(response.headers.get("content-type")).toBe("application/json"); - expect(response.headers.get("x-foo")).toBe("bar"); - expect(response.body).toBe(JSON.stringify(body)); - }); - it("should return a plain text response if the body is not an object", () => { - const body = "hello, world"; - const response = jsonRes(body); - expect(response.headers.get("content-type")).toBe("application/json"); - expect(response.body).toBe(body); + const test = await response.json(); + + expect(await response.status).toBe(200); + expect(await response.headers.get("x-foo")).toBe("bar"); + expect(await response.headers.get("Content-Type")).toBe("application/json"); + expect(JSON.stringify(test)).toBe(JSON.stringify(body)); }); }); describe("htmlRes", () => { - it("should return an HTML response with the given body and headers", () => { + it("should return an HTML response with the given body and headers", async () => { const body = "

Hello, world!

"; const response = htmlRes(body, { status: 200, @@ -61,6 +59,6 @@ describe("htmlRes", () => { expect(response.status).toBe(200); expect(response.headers.get("content-type")).toBe("text/html"); expect(response.headers.get("x-foo")).toBe("bar"); - expect(response.body).toBe(body); + expect(await response.text()).toBe(body); }); }); diff --git a/modules/server-factory/request-helpers.ts b/modules/server-factory/request-helpers.ts index 0251e67..85638ad 100644 --- a/modules/server-factory/request-helpers.ts +++ b/modules/server-factory/request-helpers.ts @@ -1,5 +1,3 @@ -import { ResBodyT } from "../utils/http-types"; - export function parseQueryParams( request: Request ): ParamsType { @@ -10,38 +8,37 @@ export function parseQueryParams( // @ts-ignore params[key] = value as any; }); + + return params; } export function parseRequestHeaders( request: Request ): HeadersType { - return request.headers as unknown as HeadersType; + return request.headers.toJSON() as unknown as HeadersType; } -export type JSONResType = ( +export type JSONResType = ( body: JSONBodyGeneric, options?: ResponseInit ) => Response; export const jsonRes: JSONResType = (body, options): Response => { - if (typeof body === "object") { - return new Response(JSON.stringify(body), { - headers: { - "Content-Type": "application/json", - ...options?.headers, - }, - ...options, - }); - } - return new Response(body, options); + return new Response(JSON.stringify(body), { + ...options, + headers: { + "Content-Type": "application/json", + ...options?.headers, + }, + }); }; export function htmlRes(body: string, options?: ResponseInit): Response { return new Response(body, { + ...options, headers: { "Content-Type": "text/html", ...options?.headers, }, - ...options, }); } diff --git a/modules/server-factory/route-handler.ts b/modules/server-factory/route-handler.ts index f7c2c06..d7e2a9e 100644 --- a/modules/server-factory/route-handler.ts +++ b/modules/server-factory/route-handler.ts @@ -89,7 +89,7 @@ export function createRoute< request, onRequest: handler, errorMessage, - onError + onError, }) ); }; diff --git a/modules/utils/http-types.ts b/modules/utils/http-types.ts index caf9c10..d465129 100644 --- a/modules/utils/http-types.ts +++ b/modules/utils/http-types.ts @@ -91,7 +91,7 @@ export type CreateRouteGeneric< getBody: () => Promise; parseQueryParams: () => ParamsType; parseHeaders: () => HeadersType; - jsonRes: ( + jsonRes: ( body: JSONBodyGeneric, options?: ResponseInit ) => Response;