-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.ts
More file actions
138 lines (118 loc) · 5.07 KB
/
errors_test.ts
File metadata and controls
138 lines (118 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { assertEquals, assertInstanceOf } from "@std/assert";
import { ClientError } from "@probitas/client";
import { HttpError } from "./errors.ts";
Deno.test("HttpError", async (t) => {
await t.step("extends ClientError", () => {
const error = new HttpError(500, "Internal Server Error");
assertInstanceOf(error, ClientError);
assertInstanceOf(error, HttpError);
});
await t.step("has correct properties", () => {
const error = new HttpError(404, "Not Found");
assertEquals(error.name, "HttpError");
assertEquals(error.kind, "http");
assertEquals(error.status, 404);
assertEquals(error.statusText, "Not Found");
});
await t.step("generates message from status", () => {
const error = new HttpError(404, "Not Found");
assertEquals(error.message, "404: Not Found");
});
await t.step("includes body in message when provided", () => {
const body = new TextEncoder().encode('{"error": "not found"}');
const error = new HttpError(404, "Not Found", { body });
assertEquals(error.message.includes("404: Not Found"), true);
assertEquals(error.message.includes("not found"), true);
});
await t.step("supports cause option", () => {
const cause = new Error("network error");
const error = new HttpError(500, "Internal Server Error", { cause });
assertEquals(error.cause, cause);
});
await t.step("body is null by default", () => {
const error = new HttpError(500, "Internal Server Error");
assertEquals(error.body, null);
});
await t.step("body property stores raw bytes", () => {
const body = new TextEncoder().encode("error details");
const error = new HttpError(500, "Internal Server Error", { body });
assertInstanceOf(error.body, Uint8Array);
assertEquals(error.body, body);
});
await t.step("headers property stores response headers", () => {
const headers = new Headers({ "Content-Type": "application/json" });
const error = new HttpError(500, "Internal Server Error", { headers });
assertEquals(error.headers?.get("Content-Type"), "application/json");
});
await t.step("text() returns body as string", () => {
const body = new TextEncoder().encode("error message");
const error = new HttpError(500, "Internal Server Error", { body });
assertEquals(error.text, "error message");
});
await t.step("text() returns null when no body", () => {
const error = new HttpError(500, "Internal Server Error");
assertEquals(error.text, null);
});
await t.step("text() can be called multiple times", () => {
const body = new TextEncoder().encode("error message");
const error = new HttpError(500, "Internal Server Error", { body });
assertEquals(error.text, error.text);
});
await t.step("json returns parsed JSON", () => {
const body = new TextEncoder().encode('{"code": "NOT_FOUND", "id": 123}');
const error = new HttpError(404, "Not Found", { body });
assertEquals(error.json, { code: "NOT_FOUND", id: 123 });
});
await t.step("json returns null when no body", () => {
const error = new HttpError(500, "Internal Server Error");
assertEquals(error.json, null);
});
await t.step("json returns null when body is not valid JSON", () => {
const body = new TextEncoder().encode("not json");
const error = new HttpError(500, "Internal Server Error", { body });
assertEquals(error.json, null);
});
await t.step("json can be accessed multiple times", () => {
const body = new TextEncoder().encode('{"error": true}');
const error = new HttpError(500, "Internal Server Error", { body });
assertEquals(error.json, error.json);
});
await t.step("json supports type assertion", () => {
interface ErrorResponse {
code: string;
message: string;
}
const body = new TextEncoder().encode('{"code": "ERR", "message": "fail"}');
const error = new HttpError(500, "Internal Server Error", { body });
const data = error.json as ErrorResponse;
assertEquals(data?.code, "ERR");
assertEquals(data?.message, "fail");
});
await t.step("arrayBuffer returns ArrayBuffer", () => {
const body = new TextEncoder().encode("data");
const error = new HttpError(500, "Internal Server Error", { body });
const buffer = error.arrayBuffer;
assertInstanceOf(buffer, ArrayBuffer);
assertEquals(buffer?.byteLength, 4);
});
await t.step("arrayBuffer returns null when no body", () => {
const error = new HttpError(500, "Internal Server Error");
assertEquals(error.arrayBuffer, null);
});
await t.step("blob returns Blob with content type", () => {
const body = new TextEncoder().encode('{"test": true}');
const headers = new Headers({ "Content-Type": "application/json" });
const error = new HttpError(500, "Internal Server Error", {
body,
headers,
});
const blob = error.blob;
assertInstanceOf(blob, Blob);
assertEquals(blob?.type, "application/json");
assertEquals(blob?.size, body.length);
});
await t.step("blob returns null when no body", () => {
const error = new HttpError(500, "Internal Server Error");
assertEquals(error.blob, null);
});
});