Skip to content
Myr

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

3 files+134 added−0 removed

modified:lib/response.js+29 added
635 unmodified lines
636 return this;637};638639/**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 */653654res.append = function append(field, val) {655 var prev = this.get(field);656 var value = val;657658 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 }664665 return this.set(field, value);666};667668/**669 * Set header `field` to `val`, or pass670 * an object of header fields.671 *672 * Examples:
352 unmodified lines
added:test/res.append.js+104 added
12var express = require('..')3var request = require('supertest')4var should = require('should')56describe('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()1415 app.use(function (req, res, next) {16 res.append('Link', '<http://localhost/>')17 next()18 })1920 app.use(function (req, res) {21 res.append('Link', '<http://localhost:80/>')22 res.end()23 })2425 request(app)26 .get('/')27 .expect('Link', '<http://localhost/>, <http://localhost:80/>', done)28 })2930 it('should accept array of values', function (done) {31 var app = express()3233 app.use(function (req, res, next) {34 res.append('Set-Cookie', ['foo=bar', 'fizz=buzz'])35 res.end()36 })3738 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 })4546 it('should get reset by res.set(field, val)', function (done) {47 var app = express()4849 app.use(function (req, res, next) {50 res.append('Link', '<http://localhost/>')51 res.append('Link', '<http://localhost:80/>')52 next()53 })5455 app.use(function (req, res) {56 res.set('Link', '<http://127.0.0.1/>')57 res.end()58 });5960 request(app)61 .get('/')62 .expect('Link', '<http://127.0.0.1/>', done)63 })6465 it('should work with res.set(field, val) first', function (done) {66 var app = express()6768 app.use(function (req, res, next) {69 res.set('Link', '<http://localhost/>')70 next()71 })7273 app.use(function(req, res){74 res.append('Link', '<http://localhost:80/>')75 res.end()76 })7778 request(app)79 .get('/')80 .expect('Link', '<http://localhost/>, <http://localhost:80/>', done)81 })8283 it('should work with cookies', function (done) {84 var app = express()8586 app.use(function (req, res, next) {87 res.cookie('foo', 'bar')88 next()89 })9091 app.use(function (req, res) {92 res.append('Set-Cookie', 'bar=baz')93 res.end()94 })9596 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
1unreleased2==========34 * 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 properly
2552 unmodified lines