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// Verify that ServerResponse.writeHead() works as setHeader. 28// Issue 5036 on github. 29 30const s = http.createServer(common.mustCall((req, res) => { 31 res.setHeader('test', '1'); 32 33 // toLowerCase() is used on the name argument, so it must be a string. 34 // Non-String header names should throw 35 assert.throws( 36 () => res.setHeader(0xf00, 'bar'), 37 { 38 code: 'ERR_INVALID_HTTP_TOKEN', 39 name: 'TypeError', 40 message: 'Header name must be a valid HTTP token ["3840"]' 41 } 42 ); 43 44 // Undefined value should throw, via 979d0ca8 45 assert.throws( 46 () => res.setHeader('foo', undefined), 47 { 48 code: 'ERR_HTTP_INVALID_HEADER_VALUE', 49 name: 'TypeError', 50 message: 'Invalid value "undefined" for header "foo"' 51 } 52 ); 53 54 res.writeHead(200, { Test: '2' }); 55 56 assert.throws(() => { 57 res.writeHead(100, {}); 58 }, { 59 code: 'ERR_HTTP_HEADERS_SENT', 60 name: 'Error', 61 message: 'Cannot render headers after they are sent to the client' 62 }); 63 64 res.end(); 65})); 66 67s.listen(0, common.mustCall(runTest)); 68 69function runTest() { 70 http.get({ port: this.address().port }, common.mustCall((response) => { 71 response.on('end', common.mustCall(() => { 72 assert.strictEqual(response.headers.test, '2'); 73 assert(response.rawHeaders.includes('Test')); 74 s.close(); 75 })); 76 response.resume(); 77 })); 78} 79