This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
151 lines (111 loc) · 3.85 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
const Retra = require('./')
const w = require('whew')
const c = require('centra')
const app = new Retra()
app.use((req, res, next) => {
if (req.url === '/testExtension') {
res.status(200).end()
}
else next()
})
app.add('GET', '/gimmeBuf', (req, res) => {
res.body(Buffer.from('hey')).end()
})
app.add('GET', '/gimmeQuery', (req, res) => {
res.body({
'name': req.query('name')
}).end()
})
app.add('POST', '/parseJSON', async (req, res) => {
res.body({
'yousent': await req.json()
}).end()
})
app.add('GET', '/statusMessage', (req, res) => {
res.status(200, 'okie dokie').end()
})
app.add('POST', (req, res) => {
res.body('Catchall POST!').end()
})
app.add('/catchallPath', (req, res) => {
res.body('Catchall path!').end()
})
app.add('/compatibilityFeatures', (req, res) => {
res.writeHead(201, {
'test': 'hval'
})
res.end({
'hey': 'hi'
})
})
app.add('/errorThrow', (req, res) => {
throw new Error('AHHHH')
})
app.add('/asyncErrorThrow', async (req, res) => {
throw new Error('AHHHH')
})
app.add((req, res) => {
res.status(404).body('Error: Content not found!').end()
})
w.add('Parse query properties from a GET request', async (result) => {
const res = await c('http://localhost:5138/gimmeQuery').timeout(2000).query('name', 'Ethan').send()
result((await res.json()).name === 'Ethan')
})
w.add('Catchall handle, response status modification', async (result) => {
const res = await c('http://localhost:5138/handleDoesNotExist').timeout(2000).send()
result(res.statusCode === 404)
})
w.add('Extensions making responses', async (result) => {
const res = await c('http://localhost:5138/testExtension').timeout(2000).send()
result(res.statusCode === 200)
})
w.add('Parse request JSON body', async (result) => {
const res = await c('http://localhost:5138/parseJSON', 'POST').body({
'hey': 'hi'
}, 'json').timeout(2000).send()
result((await res.json()).yousent.hey === 'hi')
})
w.add('Catchall path', async (result) => {
const res = await c('http://localhost:5138/catchallPath', 'DELETE').timeout(2000).send()
result((await res.text()) === 'Catchall path!')
})
w.add('Catchall method', async (result) => {
const res = await c('http://localhost:5138/somepath', 'POST').timeout(2000).send()
result((await res.text()) === 'Catchall POST!')
})
w.add('Catchall unspecific', async (result) => {
const res = await c('http://localhost:5138/thisshould404').timeout(2000).send()
result(res.statusCode === 404 && (await res.text()) === 'Error: Content not found!')
})
w.add('Buffer responses through retra', async (result) => {
const res = await c('http://localhost:5138/gimmeBuf').timeout(2000).send()
result((await res.text()) === 'hey')
})
w.add('Status message', async (result) => {
const res = await c('http://localhost:5138/statusMessage').timeout(2000).send()
result(res.coreRes.statusMessage === 'okie dokie', 'Got status message: ' + res.coreRes.statusMessage)
})
w.add('Compatibility features', async (result) => {
const res = await c('http://localhost:5138/compatibilityFeatures').timeout(2000).send()
result((await res.json()).hey === 'hi' && res.headers['test'] === 'hval' && res.statusCode === 201)
})
w.add('Sync error throwing', async (result) => {
const res = await c('http://localhost:5138/errorThrow').timeout(2000).send()
const parsedRes = await res.json()
result(parsedRes.error === 'AHHHH' && parsedRes.path.includes('errorThrow') && res.statusCode === 500)
})
w.add('Async error throwing', async (result) => {
const res = await c('http://localhost:5138/asyncErrorThrow').timeout(2000).send()
const parsedRes = await res.json()
result(parsedRes.error === 'AHHHH' && parsedRes.path.includes('asyncErrorThrow') && res.statusCode === 500)
})
app.on('routeError', (err, req, res) => {
if (res.coreRes.finished === false) {
res.writeHead(500)
res.end({
'error': err.message,
'path': req.parsedUrl.path
})
}
})
app.listen(5138, w.test)