Permalink
Browse files

more comprehensive keep-alive test

  • Loading branch information...
1 parent ff56f05 commit 834cc93220c9d20a696c76287336b3f330471bd6 @substack committed Oct 12, 2011
Showing with 139 additions and 2 deletions.
  1. +3 −2 test/chunked.js
  2. +136 −0 test/keep_alive_leak.js
View
@@ -33,7 +33,7 @@ test('chunked transfers should be transparent', function (t) {
function () { c.write('4\r\nabcd\r\n') },
function () { c.write('5\r\nefghi\r\n') },
function () { c.write('7\r\njklmnop\r\n') },
- function () { c.end() },
+ function () { c.write('0\r\n'); c.end() },
];
var iv = setInterval(function () {
@@ -70,7 +70,8 @@ test('chunked transfers should be transparent', function (t) {
'5\r',
'efghi\r',
'7\r',
- 'jklmnop\r'
+ 'jklmnop\r',
+ '0\r'
]);
t.end();
View
@@ -0,0 +1,136 @@
+var test = require('tap').test;
+var bouncy = require('../');
+var http = require('http');
+var net = require('net');
+var lazy = require('lazy');
+
+test("make sure keep-alives don't leak", function (t) {
+ var p0 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
+ var p1 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
+
+ //t.plan(3);
+ var s0 = bouncy(function (req, bounce) {
+ t.equal(req.headers.host, 'beep.boop');
+ bounce(p1);
+ });
+
+ s0.listen(p0, function () {
+ var times = 0;
+ request(p0, t, function redo (send) {
+ if (times++ < 3) send(redo.bind(null, send))
+ else {
+ send(null);
+ t.end();
+ }
+ });
+ });
+
+ var s1 = http.createServer(function (req, res) {
+ t.equal(req.method, 'POST');
+ t.equal(req.headers.host, 'beep.boop');
+ t.equal(req.headers.connection, 'keep-alive');
+ t.equal(req.headers['transfer-encoding'], 'chunked');
+
+ var data = '';
+ req.on('data', function (buf) {
+ data += buf.toString();
+ });
+
+ req.on('end', function () {
+ t.deepEqual(data, [ 'abcdefghij' ]);
+
+ res.setHeader('content-type', 'text/plain');
+ res.setHeader('connection', 'keep-alive');
+
+ setTimeout(function () {
+ res.write('oh');
+ }, 10);
+
+ setTimeout(function () {
+ res.write(' hello\r\n');
+ res.end();
+ }, 20);
+ });
+ });
+ s1.listen(p1);
+});
+
+function request (port, t, cb) {
+ var c = net.createConnection(port, function () {
+ cb(send);
+ });
+
+ function send (fn) {
+ if (!fn) { c.end(); return }
+
+console.log('send()');
+ c.write([
+ 'POST / HTTP/1.1',
+ 'Host: beep.boop',
+ 'Connection: keep-alive',
+ 'Transfer-Encoding: chunked',
+ '',
+ '',
+ ].join('\r\n'));
+
+ var chunks = [
+ '3\r\nabc\r\n',
+ '2\r\nde\r\n',
+ '5\r\nfghij\r\n',
+ '0\r\n\r\n',
+ ];
+
+ var finished = false;
+ var iv = setInterval(function () {
+ var chunk = chunks.shift();
+ if (chunk) c.write(chunk)
+ else {
+ clearInterval(iv);
+ finished = true;
+ }
+ }, 50);
+
+ var lines = [''];
+ var mode = 'header';
+
+ c.on('data', function onData (buf) {
+ for (var i = 0; i < buf.length; i++) {
+ if (buf[i] !== '\n'.charCodeAt(0)) {
+ lines[lines.length-1] += String.fromCharCode(buf[i])
+ continue;
+ }
+
+ lines.push('');
+ if (mode === 'header' && lines[lines.length-2] === '\r') {
+ mode = 'body';
+ }
+ else if (mode === 'body' && lines[lines.length-2] === '0\r') {
+ c.removeListener('data', onData);
+
+ function upcase (s) { return s.toUpperCase() }
+
+ t.ok(finished);
+ t.deepEqual(lines.slice(0,4).map(upcase).sort(), [
+ 'HTTP/1.1 200 OK\r',
+ 'Content-Type: text/plain\r',
+ 'Transfer-Encoding: chunked\r',
+ 'Connection: keep-alive\r',
+ ].sort().map(upcase));
+
+ t.deepEqual(lines.slice(4), [
+ '\r',
+ '2\r',
+ 'oh\r',
+ '7\r',
+ 'hello\r',
+ '\r',
+ '0\r'
+ ]);
+
+ fn();
+ }
+ }
+ });
+
+ }
+}

0 comments on commit 834cc93

Please sign in to comment.