-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
responder_test.ts
142 lines (132 loc) · 4.27 KB
/
responder_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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { createResponder } from "./responder.ts";
import {
assert,
assertEquals,
assertThrowsAsync,
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { StringReader } from "./vendor/https/deno.land/std/io/readers.ts";
import { readResponse } from "./serveio.ts";
import { group } from "./test_util.ts";
group("responder", (t) => {
t.test("basic", async function () {
const w = new Deno.Buffer();
const res = createResponder(w);
assert(!res.isResponded());
await res.respond({
status: 200,
headers: new Headers({
"content-type": "text/plain",
}),
body: new StringReader("ok"),
});
assert(res.isResponded());
const resp = await readResponse(w);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/plain");
assertEquals(await resp.text(), "ok");
});
t.test("respond() should throw if already responded", async function () {
const w = new Deno.Buffer();
const res = createResponder(w);
await res.respond({
status: 200,
headers: new Headers(),
});
await assertThrowsAsync(
async () =>
res.respond({
status: 200,
headers: new Headers(),
}),
Error,
"responded",
);
});
t.test("sendFile() basic", async function () {
const w = new Deno.Buffer();
const res = createResponder(w);
await res.sendFile("./fixtures/sample.txt");
const resp = await readResponse(w);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/plain");
assertEquals(await resp.text(), "sample");
});
t.test("sendFile() should throw if file not found", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
await assertThrowsAsync(
() => res.sendFile("./fixtures/not-found"),
Deno.errors.NotFound,
);
});
t.test("sendFile() with attachment", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
await res.sendFile("./fixtures/sample.txt", {
contentDisposition: "inline",
});
const resp = await readResponse(w);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-disposition"), "inline");
assertEquals(await resp.text(), "sample");
});
t.test("sendFile() with attachment", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
await res.sendFile("./fixtures/sample.txt", {
contentDisposition: "attachment",
});
const resp = await readResponse(w);
assertEquals(resp.status, 200);
assertEquals(
resp.headers.get("content-disposition"),
'attachment; filename="sample.txt"',
);
assertEquals(await resp.text(), "sample");
});
t.test("redirect() should set Location header", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
await res.redirect("/index.html");
const { status, headers } = await readResponse(w);
assertEquals(status, 302);
assertEquals(headers.get("location"), "/index.html");
});
t.test("redirect() should use partial body for response", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
await res.redirect("/", {
status: 303,
headers: new Headers({ "content-type": "text/plain" }),
body: "Redirecting...",
});
const resp = await readResponse(w);
assertEquals(resp.status, 303);
assertEquals(resp.headers.get("content-type"), "text/plain");
assertEquals(await resp.text(), "Redirecting...");
});
t.test("resirect() should throw error if status code is not in 300~399", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
await assertThrowsAsync(
async () => {
await res.redirect("/", { status: 200 });
},
Error,
"redirection status code",
);
});
t.test("markResponded()", async () => {
const w = new Deno.Buffer();
const res = createResponder(w);
res.markAsResponded(200);
assertEquals(res.isResponded(), true);
assertEquals(res.respondedStatus(), 200);
await assertThrowsAsync(
() => res.respond({ status: 404, body: "404" }),
Error,
"already",
);
});
});