Skip to content

Commit f6f78e5

Browse files
committedJan 14, 2015
Add res.append(field, val) to append headers
closes expressjs#2455
1 parent 591e89e commit f6f78e5

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
 

‎History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Add `res.append(field, val)` to append headers
45
* Deprecate leading `:` in `name` for `app.param(name, fn)`
56
* Deprecate `req.param()` -- use `req.params`, `req.body`, or `req.query` instead
67
* Deprecate `app.param(fn)`

‎lib/response.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,35 @@ res.attachment = function attachment(filename) {
636636
return this;
637637
};
638638

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} field
649+
* @param {String|Array} val
650+
* @return {ServerResponse} for chaining
651+
* @api public
652+
*/
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 vals
660+
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+
639668
/**
640669
* Set header `field` to `val`, or pass
641670
* an object of header fields.

‎test/res.append.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 because
8+
// the common node.js versions white list which _incoming_
9+
// headers can appear multiple times; there is no such white list
10+
// for outgoing, though
11+
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+
})

0 commit comments

Comments
 (0)