[Pkg-javascript-commits] [node-vary] 02/08: Imported Upstream version 1.0.0
Leo Iannacone
l3on-guest at moszumanska.debian.org
Sat Oct 11 12:53:09 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-vary.
commit 2ef057b430dbb8174e0bcb4536e04a9676d57485
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Sat Oct 11 14:34:19 2014 +0200
Imported Upstream version 1.0.0
---
.travis.yml | 2 +-
History.md | 7 ++
README.md | 30 +++--
index.js | 75 +++++++----
package.json | 19 +--
test/test.js | 406 ++++++++++++++++++++++++++++++++++++++++++++---------------
6 files changed, 402 insertions(+), 137 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 1ff243c..bdd4e73 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,4 +8,4 @@ matrix:
- node_js: "0.11"
fast_finish: true
script: "npm run-script test-travis"
-after_script: "npm install coveralls at 2.10.0 && cat ./coverage/lcov.info | coveralls"
+after_script: "test $TRAVIS_NODE_VERSION = '0.10' && npm install coveralls at 2.11.1 && cat ./coverage/lcov.info | coveralls"
diff --git a/History.md b/History.md
index 10fb1db..e5d8e69 100644
--- a/History.md
+++ b/History.md
@@ -1,3 +1,10 @@
+1.0.0 / 2014-08-10
+==================
+
+ * Accept valid `Vary` header string as `field`
+ * Add `vary.append` for low-level string manipulation
+ * Move to `jshttp` orgainzation
+
0.1.0 / 2014-06-05
==================
diff --git a/README.md b/README.md
index 6ed2825..82392d0 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,12 @@
# 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)
+[![NPM Version](http://img.shields.io/npm/v/vary.svg?style=flat)](https://www.npmjs.org/package/vary)
+[![Node.js Version](http://img.shields.io/badge/node.js->=_0.8-blue.svg?style=flat)](http://nodejs.org/download/)
+[![Build Status](http://img.shields.io/travis/jshttp/vary.svg?style=flat)](https://travis-ci.org/jshttp/vary)
+[![Coverage Status](https://img.shields.io/coveralls/jshttp/vary.svg?style=flat)](https://coveralls.io/r/jshttp/vary)
+[![Gittip](http://img.shields.io/gittip/dougwilson.svg?style=flat)](https://www.gittip.com/dougwilson/)
-Update the Vary header of a response
+Manipulate the HTTP Vary header
## Install
@@ -21,15 +23,29 @@ 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.
```js
+// Append "Origin" to the Vary header of the response
vary(res, 'Origin')
-vary(res, 'User-Agent')
-vary(res, ['Accept', 'Accept-Language', 'Accept-Encoding'])
+```
+
+### 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, 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.
+
+```js
+// Get header string appending "Origin" to "Accept, User-Agent"
+vary.append('Accept, User-Agent', 'Origin')
```
## Testing
diff --git a/index.js b/index.js
index 6f52730..1e544e8 100644
--- a/index.js
+++ b/index.js
@@ -9,6 +9,7 @@
*/
module.exports = vary;
+module.exports.append = append;
/**
* Variables.
@@ -17,50 +18,46 @@ module.exports = vary;
var separators = /[\(\)<>@,;:\\"\/\[\]\?=\{\}\u0020\u0009]/;
/**
- * Mark that a request is varied on a header field.
+ * Append a field to a vary header.
*
- * @param {Object} res
+ * @param {String} header
* @param {String|Array} field
+ * @return {String}
* @api public
*/
-function vary(res, field) {
- if (!res || !res.getHeader || !res.setHeader) {
- // quack quack
- throw new TypeError('res argument is required');
+function append(header, field) {
+ if (typeof header !== 'string') {
+ throw new TypeError('header argument is required');
}
if (!field) {
throw new TypeError('field argument is required');
}
+ // get fields array
var fields = !Array.isArray(field)
- ? [String(field)]
+ ? parse(String(field))
: field;
+ // assert on invalid fields
for (var i = 0; i < fields.length; i++) {
if (separators.test(fields[i])) {
throw new TypeError('field argument contains an invalid header');
}
}
- var val = res.getHeader('Vary') || ''
- var headers = Array.isArray(val)
- ? val.join(', ')
- : String(val);
-
- // existing unspecified vary
- if (headers === '*') {
- return;
+ // existing, unspecified vary
+ if (header === '*') {
+ return header;
}
// enumerate current values
- var vals = headers.toLowerCase().split(/ *, */);
+ var vals = parse(header.toLowerCase());
// unspecified vary
if (fields.indexOf('*') !== -1 || vals.indexOf('*') !== -1) {
- res.setHeader('Vary', '*');
- return;
+ return '*';
}
for (var i = 0; i < fields.length; i++) {
@@ -69,11 +66,47 @@ function vary(res, field) {
// append value (case-preserving)
if (vals.indexOf(field) === -1) {
vals.push(field);
- headers = headers
- ? headers + ', ' + fields[i]
+ header = header
+ ? header + ', ' + fields[i]
: fields[i];
}
}
- res.setHeader('Vary', headers);
+ 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.
+ *
+ * @param {Object} res
+ * @param {String|Array} field
+ * @api public
+ */
+
+function vary(res, field) {
+ if (!res || !res.getHeader || !res.setHeader) {
+ // quack quack
+ throw new TypeError('res argument is required');
+ }
+
+ // get existing header
+ var val = res.getHeader('Vary') || ''
+ var header = Array.isArray(val)
+ ? val.join(', ')
+ : String(val);
+
+ // set new header
+ res.setHeader('Vary', append(header, field));
}
diff --git a/package.json b/package.json
index ad09ef1..6398439 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "vary",
- "description": "Update the Vary header of a response",
- "version": "0.1.0",
+ "description": "Manipulate the HTTP Vary header",
+ "version": "1.0.0",
"author": "Douglas Christopher Wilson <doug at somethingdoug.com>",
"license": "MIT",
"keywords": [
@@ -9,18 +9,19 @@
"res",
"vary"
],
- "repository": "expressjs/vary",
+ "repository": "jshttp/vary",
"devDependencies": {
- "istanbul": "0.2.10",
- "mocha": "~1.20.0",
- "should": "~4.0.0"
+ "istanbul": "0.3.0",
+ "mocha": "~1.21.4",
+ "should": "~4.0.4",
+ "supertest": "~0.13.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/"
+ "test": "mocha --reporter spec --bail --check-leaks test/",
+ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
+ "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
}
}
diff --git a/test/test.js b/test/test.js
index c2cb383..00563c3 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,8 +1,10 @@
-var vary = require('..');
+var http = require('http');
+var request = require('supertest');
var should = require('should');
+var vary = require('..');
-describe('vary(res, header)', function () {
+describe('vary(res, field)', function () {
describe('arguments', function () {
describe('res', function () {
it('should be required', function () {
@@ -15,174 +17,380 @@ describe('vary(res, header)', function () {
});
describe('field', function () {
+ it('should be required', function (done) {
+ request(createServer(callVary()))
+ .get('/')
+ .expect(500, /field.*required/, done);
+ });
+
+ it('should accept string', function (done) {
+ request(createServer(callVary('foo')))
+ .get('/')
+ .expect(200, done);
+ });
+
+ it('should accept array of string', function (done) {
+ request(createServer(callVary(['foo', 'bar'])))
+ .get('/')
+ .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('/')
+ .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);
+ });
+ });
+ });
+
+ describe('when no Vary', function () {
+ it('should set value', function (done) {
+ request(createServer(callVary('Origin')))
+ .get('/')
+ .expect('Vary', 'Origin')
+ .expect(200, done);
+ });
+
+ it('should set value with multiple calls', function (done) {
+ request(createServer(callVary(['Origin', 'User-Agent'])))
+ .get('/')
+ .expect('Vary', 'Origin, User-Agent')
+ .expect(200, done);
+ });
+
+ it('should preserve case', function (done) {
+ request(createServer(callVary(['ORIGIN', 'user-agent', 'AccepT'])))
+ .get('/')
+ .expect('Vary', 'ORIGIN, user-agent, AccepT')
+ .expect(200, done);
+ });
+ });
+
+ describe('when existing Vary', function () {
+ it('should set value', function (done) {
+ request(createServer(alterVary('Accept', 'Origin')))
+ .get('/')
+ .expect('Vary', 'Accept, Origin')
+ .expect(200, done);
+ });
+
+ it('should set value with multiple calls', function (done) {
+ var server = createServer(function (req, res) {
+ res.setHeader('Vary', 'Accept');
+ vary(res, 'Origin');
+ vary(res, 'User-Agent');
+ });
+ request(server)
+ .get('/')
+ .expect('Vary', 'Accept, Origin, User-Agent')
+ .expect(200, done);
+ });
+
+ it('should not duplicate existing value', function (done) {
+ request(createServer(alterVary('Accept', 'Accept')))
+ .get('/')
+ .expect('Vary', 'Accept')
+ .expect(200, done);
+ });
+
+ it('should compare case-insensitive', function (done) {
+ request(createServer(alterVary('Accept', 'accEPT')))
+ .get('/')
+ .expect('Vary', 'Accept')
+ .expect(200, done);
+ });
+
+ it('should preserve case', function (done) {
+ request(createServer(alterVary('AccepT', ['accEPT', 'ORIGIN'])))
+ .get('/')
+ .expect('Vary', 'AccepT, ORIGIN')
+ .expect(200, done);
+ });
+ });
+
+ describe('when existing Vary as array', function () {
+ it('should set value', function (done) {
+ request(createServer(alterVary(['Accept', 'Accept-Encoding'], 'Origin')))
+ .get('/')
+ .expect('Vary', 'Accept, Accept-Encoding, Origin')
+ .expect(200, done);
+ });
+
+ it('should not duplicate existing value', function (done) {
+ request(createServer(alterVary(['Accept', 'Accept-Encoding'], ['accept', 'origin'])))
+ .get('/')
+ .expect('Vary', 'Accept, Accept-Encoding, origin')
+ .expect(200, done);
+ });
+ });
+
+ describe('when Vary: *', function () {
+ it('should set value', function (done) {
+ request(createServer(callVary('*')))
+ .get('/')
+ .expect('Vary', '*')
+ .expect(200, done);
+ });
+
+ it('should act as if all values alread set', function (done) {
+ request(createServer(alterVary('*', ['Origin', 'User-Agent'])))
+ .get('/')
+ .expect('Vary', '*')
+ .expect(200, done);
+ });
+
+ it('should erradicate existing values', function (done) {
+ request(createServer(alterVary('Accept, Accept-Encoding', '*')))
+ .get('/')
+ .expect('Vary', '*')
+ .expect(200, done);
+ });
+
+ it('should update bad existing header', function (done) {
+ request(createServer(alterVary('Accept, Accept-Encoding, *', 'Origin')))
+ .get('/')
+ .expect('Vary', '*')
+ .expect(200, done);
+ });
+ });
+
+ 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('/')
+ .expect('Vary', 'Accept, Accept-Language')
+ .expect(200, done);
+ });
+
+ it('should ignore double-entries', function (done) {
+ request(createServer(callVary(['Accept', 'Accept'])))
+ .get('/')
+ .expect('Vary', 'Accept')
+ .expect(200, done);
+ });
+
+ it('should be case-insensitive', function (done) {
+ request(createServer(callVary(['Accept', 'ACCEPT'])))
+ .get('/')
+ .expect('Vary', 'Accept')
+ .expect(200, done);
+ });
+
+ it('should handle contained *', function (done) {
+ request(createServer(callVary(['Origin', 'User-Agent', '*', 'Accept'])))
+ .get('/')
+ .expect('Vary', '*')
+ .expect(200, done);
+ });
+
+ it('should handle existing values', function (done) {
+ request(createServer(alterVary('Accept, Accept-Encoding', ['origin', 'accept', 'accept-charset'])))
+ .get('/')
+ .expect('Vary', 'Accept, Accept-Encoding, origin, accept-charset')
+ .expect(200, done);
+ });
+ });
+});
+
+describe('vary.append(header, field)', function () {
+ describe('arguments', function () {
+ describe('header', function () {
+ it('should be required', function () {
+ vary.append.bind().should.throw(/header.*required/);
+ });
+
+ it('should be a string', function () {
+ vary.append.bind(null, 42).should.throw(/header.*required/);
+ });
+ });
+
+ describe('field', function () {
it('should be required', function () {
- var res = createRes();
- vary.bind(null, res).should.throw(/field.*required/);
+ vary.append.bind(null, '').should.throw(/field.*required/);
});
it('should accept string', function () {
- var res = createRes();
- vary.bind(null, res, 'foo').should.not.throw();
+ 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 () {
- var res = createRes();
- vary.bind(null, res, ['foo', 'bar']).should.not.throw();
+ vary.append.bind(null, '', ['foo', 'bar']).should.not.throw();
});
- it('should not allow separators', function () {
- var res = createRes();
- vary.bind(null, res, 'invalid:header').should.throw(/field.*contains.*invalid/);
- vary.bind(null, res, 'invalid header').should.throw(/field.*contains.*invalid/);
- vary.bind(null, res, ['invalid header']).should.throw(/field.*contains.*invalid/);
+ 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/);
});
});
});
- describe('when no Vary', function () {
+ describe('when header empty', function () {
it('should set value', function () {
- var res = createRes();
- vary(res, 'Origin');
- res.getHeader('Vary').should.equal('Origin');
+ vary.append('', 'Origin').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 set value with array', function () {
+ vary.append('', ['Origin', 'User-Agent']).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');
+ vary.append('', ['ORIGIN', 'user-agent', 'AccepT']).should.equal('ORIGIN, user-agent, AccepT');
});
});
- describe('when existing Vary', function () {
+ describe('when header has values', function () {
it('should set value', function () {
- var res = createRes({'vary': 'Accept'});
- vary(res, 'Origin');
- res.getHeader('Vary').should.equal('Accept, Origin');
+ vary.append('Accept', 'Origin').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 set value with array', function () {
+ vary.append('Accept', ['Origin', 'User-Agent']).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');
+ vary.append('Accept', 'Accept').should.equal('Accept');
});
it('should compare case-insensitive', function () {
- var res = createRes({'vary': 'Accept'});
- vary(res, 'accEPT');
- res.getHeader('Vary').should.equal('Accept');
+ vary.append('Accept', 'accEPT').should.equal('Accept');
});
it('should preserve case', function () {
- var res = createRes({'vary': 'Accept'});
- vary(res, 'AccepT');
- res.getHeader('Vary').should.equal('Accept');
+ vary.append('Accept', 'AccepT').should.equal('Accept');
});
});
- describe('when existing Vary as array', function () {
+ describe('when *', 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');
+ vary.append('', '*').should.equal('*');
});
- 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');
+ it('should act as if all values already set', function () {
+ vary.append('*', 'Origin').should.equal('*');
+ });
+
+ it('should erradicate existing values', function () {
+ vary.append('Accept, Accept-Encoding', '*').should.equal('*');
+ });
+
+ it('should update bad existing header', function () {
+ vary.append('Accept, Accept-Encoding, *', 'Origin').should.equal('*');
});
});
- describe('when Vary: *', function () {
+ describe('when field is string', function () {
it('should set value', function () {
- var res = createRes();
- vary(res, '*');
- res.getHeader('Vary').should.equal('*');
+ vary.append('', 'Accept').should.equal('Accept');
});
- 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 set value when vary header', function () {
+ vary.append('', 'Accept, Accept-Encoding').should.equal('Accept, Accept-Encoding');
});
- it('should erradicate existing values', function () {
- var res = createRes({'vary': 'Accept, Accept-Encoding'});
- vary(res, '*');
- res.getHeader('Vary').should.equal('*');
+ it('should acept LWS', function () {
+ vary.append('', ' Accept , Origin ').should.equal('Accept, Origin');
});
- it('should update bad existing header', function () {
- var res = createRes({'vary': 'Accept, Accept-Encoding, *'});
- vary(res, 'Origin');
- res.getHeader('Vary').should.equal('*');
+ it('should handle contained *', function () {
+ vary.append('', 'Accept,*').should.equal('*');
});
});
- describe('when fields is array', function () {
+ describe('when field is array', function () {
it('should set value', function () {
- var res = createRes();
- vary(res, ['Accept', 'Accept-Language']);
- res.getHeader('Vary').should.equal('Accept, Accept-Language');
+ vary.append('', ['Accept', 'Accept-Language']).should.equal('Accept, Accept-Language');
});
it('should ignore double-entries', function () {
- var res = createRes();
- vary(res, ['Accept', 'Accept']);
- res.getHeader('Vary').should.equal('Accept');
+ vary.append('', ['Accept', 'Accept']).should.equal('Accept');
});
it('should be case-insensitive', function () {
- var res = createRes();
- vary(res, ['Accept', 'ACCEPT']);
- res.getHeader('Vary').should.equal('Accept');
+ vary.append('', ['Accept', 'ACCEPT']).should.equal('Accept');
});
it('should handle contained *', function () {
- var res = createRes();
- vary(res, ['Origin', 'User-Agent', '*', 'Accept']);
- res.getHeader('Vary').should.equal('*');
+ vary.append('', ['Origin', 'User-Agent', '*', 'Accept']).should.equal('*');
});
it('should handle existing values', function () {
- var res = createRes({'vary': 'Accept, Accept-Encoding'});
- vary(res, ['origin', 'accept', 'accept-charset']);
- res.getHeader('Vary').should.equal('Accept, Accept-Encoding, origin, accept-charset');
+ vary.append('Accept, Accept-Encoding', ['origin', 'accept', 'accept-charset']).should.equal('Accept, Accept-Encoding, origin, accept-charset');
});
});
});
-function createRes(headers) {
- var _headers = {};
+function alterVary(header, field) {
+ return function call(req, res) {
+ res.setHeader('Vary', header);
+ vary(res, field);
+ };
+}
- for (var key in headers) {
- _headers[key.toLowerCase()] = headers[key];
- }
+function callVary(field) {
+ return function call(req, res) {
+ vary(res, field);
+ };
+}
- return {
- getHeader: function (name) {
- return _headers[name.toLowerCase()];
- },
- setHeader: function (name, val) {
- _headers[name.toLowerCase()] = val;
+function createServer(fn) {
+ return http.createServer(function onRequest(req, res) {
+ try {
+ fn(req, res);
+ res.statusCode = 200;
+ } catch (err) {
+ res.statusCode = 500;
+ res.write(err.message);
+ } finally {
+ res.end();
}
- };
+ });
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-vary.git
More information about the Pkg-javascript-commits
mailing list