http: Only send connection:keep-alive if necessary
[platform/upstream/nodejs.git] / test / simple / test-http-should-keep-alive.js
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 var common = require('../common');
23 var assert = require('assert');
24 var http = require('http');
25 var net = require('net');
26
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'
34 ];
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
42 ];
43 var requests = 0;
44 var responses = 0;
45 http.globalAgent.maxSockets = 5;
46
47 var server = net.createServer(function(socket) {
48   socket.write(SERVER_RESPONSES[requests]);
49   ++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 ') +
56                    'Keep-Alive');
57       ++responses;
58       if (responses < SHOULD_KEEP_ALIVE.length) {
59         makeRequest();
60       } else {
61         server.close();
62       }
63       res.resume();
64     });
65   }
66
67   makeRequest();
68 });
69
70 process.on('exit', function() {
71   assert.equal(requests, SERVER_RESPONSES.length);
72   assert.equal(responses, SHOULD_KEEP_ALIVE.length);
73 });