From ad39b37974b9be5a3046fa6a5bdf9c616e0a9a1d Mon Sep 17 00:00:00 2001 From: "pooja d.p" Date: Mon, 2 Nov 2020 18:52:51 +0530 Subject: [PATCH] http: enable call chaining with setHeader() Make `response.setHeader` return the response object itself so that multiple header setting can be chained. Fixes: https://github.com/nodejs/node/issues/33148 PR-URL: https://github.com/nodejs/node/pull/35924 Reviewed-By: Luigi Pinca Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Rich Trott --- doc/api/http.md | 6 ++++- lib/_http_outgoing.js | 1 + test/parallel/test-http-set-header-chain.js | 29 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-set-header-chain.js diff --git a/doc/api/http.md b/doc/api/http.md index a17c7246c52c3e..f816dad0790536 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1587,13 +1587,17 @@ added: v0.4.0 * `name` {string} * `value` {any} +* Returns: {http.ServerResponse} + +Returns the response object. Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here to send multiple headers with the same name. Non-string values will be stored without modification. Therefore, [`response.getHeader()`][] may return non-string values. However, the non-string values will be converted to strings -for network transmission. +for network transmission. The same response object is returned to the caller, +to enable call chaining. ```js response.setHeader('Content-Type', 'text/html'); diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index c700bdd82cf57a..e570f128348a36 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -565,6 +565,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) { this[kOutHeaders] = headers = ObjectCreate(null); headers[name.toLowerCase()] = [name, value]; + return this; }; diff --git a/test/parallel/test-http-set-header-chain.js b/test/parallel/test-http-set-header-chain.js new file mode 100644 index 00000000000000..aa9519129a9123 --- /dev/null +++ b/test/parallel/test-http-set-header-chain.js @@ -0,0 +1,29 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); +const assert = require('assert'); +const expected = { + '__proto__': null, + 'testheader1': 'foo', + 'testheader2': 'bar', + 'testheader3': 'xyz' +}; +const server = http.createServer(common.mustCall((req, res) => { + let retval = res.setHeader('testheader1', 'foo'); + + // Test that the setHeader returns the same response object. + assert.strictEqual(retval, res); + + retval = res.setHeader('testheader2', 'bar').setHeader('testheader3', 'xyz'); + // Test that chaining works for setHeader. + assert.deepStrictEqual(res.getHeaders(), expected); + res.end('ok'); +})); +server.listen(0, () => { + http.get({ port: server.address().port }, common.mustCall((res) => { + res.on('data', () => {}); + res.on('end', common.mustCall(() => { + server.close(); + })); + })); +});