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'use strict';
23require('../common');
24const assert = require('assert');
25
26const http = require('http');
27
28http.createServer(function(req, res) {
29  const expectRawHeaders = [
30    'Host',
31    `localhost:${this.address().port}`,
32    'transfer-ENCODING',
33    'CHUNKED',
34    'x-BaR',
35    'yoyoyo',
36    'Connection',
37    'close',
38  ];
39  const expectHeaders = {
40    'host': `localhost:${this.address().port}`,
41    'transfer-encoding': 'CHUNKED',
42    'x-bar': 'yoyoyo',
43    'connection': 'close'
44  };
45  const expectRawTrailers = [
46    'x-bAr',
47    'yOyOyOy',
48    'x-baR',
49    'OyOyOyO',
50    'X-bAr',
51    'yOyOyOy',
52    'X-baR',
53    'OyOyOyO',
54  ];
55  const expectTrailers = { 'x-bar': 'yOyOyOy, OyOyOyO, yOyOyOy, OyOyOyO' };
56
57  this.close();
58
59  assert.deepStrictEqual(req.rawHeaders, expectRawHeaders);
60  assert.deepStrictEqual(req.headers, expectHeaders);
61
62  req.on('end', function() {
63    assert.deepStrictEqual(req.rawTrailers, expectRawTrailers);
64    assert.deepStrictEqual(req.trailers, expectTrailers);
65  });
66
67  req.resume();
68  res.setHeader('Trailer', 'x-foo');
69  res.addTrailers([
70    ['x-fOo', 'xOxOxOx'],
71    ['x-foO', 'OxOxOxO'],
72    ['X-fOo', 'xOxOxOx'],
73    ['X-foO', 'OxOxOxO'],
74  ]);
75  res.end('x f o o');
76}).listen(0, function() {
77  const req = http.request({ port: this.address().port, path: '/' });
78  req.addTrailers([
79    ['x-bAr', 'yOyOyOy'],
80    ['x-baR', 'OyOyOyO'],
81    ['X-bAr', 'yOyOyOy'],
82    ['X-baR', 'OyOyOyO'],
83  ]);
84  req.setHeader('transfer-ENCODING', 'CHUNKED');
85  req.setHeader('x-BaR', 'yoyoyo');
86  req.end('y b a r');
87  req.on('response', function(res) {
88    const expectRawHeaders = [
89      'Trailer',
90      'x-foo',
91      'Date',
92      null,
93      'Connection',
94      'close',
95      'Transfer-Encoding',
96      'chunked',
97    ];
98    const expectHeaders = {
99      'trailer': 'x-foo',
100      'date': null,
101      'connection': 'close',
102      'transfer-encoding': 'chunked'
103    };
104    res.rawHeaders[3] = null;
105    res.headers.date = null;
106    assert.deepStrictEqual(res.rawHeaders, expectRawHeaders);
107    assert.deepStrictEqual(res.headers, expectHeaders);
108    res.on('end', function() {
109      const expectRawTrailers = [
110        'x-fOo',
111        'xOxOxOx',
112        'x-foO',
113        'OxOxOxO',
114        'X-fOo',
115        'xOxOxOx',
116        'X-foO',
117        'OxOxOxO',
118      ];
119      const expectTrailers = { 'x-foo': 'xOxOxOx, OxOxOxO, xOxOxOx, OxOxOxO' };
120
121      assert.deepStrictEqual(res.rawTrailers, expectRawTrailers);
122      assert.deepStrictEqual(res.trailers, expectTrailers);
123      console.log('ok');
124    });
125    res.resume();
126  });
127});
128