-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathutils.js
More file actions
184 lines (151 loc) · 4.36 KB
/
utils.js
File metadata and controls
184 lines (151 loc) · 4.36 KB
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
const finalhandler = require('../..')
const SlowWriteStream = require('./sws')
const assert = require('node:assert')
const http = require('node:http')
const http2 = require('node:http2')
const supertest = require('supertest')
exports.createError = createError
exports.getTestHelpers = getTestHelpers
exports.SlowWriteStream = SlowWriteStream
exports.shouldHaveStatusMessage = shouldHaveStatusMessage
exports.shouldNotHaveBody = shouldNotHaveBody
exports.shouldNotHaveHeader = shouldNotHaveHeader
function createError (message, props) {
const err = new Error(message)
if (props) {
for (const prop in props) {
err[prop] = props[prop]
}
}
return err
}
function getTestHelpers (type) {
const { createServer } = type === 'http2' ? http2 : http
return {
createServer: (err, opts) =>
createServer((req, res) => {
const done = finalhandler(req, res, opts)
if (typeof err === 'function') {
err(req, res, done)
return
}
done(err)
}),
request: (server, options) => supertest(server, { ...options, http2: type === 'http2' }),
rawrequest: type === 'http2' ? rawrequestHTTP2 : rawrequest
}
}
function rawrequest (server) {
const _headers = {}
let _path
function expect (status, body, callback) {
if (arguments.length === 2) {
_headers[status.toLowerCase()] = body
return this
}
server.listen(function onlisten () {
const addr = this.address()
const port = addr.port
const req = http.get({
host: '127.0.0.1',
path: _path,
port: port
})
req.on('error', callback)
req.on('response', function onresponse (res) {
let buf = ''
res.setEncoding('utf8')
res.on('data', (s) => { buf += s })
res.on('end', () => {
let err = null
try {
for (const key in _headers) {
assert.strictEqual(res.headers[key], _headers[key])
}
assert.strictEqual(res.statusCode, status)
if (body instanceof RegExp) {
assert.ok(body.test(buf), 'expected body ' + buf + ' to match ' + body)
} else {
assert.strictEqual(buf, body, 'expected ' + body + ' response body, got ' + buf)
}
} catch (e) {
err = e
}
server.close()
callback(err)
})
})
})
}
return {
get: (path) => {
_path = path
return { expect }
}
}
}
function rawrequestHTTP2 (server) {
const _headers = {}
let _path
function expect (status, body, callback) {
if (arguments.length === 2) {
_headers[status.toLowerCase()] = body
return this
}
server.listen(function onlisten () {
let buf = ''
let resHeaders
const addr = this.address()
const port = addr.port
const client = http2.connect('http://127.0.0.1:' + port)
const req = client.request({
':method': 'GET',
':path': _path.replace(/http:\/\/localhost/, '')
})
req.on('error', callback)
req.on('response', (responseHeaders) => { resHeaders = responseHeaders })
req.on('data', (s) => { buf += s })
req.on('end', () => {
let err = null
try {
for (const key in _headers) {
assert.strictEqual(resHeaders[key], _headers[key])
}
assert.strictEqual(resHeaders[':status'], status)
if (body instanceof RegExp) {
assert.ok(body.test(buf), 'expected body ' + buf + ' to match ' + body)
} else {
assert.strictEqual(buf, body, 'expected ' + body + ' response body, got ' + buf)
}
} catch (e) {
err = e
}
req.close()
client.close()
server.close()
callback(err)
})
})
}
return {
get: (path) => {
_path = path
return { expect }
}
}
}
function shouldHaveStatusMessage (statusMessage) {
return (test) => {
assert.strictEqual(test.res.statusMessage, statusMessage, 'should have statusMessage "' + statusMessage + '"')
}
}
function shouldNotHaveBody () {
return (res) => {
assert.ok(res.text === '' || res.text === undefined)
}
}
function shouldNotHaveHeader (header) {
return (test) => {
assert.ok(test.res.headers[header] === undefined, 'response does not have header "' + header + '"')
}
}