-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.js
330 lines (276 loc) · 7.68 KB
/
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 express = require('express')
const bodyParser = require('body-parser')
const { describe, test, before, after, afterEach } = require('node:test')
const assert = require('node:assert/strict')
const zlib = require('zlib')
const fs = require('fs')
const centra = require('./')
const app = express()
app.use(bodyParser.raw())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.get('/redirectRequireCookie', (req, res) => {
if (req.header('Cookie') !== 'a=b') {
res.status(401)
res.send('Missing Cookie')
return
}
res.redirect('http://localhost:8082/simpleGet')
})
app.get('/redirect2', (req, res) => {
res.redirect('/redirect')
})
app.get('/redirect', (req, res) => {
res.redirect('http://localhost:8082/simpleGet')
})
app.get('/simpleGet', (req, res) => {
if (req.header("Cookie")) {
res.status(401)
res.send('Cookie header included')
return
}
res.status(200)
res.send('Hey')
})
app.post('/testJSON', (req, res) => {
if (req.body.hey === 'hi' && req.header('content-type') === 'application/json') {
res.status(200)
res.send('Done')
}
else {
res.status(400)
res.send('Bad content ' + JSON.stringify(req.body))
}
})
app.get('/json204', (req, res) => {
res.status(204)
res.end()
})
app.get('/stream', (req, res) => {
res.sendFile(__filename)
})
app.get('/forever', (req, res) => {
res.status(200)
setInterval(() => {
res.write('a')
}, 1)
})
app.get('/compressed', (req, res) => {
const encoding = req.query['encoding']
if (encoding === 'gzip') {
res.status(200)
res.header('content-encoding', 'gzip')
res.send(zlib.gzipSync(fs.readFileSync(__filename)))
}
else if (encoding === 'deflate') {
res.status(200)
res.header('content-encoding', 'deflate')
res.send(zlib.deflateSync(fs.readFileSync(__filename)))
}
else if (encoding === 'br') {
res.status(200)
res.header('content-encoding', 'br')
res.send(zlib.brotliCompressSync(fs.readFileSync(__filename)))
}
})
app.post('/testForm', (req, res) => {
if (req.body.hey === 'hi' && req.header('content-type') === 'application/x-www-form-urlencoded') {
res.status(200)
res.end()
return
}
else {
res.status(400)
res.send('Missing form data. ' + JSON.stringify(req.body))
}
})
app.get('/doNotResolve', (req, res) => {})
app.get('/updates', (req, res) => {
if (req.query['hey'] === 'hello' && req.headers['hey'] === 'hello' && req.headers['test'] === 'testing') {
res.status(200)
res.end()
}
else {
res.status(400)
res.end()
}
})
app.get('/testOptionChange', (req, res) => {
res.status(200).end()
})
app.get('/abort', (req, res) => {
res.write('partial')
res.socket.destroy()
})
let servers = []
let failed = false
describe('Centra', () => {
before(() => {
servers.push(app.listen(8081, () => {
servers.push(app.listen(8082))
}))
})
after(() => {
console.log('Closing servers...')
servers.forEach((s) => {
s.closeAllConnections()
s.close()
})
console.log('Exit', process.exitCode)
process.exit()
})
test('Simple GET', async (t) => {
if (await (await centra('http://localhost:8081/simpleGet').send()).text() === 'Hey') {
return
}
throw new Error('fail')
})
test('GET redirect', async (t) => {
if (await (await centra('http://localhost:8081/redirect').followRedirects(5).send()).text() === 'Hey') {
return
}
throw new Error('fail')
})
test('GET too many redirects', async (t) => {
try {
await centra('http://localhost:8081/redirect2').followRedirects(1).send()
}
catch (err) {
t.diagnostic('Got error: ' + err)
return
}
throw new Error('Missing error')
})
test('GET no redirect', async (t) => {
const status = (await centra('http://localhost:8081/redirect').send()).statusCode
if (status === 302) {
return
}
throw new Error(status)
})
test('GET redirect with Cookie', async (t) => {
const status = (await centra('http://localhost:8081/redirectRequireCookie').header('Cookie', 'a=b').header('hi', 'ok').followRedirects(2).send()).statusCode
if (status === 200) {
return
}
throw new Error(status)
})
test('Simple JSON POST', async (t) => {
const res = await centra('http://localhost:8081/testJSON', 'POST').body({
'hey': 'hi'
}, 'json').send()
if (res.statusCode === 200) {
return
}
throw new Error(await res.text())
})
test('Simple form POST', async (t) => {
const res = await centra('http://localhost:8081/testForm', 'POST').body({
'hey': 'hi'
}, 'form').send()
if (res.statusCode === 200) {
return
}
throw new Error(await res.text())
})
test('Request timeout', async (t) => {
try {
await centra('http://localhost:8081/doNotResolve').timeout(100).send()
}
catch (err) {
t.diagnostic('Timeout error, as expected. ' + err)
return
}
throw new Error('fail')
})
test('Update request info on the fly', async (t) => {
const res = await centra('http://localhost:8081/test').path('../updates').query('hey', 'hello').header('hey', 'hello').header({
'test': 'testing'
}).send()
if (res.statusCode === 200) {
return
}
throw new Error(res.statusCode)
})
test('Multiple query and multiple header', async (t) => {
const res = await centra('http://localhost:8081/updates').query({
'hey': 'hello'
}).header({
'hey': 'hello',
'test': 'testing'
}).send()
if (res.statusCode === 200) {
return
}
throw new Error(res.statusCode)
})
test('Stream a response', async (t) => {
const res = await centra('http://localhost:8081/stream').stream().send()
return new Promise((resolve, reject) => {
res.once('data', () => {
resolve('Got data!')
})
res.on('error', (err) => {
reject(err)
})
})
})
test('Edit core HTTP option', async (t) => {
const res = await centra('http://localhost:8081').option('path', '/testOptionChange').send()
if (res.statusCode === 200) {
return
}
throw new Error(res.statusCode)
})
test('Brotli compression', async (t) => {
const res = await centra('http://localhost:8081/compressed').query('encoding', 'br').compress().send()
if ((await res.text()).includes('hj988SXACXhzxbh89899') && res.headers['content-encoding'] === 'br') {
t.diagnostic('Processed ' + res.headers['content-encoding'] + ' compression.')
return
}
throw new Error('fail')
})
test('Gzip compression', async (t) => {
const res = await centra('http://localhost:8081/compressed').query('encoding', 'gzip').compress().send()
if ((await res.text()).includes('hj9889ASXhzxbh89899') && res.headers['content-encoding'] === 'gzip') {
t.diagnostic('Processed ' + res.headers['content-encoding'] + ' compression.')
return
}
throw new Error('fail')
})
test('Deflate compression', async (t) => {
const res = await centra('http://localhost:8081/compressed').query('encoding', 'deflate').compress().send()
if ((await res.text()).includes('hj988SXACXhzxbh89899') && res.headers['content-encoding'] === 'deflate') {
t.diagnostic('Processed ' + res.headers['content-encoding'] + ' compression.')
return
}
throw new Error('fail')
})
test('Buffer data', async (t) => {
const res = await centra('http://localhost:8081/testJSON', 'POST').header("Content-Type", "application/json").body(Buffer.from(JSON.stringify({
'hey': 'hi'
}))).send()
if (res.statusCode === 200) {
return
}
throw new Error(await res.text())
})
test('Error when too much data buffered', async (t) => {
try {
const req = centra('http://localhost:8081/forever')
req.resOptions.maxBuffer = 100
await req.send()
}
catch (err) {
return
}
throw new Error('fail')
})
test('Empty JSON data parses to null', async (t) => {
const res = await centra('http://localhost:8081/json204').body(Buffer.from(JSON.stringify({
'hey': 'hi'
}))).send()
const parsed = await res.json()
assert.equal(parsed, null)
})
})