http: Only send connection:keep-alive if necessary
[platform/upstream/nodejs.git] / test / simple / test-http-raw-headers.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var common = require('../common');
23 var assert = require('assert');
24
25 var http = require('http');
26
27 http.createServer(function(req, res) {
28   this.close();
29   var expectRawHeaders = [
30     'Host',
31     'localhost:' + common.PORT,
32     'transfer-ENCODING',
33     'CHUNKED',
34     'x-BaR',
35     'yoyoyo',
36     'Connection',
37     'close'
38   ];
39   var expectHeaders = {
40     host: 'localhost:' + common.PORT,
41     'transfer-encoding': 'CHUNKED',
42     'x-bar': 'yoyoyo',
43     connection: 'close'
44   };
45
46   var expectRawTrailers = [
47     'x-bAr',
48     'yOyOyOy',
49     'x-baR',
50     'OyOyOyO',
51     'X-bAr',
52     'yOyOyOy',
53     'X-baR',
54     'OyOyOyO'
55   ];
56
57   var expectTrailers = { 'x-bar': 'yOyOyOy, OyOyOyO, yOyOyOy, OyOyOyO' };
58
59   assert.deepEqual(req.rawHeaders, expectRawHeaders);
60   assert.deepEqual(req.headers, expectHeaders);
61
62   req.on('end', function() {
63     assert.deepEqual(req.rawTrailers, expectRawTrailers);
64     assert.deepEqual(req.trailers, expectTrailers);
65   });
66
67   req.resume();
68   res.addTrailers([
69     ['x-fOo', 'xOxOxOx'],
70     ['x-foO', 'OxOxOxO'],
71     ['X-fOo', 'xOxOxOx'],
72     ['X-foO', 'OxOxOxO']
73   ]);
74   res.end('x f o o');
75 }).listen(common.PORT, function() {
76   var expectRawHeaders = [
77     'Date',
78     'Tue, 06 Aug 2013 01:31:54 GMT',
79     'Connection',
80     'close',
81     'Transfer-Encoding',
82     'chunked'
83   ];
84   var req = http.request({ port: common.PORT, path: '/' });
85   req.addTrailers([
86     ['x-bAr', 'yOyOyOy'],
87     ['x-baR', 'OyOyOyO'],
88     ['X-bAr', 'yOyOyOy'],
89     ['X-baR', 'OyOyOyO']
90   ]);
91   req.setHeader('transfer-ENCODING', 'CHUNKED');
92   req.setHeader('x-BaR', 'yoyoyo');
93   req.end('y b a r');
94   req.on('response', function(res) {
95     var expectRawHeaders = [
96       'Date',
97       null,
98       'Connection',
99       'close',
100       'Transfer-Encoding',
101       'chunked'
102     ];
103     var expectHeaders = {
104       date: null,
105       connection: 'close',
106       'transfer-encoding': 'chunked'
107     };
108     res.rawHeaders[1] = null;
109     res.headers.date = null;
110     assert.deepEqual(res.rawHeaders, expectRawHeaders);
111     assert.deepEqual(res.headers, expectHeaders);
112     res.on('end', function() {
113       var expectRawTrailers = [
114         'x-fOo',
115         'xOxOxOx',
116         'x-foO',
117         'OxOxOxO',
118         'X-fOo',
119         'xOxOxOx',
120         'X-foO',
121         'OxOxOxO'
122       ];
123       var expectTrailers = { 'x-foo': 'xOxOxOx, OxOxOxO, xOxOxOx, OxOxOxO' };
124
125       assert.deepEqual(res.rawTrailers, expectRawTrailers);
126       assert.deepEqual(res.trailers, expectTrailers);
127       console.log('ok');
128     });
129     res.resume();
130   });
131 });