Remove excessive copyright/license boilerplate
[platform/upstream/nodejs.git] / test / parallel / test-http-1.0-keep-alive.js
1 var common = require('../common');
2 var assert = require('assert');
3 var http = require('http');
4 var net = require('net');
5
6 // Check that our HTTP server correctly handles HTTP/1.0 keep-alive requests.
7 check([{
8   name: 'keep-alive, no TE header',
9   requests: [{
10     expectClose: true,
11     data: 'POST / HTTP/1.0\r\n' +
12           'Connection: keep-alive\r\n' +
13           '\r\n'
14   }, {
15     expectClose: true,
16     data: 'POST / HTTP/1.0\r\n' +
17           'Connection: keep-alive\r\n' +
18           '\r\n'
19   }],
20   responses: [{
21     headers: {'Connection': 'keep-alive'},
22     chunks: ['OK']
23   }, {
24     chunks: []
25   }]
26 }, {
27   name: 'keep-alive, with TE: chunked',
28   requests: [{
29     expectClose: false,
30     data: 'POST / HTTP/1.0\r\n' +
31           'Connection: keep-alive\r\n' +
32           'TE: chunked\r\n' +
33           '\r\n'
34   }, {
35     expectClose: true,
36     data: 'POST / HTTP/1.0\r\n' +
37           '\r\n'
38   }],
39   responses: [{
40     headers: {'Connection': 'keep-alive'},
41     chunks: ['OK']
42   }, {
43     chunks: []
44   }]
45 }, {
46   name: 'keep-alive, with Transfer-Encoding: chunked',
47   requests: [{
48     expectClose: false,
49     data: 'POST / HTTP/1.0\r\n' +
50           'Connection: keep-alive\r\n' +
51           '\r\n'
52   }, {
53     expectClose: true,
54     data: 'POST / HTTP/1.0\r\n' +
55           '\r\n'
56   }],
57   responses: [{
58     headers: {'Connection': 'keep-alive',
59               'Transfer-Encoding': 'chunked'},
60     chunks: ['OK']
61   }, {
62     chunks: []
63   }]
64 }, {
65   name: 'keep-alive, with Content-Length',
66   requests: [{
67     expectClose: false,
68     data: 'POST / HTTP/1.0\r\n' +
69           'Connection: keep-alive\r\n' +
70           '\r\n'
71   }, {
72     expectClose: true,
73     data: 'POST / HTTP/1.0\r\n' +
74           '\r\n'
75   }],
76   responses: [{
77     headers: {'Connection': 'keep-alive',
78               'Content-Length': '2'},
79     chunks: ['OK']
80   }, {
81     chunks: []
82   }]
83 }]);
84
85 function check(tests) {
86   var test = tests[0];
87   if (test) http.createServer(server).listen(common.PORT, '127.0.0.1', client);
88   var current = 0;
89
90   function next() {
91     check(tests.slice(1));
92   }
93
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]);
101   }
102
103   function client() {
104     if (current === test.requests.length) return next();
105     var conn = net.createConnection(common.PORT, '127.0.0.1', connected);
106
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);
112
113       function onclose() {
114         console.error(' > CLIENT CLOSE');
115         if (!ctx.expectClose) throw new Error('unexpected close');
116         client();
117       }
118       conn.on('close', onclose);
119
120       function ondata(s) {
121         console.error(' > CLIENT ONDATA %j %j', s.length, s.toString());
122         current++;
123         if (ctx.expectClose) return;
124         conn.removeListener('close', onclose);
125         conn.removeListener('data', ondata);;
126         connected();
127       }
128       conn.on('data', ondata);
129     }
130   }
131 }