|
| 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 | + |
| 46 | +var server = net.createServer(function(socket) { |
| 47 | + socket.write(SERVER_RESPONSES[requests]); |
| 48 | + ++requests; |
| 49 | +}).listen(common.PORT, function() { |
| 50 | + function makeRequest() { |
| 51 | + var req = http.get({port: common.PORT}, function(res) { |
| 52 | + assert.equal(req.shouldKeepAlive, SHOULD_KEEP_ALIVE[responses], |
| 53 | + SERVER_RESPONSES[responses] + ' should ' + |
| 54 | + (SHOULD_KEEP_ALIVE[responses] ? '' : 'not ') + |
| 55 | + 'Keep-Alive'); |
| 56 | + ++responses; |
| 57 | + if (responses < SHOULD_KEEP_ALIVE.length) { |
| 58 | + makeRequest(); |
| 59 | + } else { |
| 60 | + server.close(); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + makeRequest(); |
| 66 | +}); |
| 67 | + |
| 68 | +process.on('exit', function() { |
| 69 | + assert.equal(requests, SERVER_RESPONSES.length); |
| 70 | + assert.equal(responses, SHOULD_KEEP_ALIVE.length); |
| 71 | +}); |
0 commit comments