Skip to content

Commit

Permalink
updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-schabel committed Sep 14, 2023
1 parent 32a2035 commit 6184526
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
32 changes: 15 additions & 17 deletions modules/server-factory/request-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -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" });
});
});
Expand All @@ -23,6 +24,7 @@ describe("parseRequestHeaders", () => {
},
});
const headers = parseRequestHeaders(request);

expect(headers).toEqual({
"content-type": "application/json",
authorization: "Bearer abc123",
Expand All @@ -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 = "<h1>Hello, world!</h1>";
const response = htmlRes(body, {
status: 200,
Expand All @@ -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);
});
});
27 changes: 12 additions & 15 deletions modules/server-factory/request-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ResBodyT } from "../utils/http-types";

export function parseQueryParams<ParamsType extends object = {}>(
request: Request
): ParamsType {
Expand All @@ -10,38 +8,37 @@ export function parseQueryParams<ParamsType extends object = {}>(
// @ts-ignore
params[key] = value as any;
});

return params;
}

export function parseRequestHeaders<HeadersType>(
request: Request
): HeadersType {
return request.headers as unknown as HeadersType;
return request.headers.toJSON() as unknown as HeadersType;
}

export type JSONResType = <JSONBodyGeneric extends ResBodyT>(
export type JSONResType = <JSONBodyGeneric extends object>(
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,
});
}
2 changes: 1 addition & 1 deletion modules/server-factory/route-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function createRoute<
request,
onRequest: handler,
errorMessage,
onError
onError,
})
);
};
Expand Down
2 changes: 1 addition & 1 deletion modules/utils/http-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type CreateRouteGeneric<
getBody: <BodyType extends ReqT["body"]>() => Promise<BodyType>;
parseQueryParams: <ParamsType>() => ParamsType;
parseHeaders: <HeadersType>() => HeadersType;
jsonRes: <JSONBodyGeneric extends ResT>(
jsonRes: <JSONBodyGeneric extends object>(
body: JSONBodyGeneric,
options?: ResponseInit
) => Response;
Expand Down

0 comments on commit 6184526

Please sign in to comment.