Skip to content

Commit

Permalink
Accept valid Vary header string as field
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Aug 10, 2014
1 parent 4c5a06f commit 1470cb1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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.
*
Expand Down
72 changes: 60 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand All @@ -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);
});
});
});

Expand Down Expand Up @@ -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('/')
Expand Down Expand Up @@ -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();
});
Expand All @@ -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/);
});
});
});

Expand Down Expand Up @@ -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');
});
Expand Down

0 comments on commit 1470cb1

Please sign in to comment.