1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
23 var assert = require('assert');
24 var http = require('http');
25 var net = require('net');
27 var SERVER_RESPONSES = [
28 'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n',
29 'HTTP/1.0 200 ok\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n',
30 'HTTP/1.0 200 ok\r\nContent-Length: 0\r\nConnection: close\r\n\r\n',
31 'HTTP/1.1 200 ok\r\nContent-Length: 0\r\n\r\n',
32 'HTTP/1.1 200 ok\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n',
33 'HTTP/1.1 200 ok\r\nContent-Length: 0\r\nConnection: close\r\n\r\n'
35 var SHOULD_KEEP_ALIVE = [
36 false, // HTTP/1.0, default
37 true, // HTTP/1.0, Connection: keep-alive
38 false, // HTTP/1.0, Connection: close
39 true, // HTTP/1.1, default
40 true, // HTTP/1.1, Connection: keep-alive
41 false // HTTP/1.1, Connection: close
45 http.globalAgent.maxSockets = 5;
47 var server = net.createServer(function(socket) {
48 socket.write(SERVER_RESPONSES[requests]);
50 }).listen(common.PORT, function() {
51 function makeRequest() {
52 var req = http.get({port: common.PORT}, function(res) {
53 assert.equal(req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses],
54 SERVER_RESPONSES[responses] + ' should ' +
55 (SHOULD_KEEP_ALIVE[responses] ? '' : 'not ') +
58 if (responses < SHOULD_KEEP_ALIVE.length) {
70 process.on('exit', function() {
71 assert.equal(requests, SERVER_RESPONSES.length);
72 assert.equal(responses, SHOULD_KEEP_ALIVE.length);