-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,97 @@ | ||
import { createFetchError, isHTTPError } from "./http_error.ts"; | ||
import { createFetchError, FetchError, isHTTPError } from "./http_error.ts"; | ||
import { assert, assertEquals } from "std/testing/asserts.ts"; | ||
|
||
Deno.test("FetchError", async () => { | ||
const req = new Request("http://example.com"); | ||
Deno.test("createFetchError - with body as JSON", async () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response(JSON.stringify({ foo: "bar" }), { | ||
status: 500, | ||
statusText: "Internal Server Error", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
const error = await createFetchError(req, res); | ||
|
||
assertEquals(error.body, { foo: "bar" }); | ||
assert(error.status === 500); | ||
assert(error.url === res.url); | ||
assertEquals(error.status, 500); | ||
assertEquals(error.url, res.url); | ||
assertEquals(error.request, req); | ||
assertEquals(error.response, res); | ||
assertEquals( | ||
error.message, | ||
"500 Internal Server Error (http://example.com/)", | ||
); | ||
}); | ||
|
||
Deno.test("FetchError - without body", async () => { | ||
const req = new Request("http://example.com"); | ||
const res = new Response(null, { status: 400 }); | ||
Deno.test("createFetchError - without body", async () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response(null, { status: 400, statusText: "Bad Request" }); | ||
|
||
const error = await createFetchError(req, res); | ||
|
||
assert(error.body === null); | ||
assert(error.message === "Unknown error"); | ||
assert(error.status === 400); | ||
assertEquals(error.body, null); | ||
assertEquals(error.message, "400 Bad Request (http://example.com/)"); | ||
assertEquals(error.status, 400); | ||
assertEquals(error.request, req); | ||
assertEquals(error.response, res); | ||
}); | ||
|
||
Deno.test("FetchError - with body as string", async () => { | ||
const req = new Request("http://example.com"); | ||
Deno.test("createFetchError - with body as string", async () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response("Bad request", { status: 400 }); | ||
|
||
const error = await createFetchError(req, res); | ||
|
||
assert(error.message === "Bad request"); | ||
assert(error.body === "Bad request"); | ||
assertEquals(error.message, "400 (http://example.com/)"); | ||
assertEquals(error.body, "Bad request"); | ||
}); | ||
|
||
Deno.test("FetchError - with invalid json", async () => { | ||
const req = new Request("http://example.com"); | ||
Deno.test("createFetchError - with invalid JSON", async () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response("{ abc: undefined }", { | ||
status: 400, | ||
statusText: "Bad Request", | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
|
||
const error = await createFetchError(req, res); | ||
|
||
assert(error.message === "{ abc: undefined }"); | ||
assert(error.body === "{ abc: undefined }"); | ||
assertEquals(error.message, "400 Bad Request (http://example.com/)"); | ||
assertEquals(error.body, "{ abc: undefined }"); | ||
}); | ||
|
||
Deno.test("FetchError - with statusText message", async () => { | ||
const req = new Request("http://example.com"); | ||
Deno.test("createFetchError - with statusText message", async () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response(undefined, { | ||
status: 400, | ||
statusText: "Bad request", | ||
}); | ||
|
||
const error = await createFetchError(req, res); | ||
|
||
assert(error.message === "Bad request"); | ||
assert(error.body === null); | ||
assertEquals(error.message, "400 Bad request (http://example.com/)"); | ||
assertEquals(error.body, null); | ||
}); | ||
|
||
Deno.test("isHTTPError", async () => { | ||
const req = new Request("http://example.com"); | ||
Deno.test("isHTTPError", () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response(null, { status: 400 }); | ||
|
||
assert(isHTTPError(await createFetchError(req, res))); | ||
assert(isHTTPError(new Error("Not a HTTPError")) === false); | ||
const fetchError = new FetchError("Test error", req, res, null); | ||
const regularError = new Error("Not a HTTPError"); | ||
|
||
assert(isHTTPError(fetchError)); | ||
assert(!isHTTPError(regularError)); | ||
}); | ||
|
||
Deno.test("isHTTPError", () => { | ||
const req = new Request("http://example.com/"); | ||
const res = new Response(null, { status: 400 }); | ||
|
||
const fetchError = new FetchError("Test error", req, res, null); | ||
const regularError = new Error("Not a HTTPError"); | ||
|
||
assert(isHTTPError(fetchError)); | ||
assert(!isHTTPError(regularError)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters