Skip to content

Commit

Permalink
a failing test for chunked encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Oct 12, 2011
1 parent d76170d commit dbe4467
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"devDependencies" : {
"tap" : "0.0.x",
"lazy" : "1.0.x",
"websocket-server" : "1.4.x",
"websocket-client" : "1.0.x"
},
Expand Down
82 changes: 82 additions & 0 deletions test/chunked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var test = require('tap').test;
var bouncy = require('../');
var http = require('http');
var net = require('net');
var lazy = require('lazy');

test('chunked transfers should be transparent', 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(2);

var s0 = bouncy(function (req, bounce) {
t.equal(req.headers.host, 'beepity.boop');
bounce(p1);
});

var s1 = net.createServer(function (c) {
lazy(c).lines.map(String).forEach(function (line) {
if (line === '' || line === '\r') {
c.write([
'HTTP/1.1 200 200 OK',
'Content-Type: text/plain',
'Transfer-Encoding: chunked',
'Connection: close',
'',
''
].join('\r\n'));
}
});

var chunks = [
function () { c.write('abcd') },
function () { c.write('efghi') },
function () { c.write('jklmnop') },
function () { c.end() },
];

var iv = setInterval(function () {
var fn = chunks.shift();
if (fn) fn()
else clearInterval(iv)
}, 25);
});

s1.listen(p1, connect);
s0.listen(p0, connect);

var connected = 0;
function connect () {
if (++connected !== 2) return;

var c = net.createConnection(p0, function () {
c.write([
'GET / HTTP/1.1',
'Host: beepity.boop',
'',
''
].join('\r\n'));

lazy(c).lines.map(String).join(function (lines) {
t.deepEqual(lines, [
'HTTP/1.1 200 200 OK',
'Content-Type: text/plain',
'Transfer-Encoding: chunked',
'Connection: close',
'',
'4',
'abcd',
'5',
'efghi',
'7',
'jklmnop'
]);

t.end();
s0.close();
s1.close();
});
});
}
});

0 comments on commit dbe4467

Please sign in to comment.