Skip to content

Commit

Permalink
Support unspecified vary
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 4, 2014
1 parent 7516ff0 commit d2922d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,20 @@ function vary(res, header) {
? val.join(', ')
: String(val);

// existing unspecified vary
if (headers === '*') {
return;
}

// enumerate current values
var vals = headers.toLowerCase().split(/ *, */);

// unspecified vary
if (header === '*' || vals.indexOf('*') !== -1) {
res.setHeader('Vary', '*');
return;
}

if (vals.indexOf(header.toLowerCase()) !== -1) {
// already set
return;
Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ describe('vary(res, header)', function () {
res.getHeader('Vary').should.equal('Accept, Accept-Encoding, origin');
});
});

describe('when Vary: *', function () {
it('should set value', function () {
var res = createRes();
vary(res, '*');
res.getHeader('Vary').should.equal('*');
});

it('should act as if all values alread set', function () {
var res = createRes({'vary': '*'});
vary(res, 'Origin');
vary(res, 'User-Agent');
res.getHeader('Vary').should.equal('*');
});

it('should erradicate existing values', function () {
var res = createRes({'vary': 'Accept, Accept-Encoding'});
vary(res, '*');
res.getHeader('Vary').should.equal('*');
});

it('should update bad existing header', function () {
var res = createRes({'vary': 'Accept, Accept-Encoding, *'});
vary(res, 'Origin');
res.getHeader('Vary').should.equal('*');
});
});
});

function createRes(headers) {
Expand Down

0 comments on commit d2922d3

Please sign in to comment.