Add res.append(field, val) to append headers
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #2455
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 591e89e · Browse files · 3 files·+134 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+134 added−0 removed
- tests:+104 added−0 removed
- other:+30 added−0 removed
Provenance
- Commit
f6f78e5- Tree
e76d023- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
591e89e
3 files changed+134 added−0 removed
modified:lib/response.js+29 added
635 unmodified lines636 return this;637 };638 639 /**640+ * Append additional header `field` with value `val`.641+ *642+ * Example:643+ *644+ * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);645+ * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');646+ * res.append('Warning', '199 Miscellaneous warning');647+ *648+ * @param {String} field649+ * @param {String|Array} val650+ * @return {ServerResponse} for chaining651+ * @api public652+ */653+654+res.append = function append(field, val) {655+ var prev = this.get(field);656+ var value = val;657+658+ if (prev) {659+ // concat the new and prev vals660+ value = Array.isArray(prev) ? prev.concat(val)661+ : Array.isArray(val) ? [prev].concat(val)662+ : [prev, val];663+ }664+665+ return this.set(field, value);666+};667+668+/**669 * Set header `field` to `val`, or pass670 * an object of header fields.671 *672 * Examples:352 unmodified linesadded:test/res.append.js+104 added
1+2+var express = require('..')3+var request = require('supertest')4+var should = require('should')5+6+describe('res', function () {7+ // note about these tests: "Link" and "X-*" are chosen because8+ // the common node.js versions white list which _incoming_9+ // headers can appear multiple times; there is no such white list10+ // for outgoing, though11+ describe('.append(field, val)', function () {12+ it('should append multiple headers', function (done) {13+ var app = express()14+15+ app.use(function (req, res, next) {16+ res.append('Link', '<http://localhost/>')17+ next()18+ })19+20+ app.use(function (req, res) {21+ res.append('Link', '<http://localhost:80/>')22+ res.end()23+ })24+25+ request(app)26+ .get('/')27+ .expect('Link', '<http://localhost/>, <http://localhost:80/>', done)28+ })29+30+ it('should accept array of values', function (done) {31+ var app = express()32+33+ app.use(function (req, res, next) {34+ res.append('Set-Cookie', ['foo=bar', 'fizz=buzz'])35+ res.end()36+ })37+38+ request(app)39+ .get('/')40+ .expect(function (res) {41+ should(res.headers['set-cookie']).eql(['foo=bar', 'fizz=buzz'])42+ })43+ .expect(200, done)44+ })45+46+ it('should get reset by res.set(field, val)', function (done) {47+ var app = express()48+49+ app.use(function (req, res, next) {50+ res.append('Link', '<http://localhost/>')51+ res.append('Link', '<http://localhost:80/>')52+ next()53+ })54+55+ app.use(function (req, res) {56+ res.set('Link', '<http://127.0.0.1/>')57+ res.end()58+ });59+60+ request(app)61+ .get('/')62+ .expect('Link', '<http://127.0.0.1/>', done)63+ })64+65+ it('should work with res.set(field, val) first', function (done) {66+ var app = express()67+68+ app.use(function (req, res, next) {69+ res.set('Link', '<http://localhost/>')70+ next()71+ })72+73+ app.use(function(req, res){74+ res.append('Link', '<http://localhost:80/>')75+ res.end()76+ })77+78+ request(app)79+ .get('/')80+ .expect('Link', '<http://localhost/>, <http://localhost:80/>', done)81+ })82+83+ it('should work with cookies', function (done) {84+ var app = express()85+86+ app.use(function (req, res, next) {87+ res.cookie('foo', 'bar')88+ next()89+ })90+91+ app.use(function (req, res) {92+ res.append('Set-Cookie', 'bar=baz')93+ res.end()94+ })95+96+ request(app)97+ .get('/')98+ .expect(function (res) {99+ should(res.headers['set-cookie']).eql(['foo=bar; Path=/', 'bar=baz'])100+ })101+ .expect(200, done)102+ })103+ })104+})modified:History.md+1 added
1 unreleased2 ==========3 4+ * Add `res.append(field, val)` to append headers5 * Deprecate leading `:` in `name` for `app.param(name, fn)`6 * Deprecate `req.param()` -- use `req.params`, `req.body`, or `req.query` instead7 * Deprecate `app.param(fn)`8 * Fix `OPTIONS` responses to include the `HEAD` method properly2552 unmodified lines