summaryrefslogtreecommitdiff
path: root/test/sequential/test-http-server-keep-alive-timeout-slow-server.js
blob: 1543c1415fa5e92e849216f9ea6247f88241bc24 (plain)
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
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall((req, res) => {
  if (req.url === '/first') {
    res.end('ok');
    return;
  }
  setTimeout(() => {
    res.end('ok');
  }, common.platformTimeout(500));
}, 2));

server.keepAliveTimeout = common.platformTimeout(200);

const agent = new http.Agent({
  keepAlive: true,
  maxSockets: 1
});

function request(path, callback) {
  const port = server.address().port;
  const req = http.request({ agent, path, port }, common.mustCall((res) => {
    assert.strictEqual(res.statusCode, 200);

    res.setEncoding('utf8');

    let result = '';
    res.on('data', (chunk) => {
      result += chunk;
    });

    res.on('end', common.mustCall(() => {
      assert.strictEqual(result, 'ok');
      callback();
    }));
  }));
  req.end();
}

server.listen(0, common.mustCall(() => {
  request('/first', () => {
    request('/second', () => {
      server.close();
    });
  });
}));