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'; 23const common = require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26 27// Simple test of Node's HTTP Client mutable headers 28// OutgoingMessage.prototype.setHeader(name, value) 29// OutgoingMessage.prototype.getHeader(name) 30// OutgoingMessage.prototype.removeHeader(name, value) 31// ServerResponse.prototype.statusCode 32// <ClientRequest>.method 33// <ClientRequest>.path 34 35let test = 'headers'; 36const content = 'hello world\n'; 37const cookies = [ 38 'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT', 39 'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT', 40]; 41 42const s = http.createServer(common.mustCall((req, res) => { 43 switch (test) { 44 case 'headers': { 45 // Check that header-related functions work before setting any headers 46 const headers = res.getHeaders(); 47 const exoticObj = Object.create(null); 48 assert.deepStrictEqual(headers, exoticObj); 49 assert.deepStrictEqual(res.getHeaderNames(), []); 50 assert.deepStrictEqual(res.getRawHeaderNames(), []); 51 assert.strictEqual(res.hasHeader('Connection'), false); 52 assert.strictEqual(res.getHeader('Connection'), undefined); 53 54 assert.throws( 55 () => res.setHeader(), 56 { 57 code: 'ERR_INVALID_HTTP_TOKEN', 58 name: 'TypeError', 59 message: 'Header name must be a valid HTTP token ["undefined"]' 60 } 61 ); 62 assert.throws( 63 () => res.setHeader('someHeader'), 64 { 65 code: 'ERR_HTTP_INVALID_HEADER_VALUE', 66 name: 'TypeError', 67 message: 'Invalid value "undefined" for header "someHeader"' 68 } 69 ); 70 assert.throws( 71 () => res.getHeader(), 72 { 73 code: 'ERR_INVALID_ARG_TYPE', 74 name: 'TypeError', 75 message: 'The "name" argument must be of type string. ' + 76 'Received undefined' 77 } 78 ); 79 assert.throws( 80 () => res.removeHeader(), 81 { 82 code: 'ERR_INVALID_ARG_TYPE', 83 name: 'TypeError', 84 message: 'The "name" argument must be of type string. ' + 85 'Received undefined' 86 } 87 ); 88 89 const arrayValues = [1, 2, 3]; 90 res.setHeader('x-test-header', 'testing'); 91 res.setHeader('X-TEST-HEADER2', 'testing'); 92 res.setHeader('set-cookie', cookies); 93 res.setHeader('x-test-array-header', arrayValues); 94 95 assert.strictEqual(res.getHeader('x-test-header'), 'testing'); 96 assert.strictEqual(res.getHeader('x-test-header2'), 'testing'); 97 98 const headersCopy = res.getHeaders(); 99 const expected = { 100 'x-test-header': 'testing', 101 'x-test-header2': 'testing', 102 'set-cookie': cookies, 103 'x-test-array-header': arrayValues 104 }; 105 Object.setPrototypeOf(expected, null); 106 assert.deepStrictEqual(headersCopy, expected); 107 108 assert.deepStrictEqual(res.getHeaderNames(), 109 ['x-test-header', 'x-test-header2', 110 'set-cookie', 'x-test-array-header']); 111 112 assert.deepStrictEqual(res.getRawHeaderNames(), 113 ['x-test-header', 'X-TEST-HEADER2', 114 'set-cookie', 'x-test-array-header']); 115 116 assert.strictEqual(res.hasHeader('x-test-header2'), true); 117 assert.strictEqual(res.hasHeader('X-TEST-HEADER2'), true); 118 assert.strictEqual(res.hasHeader('X-Test-Header2'), true); 119 [ 120 undefined, 121 null, 122 true, 123 {}, 124 { toString: () => 'X-TEST-HEADER2' }, 125 () => { }, 126 ].forEach((val) => { 127 assert.throws( 128 () => res.hasHeader(val), 129 { 130 code: 'ERR_INVALID_ARG_TYPE', 131 name: 'TypeError', 132 message: 'The "name" argument must be of type string.' + 133 common.invalidArgTypeHelper(val) 134 } 135 ); 136 }); 137 138 res.removeHeader('x-test-header2'); 139 140 assert.strictEqual(res.hasHeader('x-test-header2'), false); 141 assert.strictEqual(res.hasHeader('X-TEST-HEADER2'), false); 142 assert.strictEqual(res.hasHeader('X-Test-Header2'), false); 143 break; 144 } 145 case 'contentLength': 146 res.setHeader('content-length', content.length); 147 assert.strictEqual(res.getHeader('Content-Length'), content.length); 148 break; 149 150 case 'transferEncoding': 151 res.setHeader('transfer-encoding', 'chunked'); 152 assert.strictEqual(res.getHeader('Transfer-Encoding'), 'chunked'); 153 break; 154 155 case 'writeHead': 156 res.statusCode = 404; 157 res.setHeader('x-foo', 'keyboard cat'); 158 res.writeHead(200, { 'x-foo': 'bar', 'x-bar': 'baz' }); 159 break; 160 161 default: 162 assert.fail('Unknown test'); 163 } 164 165 res.statusCode = 201; 166 res.end(content); 167}, 4)); 168 169s.listen(0, nextTest); 170 171 172function nextTest() { 173 if (test === 'end') { 174 return s.close(); 175 } 176 177 let bufferedResponse = ''; 178 179 const req = http.get({ 180 port: s.address().port, 181 headers: { 'X-foo': 'bar' } 182 }, common.mustCall((response) => { 183 switch (test) { 184 case 'headers': 185 assert.strictEqual(response.statusCode, 201); 186 assert.strictEqual(response.headers['x-test-header'], 'testing'); 187 assert.strictEqual(response.headers['x-test-array-header'], 188 [1, 2, 3].join(', ')); 189 assert.deepStrictEqual(cookies, response.headers['set-cookie']); 190 assert.strictEqual(response.headers['x-test-header2'], undefined); 191 test = 'contentLength'; 192 break; 193 194 case 'contentLength': 195 assert.strictEqual(+response.headers['content-length'], content.length); 196 test = 'transferEncoding'; 197 break; 198 199 case 'transferEncoding': 200 assert.strictEqual(response.headers['transfer-encoding'], 'chunked'); 201 test = 'writeHead'; 202 break; 203 204 case 'writeHead': 205 assert.strictEqual(response.headers['x-foo'], 'bar'); 206 assert.strictEqual(response.headers['x-bar'], 'baz'); 207 assert.strictEqual(response.statusCode, 200); 208 test = 'end'; 209 break; 210 211 default: 212 assert.fail('Unknown test'); 213 } 214 215 response.setEncoding('utf8'); 216 response.on('data', (s) => { 217 bufferedResponse += s; 218 }); 219 220 response.on('end', common.mustCall(() => { 221 assert.strictEqual(bufferedResponse, content); 222 common.mustCall(nextTest)(); 223 })); 224 })); 225 226 assert.deepStrictEqual(req.getHeaderNames(), 227 ['x-foo', 'host']); 228 229 assert.deepStrictEqual(req.getRawHeaderNames(), 230 ['X-foo', 'Host']); 231} 232