diff --git a/lib/_http_server.js b/lib/_http_server.js index 4cb0c4b2f3b99f..eab7d64ff48169 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -187,9 +187,6 @@ ServerResponse.prototype.writeHead = function(statusCode) { var obj = arguments[headerIndex]; if (obj && this._headers) { - // Slow-case: when progressive API and header fields are passed. - headers = this._renderHeaders(); - if (util.isArray(obj)) { // handle array case // TODO: remove when array is no longer accepted @@ -207,8 +204,10 @@ ServerResponse.prototype.writeHead = function(statusCode) { var keys = Object.keys(obj); for (var i = 0; i < keys.length; i++) { var k = keys[i]; - if (k) headers[k] = obj[k]; + if (k) this.setHeader(k, obj[k]); } + // Slow-case: when progressive API and header fields are passed. + headers = this._renderHeaders(); } } else if (this._headers) { // only progressive api is used diff --git a/test/simple/test-http-write-head.js b/test/simple/test-http-write-head.js new file mode 100644 index 00000000000000..2de59fe2edaf7d --- /dev/null +++ b/test/simple/test-http-write-head.js @@ -0,0 +1,46 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var http = require('http'); + +// Verify that ServerResponse.writeHead() works as setHeader. +// Issue 5036 on github. + +var s = http.createServer(function(req, res) { + res.setHeader('test', '1'); + res.writeHead(200, { Test: '2' }); + res.end(); +}); + +s.listen(common.PORT, runTest); + +function runTest() { + http.get({ port: common.PORT }, function(response) { + response.on('end', function() { + assert.equal(response.headers['test'], '2'); + assert(response.rawHeaders.indexOf('Test') !== -1); + s.close(); + }); + response.resume(); + }); +}