summaryrefslogtreecommitdiff
path: root/node_modules/snekfetch/test/server.js
blob: ac75cca4d4e2457a32ce03bf2cca49ce6df94b10 (plain) (blame)
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
const http = require('http');

const ref = require.main === module;

const server = http.createServer((req, res) => {
  if (!ref)
    req.connection.unref();
  switch (req.url) {
    case '/invalid-json':
      res.setHeader('Content-Type', 'application/json');
      res.end('{ "a": 1');
      break;
    case '/form-urlencoded':
      res.setHeader('Content-Type', 'application/x-www-form-urlencoded');
      res.end('test=1&hello=world');
      break;
    case '/echo': {
      let body = '';
      req.on('data', (c) => { body += c; });
      req.on('end', () => {
        res.end(body);
      });
      break;
    }
    default:
      res.end();
      break;
  }
});

server.on('connection', (socket) => {
  if (!ref)
    socket.unref();
});

server.listen(0);

exports.port = server.address().port;

if (ref)
  console.log(exports.port); // eslint-disable-line no-console