add res.vary(). Closes #1682
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by cjihrig · committed by TJ Holowaychuk · parent e43ff07 · Browse files · 2 files·+94 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+94 added−1 removed
- tests:+55 added−0 removed
- other:+39 added−1 removed
Provenance
- Commit
e5de08f- Tree
2fdf4bb- Author
cjihrig <cjihrig@gmail.com>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
e43ff07
2 files changed+94 added−1 removed
modified:lib/response.js+39 added-1 removed
461 unmodified lines462 var keys = Object.keys(obj);463 464 var key = req.accepts(keys);465 466- this.set('Vary', 'Accept');466+ this.vary("Accept");467 468 if (key) {469 this.set('Content-Type', normalizeType(key).value);470 obj[key](req, this, next);249 unmodified lines720 this.end(head ? null : body);721 };722 723 /**724+ * Add `field` to Vary. If already present in the Vary set, then725+ * this call is simply ignored.726+ *727+ * @param {Array|String} field728+ * @param {ServerResponse} for chaining729+ * @api public730+ */731+732+res.vary = function(field){733+ var self = this;734+735+ // nothing736+ if (!field) return this;737+738+ // array739+ if (Array.isArray(field)) {740+ field.forEach(function(field){741+ self.vary(field);742+ });743+ return;744+ }745+746+ var vary = this.get('Vary');747+748+ // append 749+ if (vary) {750+ vary = vary.split(/ *, */);751+ if (!~vary.indexOf(field)) vary.push(field);752+ this.set('Vary', vary.join(', '));753+ return this;754+ }755+756+ // set757+ this.set('Vary', field);758+ return this;759+};760+761+/**762 * Render `view` with the given `options` and optional callback `fn`.763 * When a callback function is given a response will _not_ be made764 * automatically, otherwise a response of _200_ and _text/html_ is given.765 *34 unmodified linesadded:test/res.vary.js+55 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
var express = require('../') , should = require('should');
function response() { var res = Object.create(express.response); res._headers = {}; return res;}
describe('res.vary()', function(){ describe('with no arguments', function(){ it('should not set Vary', function(){ var res = response(); res.vary(); should.not.exist(res.get('Vary')); }) })
describe('with an empty array', function(){ it('should not set Vary', function(){ var res = response(); res.vary([]); should.not.exist(res.get('Vary')); }) })
describe('with an array', function(){ it('should set the values', function(){ var res = response(); res.vary(['Accept', 'Accept-Language', 'Accept-Encoding']); res.get('Vary').should.equal('Accept, Accept-Language, Accept-Encoding'); }) })
describe('with a string', function(){ it('should set the value', function(){ var res = response(); res.vary('Accept'); res.get('Vary').should.equal('Accept'); }) })
describe('when the value is present', function(){ it('should not add it again', function(){ var res = response(); res.vary('Accept'); res.vary('Accept-Encoding'); res.vary('Accept-Encoding'); res.vary('Accept-Encoding'); res.vary('Accept'); res.get('Vary').should.equal('Accept, Accept-Encoding'); }) })})