Skip to content

Commit

Permalink
Add res.append(field, val) to append headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jan 25, 2015
1 parent ceacafe commit 5a3f32d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ koa uses [http-assert](https://github.com/jshttp/http-assert) for assertions.
- `ctx.redirect()`
- `ctx.attachment()`
- `ctx.set()`
- `ctx.append()`
- `ctx.remove()`
- `ctx.lastModified=`
- `ctx.etag=`
7 changes: 7 additions & 0 deletions docs/api/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ var etag = this.get('ETag');
this.set('Cache-Control', 'no-cache');
```

### response.append(field, value)
Append additional header `field` with value `val`.

```js
this.append('Link', '<https://127.0.0.1/>');
```

### response.set(fields)

Set several response header `fields` with an object:
Expand Down
1 change: 1 addition & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ delegate(proto, 'response')
.method('remove')
.method('vary')
.method('set')
.method('append')
.access('status')
.access('message')
.access('body')
Expand Down
27 changes: 27 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@ module.exports = {
}
},

/**
* Append additional header `field` with value `val`.
*
* Examples:
*
* this.append('Link', ['<https://localhost/>', '<https://localhost:3000/>']);
* this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
* this.append('Warning', '199 Miscellaneous warning');
*
* @param {String} field
* @param {String|Array} val
* @api public
*/

append: function(field, val){
var prev = this.get(field);
var value = val;
if (prev) {
// concat the new and prev vals
value = Array.isArray(prev) ? prev.concat(val)
: Array.isArray(val) ? [prev].concat(val)
: [prev, val];
}

return this.set(field, value);
},

/**
* Remove header `field`.
*
Expand Down
38 changes: 38 additions & 0 deletions test/response/append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

var context = require('../context');

describe('ctx.append(name, val)', function(){
it('should append multiple headers', function(){
var ctx = context();
ctx.append('x-foo', 'bar1');
ctx.append('x-foo', 'bar2');
ctx.response.header['x-foo'].should.eql(['bar1', 'bar2']);
})

it('should accept array of values', function (){
var ctx = context();

ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']);
ctx.response.header['set-cookie'].should.eql(['foo=bar', 'fizz=buzz']);
})

it('should get reset by res.set(field, val)', function (){
var ctx = context();

ctx.append('Link', '<https://localhost/>');
ctx.append('Link', '<https://localhost:80/>');

ctx.set('Link', '<https://127.0.0.1/>');

ctx.response.header.link.should.equal('<https://127.0.0.1/>');
})

it('should work with res.set(field, val) first', function (){
var ctx = context();

ctx.set('Link', '<https://localhost/>');
ctx.append('Link', '<https://localhost:80/>');

ctx.response.header.link.should.eql(['<https://localhost/>', '<https://localhost:80/>']);
})
})

0 comments on commit 5a3f32d

Please sign in to comment.