-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.ts
306 lines (274 loc) · 9.51 KB
/
response_test.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { StringReader } from "./deps.ts";
import { assert, assertEquals, assertObjectMatch } from "./dev_deps.ts";
import { Response } from "./response.ts";
import { MultiLineResponseCodes } from "./model.ts";
Deno.test("Response", async (t) => {
await t.step("status", async (t) => {
await t.step("is anything passed to constructor", () => {
for (let status = 100; status < 600; status++) {
const response = new Response(new StringReader(""), { status });
assertEquals(response.status, status);
}
});
});
await t.step("headers", async (t) => {
await t.step("is anything passed to constructor", () => {
const headers = new Headers({
"foo": "bar",
"bar": "foo",
});
const response = new Response(new StringReader(""), { headers });
assertObjectMatch(
Object.fromEntries(response.headers),
Object.fromEntries(headers),
);
});
});
await t.step("body", async (t) => {
await t.step("is empty from empty reader", async () => {
const response = new Response(new StringReader(""));
const body = await response.text();
assert(!body);
});
await t.step(
"is empty with non multi-line status and any reader",
async () => {
const response = new Response(new StringReader("foobar"), {
status: 200,
});
const body = await response.text();
assert(!body);
},
);
await t.step(
"is empty with 211 status (generated by GROUP command) and any reader",
async () => {
const response = new Response(new StringReader("foobar"), {
status: 211,
});
const body = await response.text();
assert(!body);
},
);
await t.step("is empty from reader of single dot", async () => {
for await (const status of MultiLineResponseCodes) {
const response = new Response(new StringReader("."), { status });
const body = await response.text();
assert(!body);
}
});
await t.step("is empty from reader of terminating line", async () => {
for await (const status of MultiLineResponseCodes) {
const response = new Response(new StringReader(".\r\n"), { status });
const body = await response.text();
assert(!body);
}
});
await t.step(
"is full line from reader of line before terminating line",
async () => {
for await (
const status of MultiLineResponseCodes.filter((s) => s !== 221)
) {
const response = new Response(new StringReader("foobar\r\n.\r\n"), {
status,
});
const body = await response.text();
assertEquals(body, "foobar\r\n");
}
},
);
await t.step("undo dot-stuffing", async () => {
for await (
const status of MultiLineResponseCodes.filter((s) => s !== 221)
) {
let response = new Response(new StringReader("..foobar\r\n.\r\n"), {
status,
});
let body = await response.text();
assertEquals(body, ".foobar\r\n");
// But it should not undo single-dot.
response = new Response(new StringReader(".foobar\r\n.\r\n"), {
status,
});
body = await response.text();
assertEquals(body, ".foobar\r\n");
}
});
await t.step(
"is full line with 211 status (generated by LISTGROUP command) and special statusText",
async () => {
let response = new Response(new StringReader("foobar\r\n.\r\n"), {
status: 211,
statusText: "articles follow",
});
let body = await response.text();
assertEquals(body, "foobar\r\n");
response = new Response(new StringReader("foobar\r\n.\r\n"), {
status: 211,
statusText: "list below",
});
body = await response.text();
assertEquals(body, "foobar\r\n");
},
);
await t.step(
"can also take a ReadableStream as body and just return new Response from it",
async () => {
const status = 101;
const statusText = "capabilities follow";
let response = new Response(new StringReader("foobar\r\n.\r\n"), {
status,
statusText,
});
response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
});
assertEquals(response.status, status);
assertEquals(response.statusText, statusText);
const body = await response.text();
assertEquals(body, "foobar\r\n");
},
);
});
});
Deno.test("Response.from", async (t) => {
function getReader(lines: string[]) {
return new StringReader(lines.join("\r\n") + "\r\n");
}
await t.step("status", async (t) => {
await t.step("is a three-digit status indicator", async () => {
for (let status = 100; status < 600; status++) {
const response = await Response.from(new StringReader(`${status}`));
assertEquals(response.status, status);
}
});
await t.step("MAY have any text after the response code", async () => {
const response = await Response.from(
new StringReader(`205 closing connection`),
);
assertEquals(response.status, 205);
assertEquals(response.statusText, "closing connection");
});
});
await t.step("headers", async (t) => {
await t.step("is empty when status not 220 or 221", async () => {
for (
let status = 100;
status < 600 && status !== 220 && status !== 221;
status++
) {
const response = await Response.from(new StringReader(`${status}`));
assertObjectMatch(response.headers, {});
}
});
await t.step("is empty when input is empty", async () => {
const reader = getReader([
`221`,
`.`,
]);
const response = await Response.from(reader);
assertObjectMatch(response.headers, {});
});
await t.step("is empty from reader of terminating line", async () => {
const reader = getReader([
`221`,
`.`,
]);
const response = await Response.from(reader);
assertObjectMatch(response.headers, {});
});
await t.step("consists of one or more header lines", async () => {
const reader = getReader([
`221`,
`path: pathost!demo!whitehouse!not-for-mail`,
`.`,
]);
const response = await Response.from(reader);
const headers = response.headers;
assertObjectMatch(Object.fromEntries(headers), {
"path": "pathost!demo!whitehouse!not-for-mail",
});
});
await t.step("is normalized to lowercase name", async () => {
const reader = getReader([
`221`,
`Path: pathost!demo!whitehouse!not-for-mail`,
`From: "Demo User" <[email protected]>`,
`Newsgroups: misc.test`,
`Subject: I am just a test article`,
`Date: 6 Oct 1998 04:38:40 -0500`,
`Organization: An Example Net, Uncertain, Texas`,
`Message-ID: <[email protected]>`,
`.`,
]);
const response = await Response.from(reader);
const headers = response.headers;
assertObjectMatch(Object.fromEntries(headers), {
"path": "pathost!demo!whitehouse!not-for-mail",
"from": `"Demo User" <[email protected]>`,
"newsgroups": "misc.test",
"subject": "I am just a test article",
"date": "6 Oct 1998 04:38:40 -0500",
"organization": "An Example Net, Uncertain, Texas",
"message-id": "<[email protected]>",
});
});
await t.step(
"is separated from the body by a single empty line",
async () => {
const reader = getReader([
`220`,
`Path: pathost!demo!whitehouse!not-for-mail`,
`From: "Demo User" <[email protected]>`,
`Newsgroups: misc.test`,
`Subject: I am just a test article`,
`Date: 6 Oct 1998 04:38:40 -0500`,
`Organization: An Example Net, Uncertain, Texas`,
`Message-ID: <[email protected]>`,
``,
`This is just a test article.`,
`.`,
]);
const response = await Response.from(reader);
const headers = response.headers;
assertObjectMatch(Object.fromEntries(headers), {
"path": "pathost!demo!whitehouse!not-for-mail",
"from": `"Demo User" <[email protected]>`,
"newsgroups": "misc.test",
"subject": "I am just a test article",
"date": "6 Oct 1998 04:38:40 -0500",
"organization": "An Example Net, Uncertain, Texas",
"message-id": "<[email protected]>",
});
const body = await response.text();
assertEquals(body, "This is just a test article.\r\n");
},
);
});
await t.step("body", async (t) => {
await t.step("is null when no body", async () => {
const reader = getReader([
`111`,
]);
const response = await Response.from(reader);
assertEquals(response.body, null);
});
await t.step("is null when only headers", async () => {
const reader = getReader([
`221`,
`Path: pathost!demo!whitehouse!not-for-mail`,
`From: "Demo User" <[email protected]>`,
`Newsgroups: misc.test`,
`Subject: I am just a test article`,
`Date: 6 Oct 1998 04:38:40 -0500`,
`Organization: An Example Net, Uncertain, Texas`,
`Message-ID: <[email protected]>`,
`.`,
]);
const response = await Response.from(reader);
assertEquals(response.body, null);
});
});
});