diff --git a/History.md b/History.md index 099866b..76dbb66 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ unreleased ========== + * Accept valid `Vary` header string as `field` * Add `vary.append` for low-level string manipulation 0.1.0 / 2014-06-05 diff --git a/README.md b/README.md index db8e661..82392d0 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,8 @@ var vary = require('vary') ### vary(res, field) Adds the given header `field` to the `Vary` response header of `res`. -This can be a string of a single field or an array of multiple fields. +This can be a string of a single field, a string of a valid `Vary` +header, or an array of multiple fields. This will append the header if not already listed, otherwise leaves it listed in the current location. @@ -36,7 +37,8 @@ vary(res, 'Origin') ### vary.append(header, field) Adds the given header `field` to the `Vary` response header string `header`. -This can be a string of a single field or an array of multiple fields. +This can be a string of a single field, a string of a valid `Vary` header, +or an array of multiple fields. This will append the header if not already listed, otherwise leaves it listed in the current location. The new header string is returned. diff --git a/index.js b/index.js index 483d325..1e544e8 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ function append(header, field) { // get fields array var fields = !Array.isArray(field) - ? [String(field)] + ? parse(String(field)) : field; // assert on invalid fields @@ -53,7 +53,7 @@ function append(header, field) { } // enumerate current values - var vals = header.toLowerCase().split(/ *, */); + var vals = parse(header.toLowerCase()); // unspecified vary if (fields.indexOf('*') !== -1 || vals.indexOf('*') !== -1) { @@ -75,6 +75,18 @@ function append(header, field) { return header; } +/** + * Parse a vary header into an array. + * + * @param {String} header + * @return {Array} + * @api private + */ + +function parse(header) { + return header.trim().split(/ *, */); +} + /** * Mark that a request is varied on a header field. * diff --git a/test/test.js b/test/test.js index 545201e..00563c3 100644 --- a/test/test.js +++ b/test/test.js @@ -35,6 +35,12 @@ describe('vary(res, field)', function () { .expect(200, done); }); + it('should accept string that is Vary header', function (done) { + request(createServer(callVary('foo, bar'))) + .get('/') + .expect(200, done); + }); + it('should not allow separator ":"', function (done) { request(createServer(callVary('invalid:header'))) .get('/') @@ -46,12 +52,6 @@ describe('vary(res, field)', function () { .get('/') .expect(500, /field.*contains.*invalid/, done); }); - - it('should not allow separator ","', function (done) { - request(createServer(callVary('invalid,header'))) - .get('/') - .expect(500, /field.*contains.*invalid/, done); - }); }); }); @@ -166,7 +166,37 @@ describe('vary(res, field)', function () { }); }); - describe('when fields is array', function () { + describe('when field is string', function () { + it('should set value', function (done) { + request(createServer(callVary('Accept'))) + .get('/') + .expect('Vary', 'Accept') + .expect(200, done); + }); + + it('should set value when vary header', function (done) { + request(createServer(callVary('Accept, Accept-Encoding'))) + .get('/') + .expect('Vary', 'Accept, Accept-Encoding') + .expect(200, done); + }); + + it('should acept LWS', function (done) { + request(createServer(callVary(' Accept , Origin '))) + .get('/') + .expect('Vary', 'Accept, Origin') + .expect(200, done); + }); + + it('should handle contained *', function (done) { + request(createServer(callVary('Accept,*'))) + .get('/') + .expect('Vary', '*') + .expect(200, done); + }); + }); + + describe('when field is array', function () { it('should set value', function (done) { request(createServer(callVary(['Accept', 'Accept-Language']))) .get('/') @@ -225,6 +255,10 @@ describe('vary.append(header, field)', function () { vary.append.bind(null, '', 'foo').should.not.throw(); }); + it('should accept string that is Vary header', function () { + vary.append.bind(null, '', 'foo, bar').should.not.throw(); + }); + it('should accept array of string', function () { vary.append.bind(null, '', ['foo', 'bar']).should.not.throw(); }); @@ -236,10 +270,6 @@ describe('vary.append(header, field)', function () { it('should not allow separator " "', function () { vary.append.bind(null, '', 'invalid header').should.throw(/field.*contains.*invalid/); }); - - it('should not allow separator ","', function () { - vary.append.bind(null, '', 'invalid,header').should.throw(/field.*contains.*invalid/); - }); }); }); @@ -297,7 +327,25 @@ describe('vary.append(header, field)', function () { }); }); - describe('when fields is array', function () { + describe('when field is string', function () { + it('should set value', function () { + vary.append('', 'Accept').should.equal('Accept'); + }); + + it('should set value when vary header', function () { + vary.append('', 'Accept, Accept-Encoding').should.equal('Accept, Accept-Encoding'); + }); + + it('should acept LWS', function () { + vary.append('', ' Accept , Origin ').should.equal('Accept, Origin'); + }); + + it('should handle contained *', function () { + vary.append('', 'Accept,*').should.equal('*'); + }); + }); + + describe('when field is array', function () { it('should set value', function () { vary.append('', ['Accept', 'Accept-Language']).should.equal('Accept, Accept-Language'); });