forked from mcollina/autocannon
/
helper.js
201 lines (158 loc) · 4.61 KB
/
helper.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
'use strict'
const http = require('http')
const https = require('https')
const tls = require('tls')
const fs = require('fs')
const path = require('path')
const BusBoy = require('busboy')
function startServer (opts) {
opts = opts || {}
const statusCode = opts.statusCode || 200
const server = http.createServer(handle)
server.autocannonConnects = 0
server.on('connection', () => { server.autocannonConnects++ })
server.listen(opts.socketPath || 0)
function handle (req, res) {
res.statusCode = statusCode
res.end(opts.body || 'hello world')
}
server.unref()
return server
}
function startTrailerServer () {
const server = http.createServer(handle)
function handle (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain', Trailer: 'Content-MD5' })
res.write('hello ')
res.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' })
res.end('world')
}
server.listen(0)
server.unref()
return server
}
// this server won't reply to requests
function startTimeoutServer () {
const server = http.createServer(() => {})
server.listen(0)
server.unref()
return server
}
// this server destroys the socket on connection, should result in ECONNRESET
function startSocketDestroyingServer () {
const server = http.createServer(handle)
function handle (req, res) {
res.destroy()
server.close()
}
server.listen(0)
server.unref()
return server
}
// this server won't reply to requests
function startHttpsServer () {
const options = {
key: fs.readFileSync(path.join(__dirname, '/key.pem')),
cert: fs.readFileSync(path.join(__dirname, '/cert.pem')),
passphrase: 'test'
}
const server = https.createServer(options, handle)
server.listen(0)
function handle (req, res) {
res.end('hello world')
}
server.unref()
return server
}
// this server will echo the SNI Server Name in a HTTP header
function startTlsServer () {
const key = fs.readFileSync(path.join(__dirname, '/key.pem'))
const cert = fs.readFileSync(path.join(__dirname, '/cert.pem'))
const passphrase = 'test'
var servername = ''
const options = {
key,
cert,
passphrase,
SNICallback: function (name, cb) {
servername = name
cb(null, tls.createSecureContext({
key,
cert,
passphrase
}))
}
}
const server = tls.createServer(options, handle)
server.listen(0)
function handle (socket) {
socket.on('data', function (data) {
// Assume this is a http get request and send back the servername in an otherwise empty reponse.
socket.write('HTTP/1.1 200 OK\nX-servername: ' + servername + '\nContent-Length: 0\n\n')
socket.setEncoding('utf8')
socket.pipe(socket)
})
socket.on('error', noop)
}
server.unref()
return server
}
function startMultipartServer (opts = {}, test = () => {}) {
const server = http.createServer(handle)
const allowed = ['POST', 'PUT']
function handle (req, res) {
if (allowed.includes(req.method)) {
const bboy = new BusBoy({ headers: req.headers, ...opts })
const fileData = []
const payload = {}
bboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
payload[fieldname] = {
filename,
encoding,
mimetype
}
file.on('data', data => fileData.push(data))
})
bboy.on('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
payload[fieldname] = val
})
bboy.on('finish', () => {
res.statusCode = fileData.length ? 201 : 400
res.write(JSON.stringify(payload))
res.end()
test(payload)
})
req.pipe(bboy)
} else {
res.statusCode = 404
res.write(JSON.stringify({}))
res.end()
}
}
server.listen(0)
server.unref()
return server
}
function startBasicAuthServer () {
const server = http.createServer(handle)
function handle (req, res) {
if (!req.headers.authorization || req.headers.authorization.indexOf('Basic ') === -1) {
res.writeHead(401)
return res.end()
}
res.writeHead(200)
res.end('hello world')
}
server.listen(0)
server.unref()
return server
}
module.exports.startServer = startServer
module.exports.startTimeoutServer = startTimeoutServer
module.exports.startSocketDestroyingServer = startSocketDestroyingServer
module.exports.startHttpsServer = startHttpsServer
module.exports.startTrailerServer = startTrailerServer
module.exports.startTlsServer = startTlsServer
module.exports.startMultipartServer = startMultipartServer
module.exports.startBasicAuthServer = startBasicAuthServer
function noop () {}