1 var common = require('../common');
2 var assert = require('assert');
3 var http = require('http');
4 var net = require('net');
6 // Check that our HTTP server correctly handles HTTP/1.0 keep-alive requests.
8 name: 'keep-alive, no TE header',
11 data: 'POST / HTTP/1.0\r\n' +
12 'Connection: keep-alive\r\n' +
16 data: 'POST / HTTP/1.0\r\n' +
17 'Connection: keep-alive\r\n' +
21 headers: {'Connection': 'keep-alive'},
27 name: 'keep-alive, with TE: chunked',
30 data: 'POST / HTTP/1.0\r\n' +
31 'Connection: keep-alive\r\n' +
36 data: 'POST / HTTP/1.0\r\n' +
40 headers: {'Connection': 'keep-alive'},
46 name: 'keep-alive, with Transfer-Encoding: chunked',
49 data: 'POST / HTTP/1.0\r\n' +
50 'Connection: keep-alive\r\n' +
54 data: 'POST / HTTP/1.0\r\n' +
58 headers: {'Connection': 'keep-alive',
59 'Transfer-Encoding': 'chunked'},
65 name: 'keep-alive, with Content-Length',
68 data: 'POST / HTTP/1.0\r\n' +
69 'Connection: keep-alive\r\n' +
73 data: 'POST / HTTP/1.0\r\n' +
77 headers: {'Connection': 'keep-alive',
78 'Content-Length': '2'},
85 function check(tests) {
87 if (test) http.createServer(server).listen(common.PORT, '127.0.0.1', client);
91 check(tests.slice(1));
94 function server(req, res) {
95 if (current + 1 === test.responses.length) this.close();
96 var ctx = test.responses[current];
97 console.error('< SERVER SENDING RESPONSE', ctx);
98 res.writeHead(200, ctx.headers);
99 ctx.chunks.slice(0, -1).forEach(function(chunk) { res.write(chunk) });
100 res.end(ctx.chunks[ctx.chunks.length - 1]);
104 if (current === test.requests.length) return next();
105 var conn = net.createConnection(common.PORT, '127.0.0.1', connected);
107 function connected() {
108 var ctx = test.requests[current];
109 console.error(' > CLIENT SENDING REQUEST', ctx);
110 conn.setEncoding('utf8');
111 conn.write(ctx.data);
114 console.error(' > CLIENT CLOSE');
115 if (!ctx.expectClose) throw new Error('unexpected close');
118 conn.on('close', onclose);
121 console.error(' > CLIENT ONDATA %j %j', s.length, s.toString());
123 if (ctx.expectClose) return;
124 conn.removeListener('close', onclose);
125 conn.removeListener('data', ondata);;
128 conn.on('data', ondata);