Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 4, 2014
0 parents commit 7516ff0
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
node_modules/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
test/
.travis.yml
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.11"
matrix:
allow_failures:
- node_js: "0.11"
fast_finish: true
script: "npm run-script test-travis"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
unreleased
==========

* Initial release
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2014 Douglas Christopher Wilson

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# vary

[![NPM version](https://badge.fury.io/js/vary.svg)](http://badge.fury.io/js/vary)
[![Build Status](https://travis-ci.org/expressjs/vary.svg?branch=master)](https://travis-ci.org/expressjs/vary)
[![Coverage Status](https://img.shields.io/coveralls/expressjs/vary.svg?branch=master)](https://coveralls.io/r/expressjs/vary)

Update the Vary header of a response

## Install

```sh
$ npm install vary
```

## API

```js
var vary = require('vary')
```

### vary(res, header)

Adds the given `header` to the `Vary` response header of `res`.

This will append the header if not already listed, otherwise leaves
it listed in the current location.

```js
vary(res, 'Origin')
vary(res, 'User-Agent')
```

## Testing

```sh
$ npm test
```

## License

[MIT](LICENSE)
60 changes: 60 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*!
* vary
* Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/

/**
* Module exports.
*/

module.exports = vary;

/**
* Variables.
*/

var separators = /[\(\)<>@,;:\\"\/\[\]\?=\{\}\u0020\u0009]/;

/**
* Mark that a request is varied on a header.
*
* @param {Object} res
* @param {String} header
* @api public
*/

function vary(res, header) {
if (!res || !res.getHeader || !res.setHeader) {
// quack quack
throw new TypeError('res argument is required');
}

if (!header) {
throw new TypeError('header argument is required');
}

if (separators.test(header)) {
throw new TypeError('header argument is not a valid header');
}

var val = res.getHeader('Vary') || ''
var headers = Array.isArray(val)
? val.join(', ')
: String(val);

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

if (vals.indexOf(header.toLowerCase()) !== -1) {
// already set
return;
}

// append value (case-preserving)
val = headers
? headers + ', ' + header
: header;

res.setHeader('Vary', val);
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "vary",
"description": "Update the Vary header of a response",
"version": "0.0.0",
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>",
"license": "MIT",
"keywords": [
"http",
"res",
"vary"
],
"repository": "expressjs/vary",
"devDependencies": {
"istanbul": "0.2.10",
"mocha": "~1.20.0",
"should": "~4.0.0"
},
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"test": "mocha --reporter dot test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec test/"
}
}
118 changes: 118 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

var vary = require('..');
var should = require('should');

describe('vary(res, header)', function () {
describe('arguments', function () {
describe('res', function () {
it('should be required', function () {
vary.bind().should.throw(/res.*required/);
});

it('should not allow non-res-like objects', function () {
vary.bind(null, {}).should.throw(/res.*required/);
});
});

describe('header', function () {
it('should be required', function () {
var res = createRes();
vary.bind(null, res).should.throw(/header.*required/);
});

it('should not allow separators', function () {
var res = createRes();
vary.bind(null, res, 'invalid:header').should.throw(/header.*not.*valid/);
vary.bind(null, res, 'invalid header').should.throw(/header.*not.*valid/);
});
});
});

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

it('should set value with multiple calls', function () {
var res = createRes();
vary(res, 'Origin');
vary(res, 'User-Agent');
res.getHeader('Vary').should.equal('Origin, User-Agent');
});

it('should preserve case', function () {
var res = createRes();
vary(res, 'ORIGIN');
vary(res, 'user-agent');
vary(res, 'AccepT');
res.getHeader('Vary').should.equal('ORIGIN, user-agent, AccepT');
});
});

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

it('should set value with multiple calls', function () {
var res = createRes({'vary': 'Accept'});
vary(res, 'Origin');
vary(res, 'User-Agent');
res.getHeader('Vary').should.equal('Accept, Origin, User-Agent');
});

it('should not duplicate existing value', function () {
var res = createRes({'vary': 'Accept'});
vary(res, 'Accept');
res.getHeader('Vary').should.equal('Accept');
});

it('should compare case-insensitive', function () {
var res = createRes({'vary': 'Accept'});
vary(res, 'accEPT');
res.getHeader('Vary').should.equal('Accept');
});

it('should preserve case', function () {
var res = createRes({'vary': 'Accept'});
vary(res, 'AccepT');
res.getHeader('Vary').should.equal('Accept');
});
});

describe('when existing Vary as array', function () {
it('should set value', function () {
var res = createRes({'vary': ['Accept', 'Accept-Encoding']});
vary(res, 'Origin');
res.getHeader('Vary').should.equal('Accept, Accept-Encoding, Origin');
});

it('should not duplicate existing value', function () {
var res = createRes({'vary': ['Accept', 'Accept-Encoding']});
vary(res, 'accept');
vary(res, 'origin');
res.getHeader('Vary').should.equal('Accept, Accept-Encoding, origin');
});
});
});

function createRes(headers) {
var _headers = {};

for (var key in headers) {
_headers[key.toLowerCase()] = headers[key];
}

return {
getHeader: function (name) {
return _headers[name.toLowerCase()];
},
setHeader: function (name, val) {
_headers[name.toLowerCase()] = val;
}
};
}

0 comments on commit 7516ff0

Please sign in to comment.