UNPKG

857 BJavaScriptView Raw
1'use strict';
2
3const http = require('http');
4
5const ref = require.main === module;
6
7const server = http.createServer((req, res) => {
8 if (!ref)
9 req.connection.unref();
10 switch (req.url) {
11 case '/invalid-json':
12 res.setHeader('Content-Type', 'application/json');
13 res.end('{ "a": 1');
14 break;
15 case '/form-urlencoded':
16 res.setHeader('Content-Type', 'application/x-www-form-urlencoded');
17 res.end('test=1&hello=world');
18 break;
19 case '/echo': {
20 req.on('data', (c) => { res.write(c); });
21 req.on('end', () => { res.end(); });
22 break;
23 }
24 default:
25 res.end();
26 break;
27 }
28});
29
30server.on('connection', (socket) => {
31 if (!ref)
32 socket.unref();
33});
34
35server.listen(0);
36
37exports.port = server.address().port;
38
39if (ref)
40 console.log(exports.port); // eslint-disable-line no-console