• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const http = require('http');
6
7const server = http.createServer(common.mustCall((req, res) => {
8  if (req.url === '/first') {
9    res.end('ok');
10    return;
11  }
12  setTimeout(() => {
13    res.end('ok');
14  }, common.platformTimeout(500));
15}, 2));
16
17server.keepAliveTimeout = common.platformTimeout(200);
18
19const agent = new http.Agent({
20  keepAlive: true,
21  maxSockets: 1
22});
23
24function request(path, callback) {
25  const port = server.address().port;
26  const req = http.request({ agent, path, port }, common.mustCall((res) => {
27    assert.strictEqual(res.statusCode, 200);
28
29    res.setEncoding('utf8');
30
31    let result = '';
32    res.on('data', (chunk) => {
33      result += chunk;
34    });
35
36    res.on('end', common.mustCall(() => {
37      assert.strictEqual(result, 'ok');
38      callback();
39    }));
40  }));
41  req.end();
42}
43
44server.listen(0, common.mustCall(() => {
45  request('/first', () => {
46    request('/second', () => {
47      server.close();
48    });
49  });
50}));
51