1 | 'use strict';
|
2 |
|
3 | const http = require('http');
|
4 |
|
5 | const ref = require.main === module;
|
6 |
|
7 | const 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 |
|
30 | server.on('connection', (socket) => {
|
31 | if (!ref)
|
32 | socket.unref();
|
33 | });
|
34 |
|
35 | server.listen(0);
|
36 |
|
37 | exports.port = server.address().port;
|
38 |
|
39 | if (ref)
|
40 | console.log(exports.port);
|