-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtoolkit.js
173 lines (164 loc) · 6.21 KB
/
toolkit.js
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
import { assertEquals, assertStrictEquals, assertStringIncludes } from '../dev-dependencies.ts';
import pogo from '../main.ts';
const { test } = Deno;
test('h.response() set JSON body', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/',
handler(request, h) {
return h.response({ hello : 'world' });
}
});
const response = await server.inject('/');
assertStrictEquals(response.status, 200);
assertStrictEquals(response.headers.get('content-type'), 'application/json; charset=utf-8');
assertStrictEquals(await response.text(), JSON.stringify({ hello : 'world' }));
});
test('h.redirect()', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/redirect',
handler(request, h) {
return h.redirect('/one');
}
});
server.route({
method : 'GET',
path : '/redirect-no-rewrite',
handler(request, h) {
return h.redirect('/two').rewritable(false);
}
});
server.route({
method : 'GET',
path : '/permanent',
handler(request, h) {
return h.redirect('/three').permanent();
}
});
server.route({
method : 'GET',
path : '/permanent-no-rewrite',
handler(request, h) {
return h.redirect('/four').permanent().rewritable(false);
}
});
const responseOne = await server.inject('/redirect');
const responseTwo = await server.inject('/redirect-no-rewrite');
const responseThree = await server.inject('/permanent');
const responseFour = await server.inject('/permanent-no-rewrite');
assertStrictEquals(responseOne.status, 302);
assertStrictEquals(responseOne.headers.get('content-type'), null);
assertStrictEquals(responseOne.headers.get('location'), '/one');
assertStrictEquals(await responseOne.text(), '');
assertStrictEquals(responseTwo.status, 307);
assertStrictEquals(responseTwo.headers.get('content-type'), null);
assertStrictEquals(responseTwo.headers.get('location'), '/two');
assertStrictEquals(await responseTwo.text(), '');
assertStrictEquals(responseThree.status, 301);
assertStrictEquals(responseThree.headers.get('content-type'), null);
assertStrictEquals(responseThree.headers.get('location'), '/three');
assertStrictEquals(await responseThree.text(), '');
assertStrictEquals(responseFour.status, 308);
assertStrictEquals(responseFour.headers.get('content-type'), null);
assertStrictEquals(responseFour.headers.get('location'), '/four');
assertStrictEquals(await responseFour.text(), '');
});
test('h.file()', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/names',
handler(request, h) {
return h.file('./test/fixture/names.json');
}
});
const response = await server.inject('/names');
assertStrictEquals(response.status, 200);
assertStrictEquals(response.headers.get('content-type'), 'application/json; charset=utf-8');
assertStrictEquals(await response.text(), '[\n "Alice",\n "Bob",\n "Cara"\n]\n');
});
test('h.file() outside default confine', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/forbid',
handler(request, h) {
return h.file('/etc/hosts');
}
});
const response = await server.inject('/forbid');
assertStrictEquals(response.status, 403);
assertStrictEquals(response.headers.get('content-type'), 'application/json; charset=utf-8');
assertStrictEquals(await response.text(), JSON.stringify({
error : 'Forbidden',
message : 'Forbidden',
status : 403
}));
});
test('h.directory() serve files', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/dirtest/{file*}',
handler(request, h) {
return h.directory('./test/fixture');
}
});
const response = await server.inject('/dirtest/names.json');
assertStrictEquals(response.status, 200);
assertStrictEquals(response.headers.get('content-type'), 'application/json; charset=utf-8');
assertStrictEquals(await response.text(), '[\n "Alice",\n "Bob",\n "Cara"\n]\n');
});
test('h.directory() forbids listing by default', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/dirtest/{file?}',
handler(request, h) {
return h.directory('./test/fixture');
}
});
const response = await server.inject('/dirtest/');
assertStrictEquals(response.status, 403);
assertStrictEquals(response.headers.get('content-type'), 'application/json; charset=utf-8');
assertStrictEquals(await response.text(), JSON.stringify({
error : 'Forbidden',
message : 'Forbidden',
status : 403
}));
});
test('h.directory() listing with relative path', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/dirtest/{file?}',
handler(request, h) {
return h.directory('./test/fixture', {listing: true});
}
});
const response = await server.inject('/dirtest/');
assertStrictEquals(response.status, 200);
assertStrictEquals(response.headers.get('content-type'), 'text/html; charset=utf-8');
const responseText = await response.text();
assertStringIncludes(responseText, '<html>');
assertStringIncludes(responseText, 'names.json');
});
test('h.directory() listing with partial relative path', async () => {
const server = pogo.server();
server.route({
method : 'GET',
path : '/dirtest/{file?}',
handler(request, h) {
return h.directory('test/fixture', {listing: true});
}
});
const response = await server.inject('/dirtest/');
assertStrictEquals(response.status, 200);
assertStrictEquals(response.headers.get('content-type'), 'text/html; charset=utf-8');
const responseText = await response.text();
assertStringIncludes(responseText, '<html>');
assertStringIncludes(responseText, 'names.json');
});