• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25
26const http = require('http');
27
28
29let serverSocket = null;
30const server = http.createServer(function(req, res) {
31  // They should all come in on the same server socket.
32  if (serverSocket) {
33    assert.strictEqual(req.socket, serverSocket);
34  } else {
35    serverSocket = req.socket;
36  }
37
38  res.end(req.url);
39});
40server.listen(0, function() {
41  makeRequest(expectRequests);
42});
43
44const agent = http.Agent({ keepAlive: true });
45
46
47let clientSocket = null;
48const expectRequests = 10;
49let actualRequests = 0;
50
51
52function makeRequest(n) {
53  if (n === 0) {
54    server.close();
55    agent.destroy();
56    return;
57  }
58
59  const req = http.request({
60    port: server.address().port,
61    path: `/${n}`,
62    agent: agent
63  });
64
65  req.end();
66
67  req.on('socket', function(sock) {
68    if (clientSocket) {
69      assert.strictEqual(sock, clientSocket);
70    } else {
71      clientSocket = sock;
72    }
73  });
74
75  req.on('response', function(res) {
76    let data = '';
77    res.setEncoding('utf8');
78    res.on('data', function(c) {
79      data += c;
80    });
81    res.on('end', function() {
82      assert.strictEqual(data, `/${n}`);
83      setTimeout(function() {
84        actualRequests++;
85        makeRequest(n - 1);
86      }, 1);
87    });
88  });
89}
90
91process.on('exit', function() {
92  assert.strictEqual(actualRequests, expectRequests);
93  console.log('ok');
94});
95