-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_http_server_response_test.js
More file actions
137 lines (115 loc) · 3.5 KB
/
_http_server_response_test.js
File metadata and controls
137 lines (115 loc) · 3.5 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
// Copyright Titanium I.T. LLC.
import assert from "util/assert.js";
import { HttpServerResponse } from "http/http_server_response.js";
describe("HttpServerResponse", () => {
describe("generic factory", () => {
it("has status code, headers, body, content type, and debug field", () => {
const response = HttpServerResponse.create({
status: 999,
headers: { "my-header": "my-value" },
body: "my body",
contentType: "my-content-type",
debug: "my debug field",
});
assert.equal(response.status, 999, "status");
assert.equal(response.body, "my body", "body");
assert.deepEqual(response.headers, {
"my-header": "my-value",
"content-type": "my-content-type",
}, "headers");
// debug field is purely for convenience of troubleshooting failed tests--it's not accessible,
// but it shows up in assertions and console logs.
assert.equal(response._debug, "my debug field", "debug field");
});
it("headers, contentType, and debug field are optional", () => {
assertResponseEquals(HttpServerResponse.create({
status: 999,
body: "my body",
}), {
status: 999,
body: "my body",
});
});
it("it converts errors in debug field to stack traces (for ease of debugging)", () => {
const response = HttpServerResponse.create({
status: 999,
body: "irrelevant body",
debug: new Error("my error"),
});
assert.match(response._debug, /^Error: my error\n at/);
});
it("doesn't change response headers when original header object changes", () => {
const headers = { "my-header": "my-value" };
const response = HttpServerResponse.create({
status: 999,
body: "irrelevant body",
headers,
});
headers["new-header"] = "should not appear in response";
assert.deepEqual(response.headers, { "my-header": "my-value" });
});
it("headers' content-type field takes precedence over contentType option", () => {
const response = HttpServerResponse.create({
status: 999,
headers: { "content-type": "overridden" },
body: "irrelevant-body",
contentType: "my content type",
});
assert.deepEqual(response.headers, { "content-type": "overridden" });
});
});
describe("content-type factories", () => {
it("plain text", () => {
assertResponseEquals(HttpServerResponse.createPlainTextResponse({
status: 999,
headers: { "my-header": "my-value" },
body: "my body",
debug: "my debug",
}), {
status: 999,
headers: {
"content-type": "text/plain; charset=utf-8",
"my-header": "my-value",
},
body: "my body",
debug: "my debug",
});
});
it("HTML", () => {
assertResponseEquals(HttpServerResponse.createHtmlResponse({
status: 999,
headers: { "my-header": "my-value" },
body: "my body",
debug: "my debug",
}), {
status: 999,
headers: {
"content-type": "text/html; charset=utf-8",
"my-header": "my-value",
},
body: "my body",
debug: "my debug",
});
});
it("JSON", () => {
assertResponseEquals(HttpServerResponse.createJsonResponse({
status: 999,
headers: { "my-header": "my-value" },
body: { "my-json": "my-body" },
debug: "my debug",
}), {
status: 999,
headers: {
"content-type": "application/json",
"my-header": "my-value",
},
body: JSON.stringify({ "my-json": "my-body" }),
debug: "my debug",
});
});
});
});
function assertResponseEquals(actual, expectedOptions) {
const expected = new HttpServerResponse(expectedOptions);
assert.deepEqual(actual, expected);
}