forked from sequoiar/bouncy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunked.js
83 lines (72 loc) · 2.41 KB
/
chunked.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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('4\r\nabcd\r\n') },
function () { c.write('5\r\nefghi\r\n') },
function () { c.write('7\r\njklmnop\r\n') },
function () { c.write('0\r\n'); 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\r',
'Content-Type: text/plain\r',
'Transfer-Encoding: chunked\r',
'Connection: close\r',
'\r',
'4\r',
'abcd\r',
'5\r',
'efghi\r',
'7\r',
'jklmnop\r',
'0\r'
]);
t.end();
s0.close();
s1.close();
});
});
}
});