-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmain.test.js
330 lines (297 loc) · 11.6 KB
/
main.test.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const test = require('tap').test;
const path = require('path');
const fs = require('fs');
const request = require('request');
const httpServer = require('../lib/http-server');
const promisify = require('util').promisify;
const requestAsync = promisify(request);
const fsReadFile = promisify(fs.readFile);
// Prevent errors from being swallowed
process.on('uncaughtException', console.error);
const root = path.join(__dirname, 'fixtures', 'root');
// Tests are grouped into those which can run together. The groups are given
// their own port to run on and live inside a Promise. Tests are done when all
// Promise test groups complete.
test('http-server main', (t) => {
Promise.all([
new Promise((resolve) => {
const server = httpServer.createServer({
root,
robots: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true'
},
cors: true,
corsHeaders: 'X-Test',
ext: true,
brotli: true,
gzip: true
});
server.listen(8080, async () => {
try {
// Since none of these depend on anything not already declared, they
// can run on the event loop at their own leisure
await Promise.all([
// request file from root
requestAsync("http://localhost:8080/file").then(async (res) => {
// files should be served from the root
t.equal(res.statusCode, 200);
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim(), 'root file content matches');
}).catch(err => t.fail(err.toString())),
// Request non-existent file
requestAsync("http://localhost:8080/404").then(res => {
t.ok(res);
t.equal(res.statusCode, 404);
}).catch(err => t.fail(err.toString())),
// Request root
requestAsync("http://localhost:8080/").then(res => {
t.ok(res);
t.equal(res.statusCode, 200);
t.match(res.body, './file');
t.match(res.body, './canYouSeeMe');
// Custom headers
t.equal(res.headers['access-control-allow-origin'], '*');
t.equal(res.headers['access-control-allow-credentials'], 'true');
}).catch(err => t.fail(err.toString())),
// Get robots
requestAsync("http://localhost:8080/robots.txt").then(res => {
t.equal(res.statusCode, 200);
}).catch(err => t.fail(err.toString())),
// CORS time
requestAsync({
uri: 'http://localhost:8080',
method: 'OPTIONS',
headers: {
'Access-Control-Request-Method': 'GET',
Origin: 'http://example.com',
'Access-Control-Request-Headers': 'Foobar'
}
}).then(res => {
t.equal(res.statusCode, 204);
t.ok(
res.headers['access-control-allow-headers']
.split(/\s*,\s*/g)
.indexOf('X-Test') >= 0, 204);
}).catch(err => t.fail(err.toString())),
t.test(
"Regression: don't crash on control characters in query strings",
{},
(t) => {
requestAsync({
uri: encodeURI('http://localhost:8080/file?\x0cfoo'),
}).then(res => {
t.equal(res.statusCode, 200);
}).catch(err => t.fail(err.toString()))
.finally(() => t.end());
}
),
// Light compression testing. Heavier compression tests exist in
// compression.test.js
requestAsync({
uri: 'http://localhost:8080/compression/',
headers: {
'accept-encoding': 'gzip'
}
}).then(res => {
t.equal(res.statusCode, 200);
t.equal(res.headers['content-encoding'], 'gzip');
}).catch(err => t.fail(err.toString())),
requestAsync({
uri: 'http://localhost:8080/compression/',
headers: {
'accept-encoding': 'gzip, br'
}
}).then(res => {
t.equal(res.statusCode, 200);
t.equal(res.headers['content-encoding'], 'br');
}).catch(err => t.fail(err.toString())),
requestAsync("http://localhost:8080/htmlButNot").then(res => {
t.equal(res.statusCode, 200);
t.match(res.headers['content-type'], /^text\/html/);
}).catch(err => t.fail(err.toString()))
]);
// Another server proxies 8081 to 8080
const proxyServer = httpServer.createServer({
proxy: "http://localhost:8080",
root: path.join(__dirname, 'fixtures')
});
await new Promise((resolve) => {
proxyServer.listen(8081, async () => {
try {
// Serve files from proxy root
await requestAsync("http://localhost:8081/root/file").then(async (res) => {
t.ok(res);
t.equal(res.statusCode, 200);
// File content matches
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim(), 'proxied root file content matches');
}).catch(err => t.fail(err.toString()));
// Proxy fallback
await requestAsync("http://localhost:8081/file").then(async (res) => {
t.ok(res);
t.equal(res.statusCode, 200);
// File content matches
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim(), 'proxy fallback root file content matches');
}).catch(err => t.fail(err.toString()));
} catch (err) {
t.fail(err.toString());
} finally {
proxyServer.close();
resolve();
}
});
});
} catch (err) {
t.fail(err.toString());
} finally {
server.close();
resolve();
}
});
}),
new Promise((resolve) => {
const server = httpServer.createServer({
root,
username: 'correct_username',
password: 'correct_password'
});
server.listen(8082, async () => {
try {
await Promise.all([
// Bad request with no auth
requestAsync("http://localhost:8082/file").then((res) => {
t.equal(res.statusCode, 401);
t.equal(res.body, 'Access denied', 'Bad auth returns expected body');
}).catch(err => t.fail(err.toString())),
// bad user
requestAsync("http://localhost:8082/file", {
auth: {
user: 'wrong_username',
pass: 'correct_password'
}
}).then((res) => {
t.equal(res.statusCode, 401);
t.equal(res.body, 'Access denied', 'Bad auth returns expected body');
}).catch(err => t.fail(err.toString())),
// bad password
requestAsync("http://localhost:8082/file", {
auth: {
user: 'correct_username',
pass: 'wrong_password'
}
}).then((res) => {
t.equal(res.statusCode, 401);
t.equal(res.body, 'Access denied', 'Bad auth returns expected body');
}).catch(err => t.fail(err.toString())),
// nonexistant file, and bad auth
requestAsync("http://localhost:8082/404", {
auth: {
user: 'correct_username',
pass: 'wrong_password'
}
}).then((res) => {
t.equal(res.statusCode, 401);
t.equal(res.body, 'Access denied', 'Bad auth returns expected body');
}).catch(err => t.fail(err.toString())),
// good path, good auth
requestAsync("http://localhost:8082/file", {
auth: {
user: 'correct_username',
pass: 'correct_password'
}
}).then(async (res) => {
t.equal(res.statusCode, 200);
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim(), 'auth-protected file with good auth has expected file content');
}).catch(err => t.fail(err.toString())),
]);
} catch (err) {
t.fail(err.toString());
} finally {
server.close();
resolve();
}
});
}),
new Promise((resolve) => {
const server = httpServer.createServer({
root,
username: 'correct_username',
password: 123456
});
server.listen(8083, async () => {
try {
await Promise.all([
// regression test
requestAsync("http://localhost:8083/file").then(res => {
t.equal(res.statusCode, 401);
t.equal(res.body, 'Access denied', 'Bad auth returns expected body');
}).catch(err => t.fail(err.toString())),
// regression test, bad username
requestAsync("http://localhost:8083/file", {
auth: {
user: 'wrong_username',
pass: '123456'
}
}).then(res => {
t.equal(res.statusCode, 401);
t.equal(res.body, 'Access denied', 'Bad auth returns expected body');
}).catch(err => t.fail(err.toString())),
// regression test, correct auth, even though the password is a
// different type.
requestAsync("http://localhost:8083/file", {
auth: {
user: 'correct_username',
pass: '123456'
}
}).then(async (res) => {
t.equal(res.statusCode, 200);
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim(), 'numeric auth with good auth has expected file content');
}).catch(err => t.fail(err.toString()))
]);
} catch (err) {
t.fail(err.toString());
} finally {
server.close();
resolve();
}
});
}),
new Promise((resolve) => {
const server = httpServer.createServer({
root,
baseDir: '/test'
});
server.listen(8084, async () => {
try {
await Promise.all([
// should serve files at the specified baseDir
requestAsync("http://localhost:8084/test/file").then(async (res) => {
t.equal(res.statusCode, 200);
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8');
t.equal(res.body.trim(), fileData.trim());
}).catch(err => t.fail(err.toString())),
// should not serve files at the root
requestAsync("http://localhost:8084/file").then((res) => {
t.equal(res.statusCode, 403);
t.equal(res.body, '');
}).catch(err => t.fail(err.toString())),
]);
} catch (err) {
t.fail(err.toString());
} finally {
server.close();
resolve();
}
});
}),
]).then(() => t.end())
.catch(err => {
t.fail(err.toString());
t.end();
});
});