Skip to content

Commit

Permalink
tests for new error
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Jul 1, 2023
1 parent 0877f14 commit 659ed37
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/http_error.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
import { HTTPError, isHTTPError } from "./http_error.ts";
import { isHTTPError, YumiError } from "./http_error.ts";
import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("HTTPError - create with json value", async () => {
Deno.test("YumiError", async () => {
const req = new Request("http://example.com");
const res = new Response(JSON.stringify({ foo: "bar" }), { status: 500 });
const error = await HTTPError.create(res);
const error = await YumiError.create(req, res);

assertEquals(error.json, { foo: "bar" });
assertEquals(error.text, JSON.stringify({ foo: "bar" }));
assertEquals(error.body, { foo: "bar" });
assert(error.status === 500);
assert(error.url === res.url);
assertEquals(error.request, req);
assertEquals(error.response, res);
});

Deno.test("HTTPError - create with text", async () => {
const res = new Response("Bad request", { status: 400 });
const error = await HTTPError.create(res);

assert(typeof error.json === "undefined");
assertEquals(error.text, "Bad request");
assert(error.status === 400);
assertEquals(error.response, res);
});

Deno.test("HTTPError - without body", async () => {
Deno.test("YumiError - without body", async () => {
const req = new Request("http://example.com");
const res = new Response(null, { status: 400 });
const error = await HTTPError.create(res);
const error = await YumiError.create(req, res);

assert(typeof error.json === "undefined");
assert(typeof error.text === "undefined");
assert(typeof error.body === "undefined");
assert(error.status === 400);
assertEquals(error.request, req);
assertEquals(error.response, res);
});

Deno.test("isHTTPError with HTTPError", async () => {
assert(
isHTTPError(await HTTPError.create(new Response(null, { status: 400 }))),
);
const req = new Request("http://example.com");
const res = new Response(null, { status: 400 });

assert(isHTTPError(await YumiError.create(req, res)));
assert(isHTTPError(new Error("Not a HTTPError")) === false);
});

0 comments on commit 659ed37

Please sign in to comment.