pax_global_header 0000666 0000000 0000000 00000000064 12412100246 0014502 g ustar 00root root 0000000 0000000 52 comment=4b0bc3f2fec38a839556bd4674f79024929ba256
negotiator-0.4.8/ 0000775 0000000 0000000 00000000000 12412100246 0013666 5 ustar 00root root 0000000 0000000 negotiator-0.4.8/.gitignore 0000664 0000000 0000000 00000000227 12412100246 0015657 0 ustar 00root root 0000000 0000000 # OS X
.DS_Store*
Icon?
._*
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
.directory
*~
# npm
node_modules
*.log
*.gz
# Coveralls
coverage
negotiator-0.4.8/.travis.yml 0000664 0000000 0000000 00000000257 12412100246 0016003 0 ustar 00root root 0000000 0000000 node_js:
- "0.6"
- "0.8"
- "0.10"
- "0.11"
language: node_js
script: "npm run-script test-cov"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
negotiator-0.4.8/LICENSE 0000664 0000000 0000000 00000002141 12412100246 0014671 0 ustar 00root root 0000000 0000000 (The MIT License)
Copyright (c) 2012 Federico Romero
Copyright (c) 2012-2014 Isaac Z. Schlueter
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.
negotiator-0.4.8/README.md 0000664 0000000 0000000 00000007656 12412100246 0015163 0 ustar 00root root 0000000 0000000 # negotiator
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Node.js Version][node-version-image]][node-version-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
An HTTP content negotiator for Node.js
## Installation
```sh
$ npm install negotiator
```
## API
```js
var Negotiator = require('negotiator')
```
### Accept Negotiation
```js
availableMediaTypes = ['text/html', 'text/plain', 'application/json']
// The negotiator constructor receives a request object
negotiator = new Negotiator(request)
// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'
negotiator.mediaTypes()
// -> ['text/html', 'image/jpeg', 'application/*']
negotiator.mediaTypes(availableMediaTypes)
// -> ['text/html', 'application/json']
negotiator.mediaType(availableMediaTypes)
// -> 'text/html'
```
You can check a working example at `examples/accept.js`.
#### Methods
##### mediaTypes(availableMediaTypes):
Returns an array of preferred media types ordered by priority from a list of available media types.
##### mediaType(availableMediaType):
Returns the top preferred media type from a list of available media types.
### Accept-Language Negotiation
```js
negotiator = new Negotiator(request)
availableLanguages = 'en', 'es', 'fr'
// Let's say Accept-Language header is 'en;q=0.8, es, pt'
negotiator.languages()
// -> ['es', 'pt', 'en']
negotiator.languages(availableLanguages)
// -> ['es', 'en']
language = negotiator.language(availableLanguages)
// -> 'es'
```
You can check a working example at `examples/language.js`.
#### Methods
##### languages(availableLanguages):
Returns an array of preferred languages ordered by priority from a list of available languages.
##### language(availableLanguages):
Returns the top preferred language from a list of available languages.
### Accept-Charset Negotiation
```js
availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']
negotiator = new Negotiator(request)
// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'
negotiator.charsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']
negotiator.charsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']
negotiator.charset(availableCharsets)
// -> 'utf-8'
```
You can check a working example at `examples/charset.js`.
#### Methods
##### charsets(availableCharsets):
Returns an array of preferred charsets ordered by priority from a list of available charsets.
##### charset(availableCharsets):
Returns the top preferred charset from a list of available charsets.
### Accept-Encoding Negotiation
```js
availableEncodings = ['identity', 'gzip']
negotiator = new Negotiator(request)
// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
negotiator.encodings()
// -> ['gzip', 'identity', 'compress']
negotiator.encodings(availableEncodings)
// -> ['gzip', 'identity']
negotiator.encoding(availableEncodings)
// -> 'gzip'
```
You can check a working example at `examples/encoding.js`.
#### Methods
##### encodings(availableEncodings):
Returns an array of preferred encodings ordered by priority from a list of available encodings.
##### encoding(availableEncodings):
Returns the top preferred encoding from a list of available encodings.
## License
[MIT](LICENSE)
[npm-image]: https://img.shields.io/npm/v/negotiator.svg?style=flat
[npm-url]: https://npmjs.org/package/negotiator
[node-version-image]: https://img.shields.io/node/v/negotiator.svg?style=flat
[node-version-url]: http://nodejs.org/download/
[travis-image]: https://img.shields.io/travis/jshttp/negotiator.svg?style=flat
[travis-url]: https://travis-ci.org/jshttp/negotiator
[coveralls-image]: https://img.shields.io/coveralls/jshttp/negotiator.svg?style=flat
[coveralls-url]: https://coveralls.io/r/jshttp/negotiator?branch=master
[downloads-image]: https://img.shields.io/npm/dm/negotiator.svg?style=flat
[downloads-url]: https://npmjs.org/package/negotiator
negotiator-0.4.8/examples/ 0000775 0000000 0000000 00000000000 12412100246 0015504 5 ustar 00root root 0000000 0000000 negotiator-0.4.8/examples/accept.js 0000664 0000000 0000000 00000002314 12412100246 0017301 0 ustar 00root root 0000000 0000000 (function() {
var Negotiator, availableMediaTypes, http, key, representations, server, val;
Negotiator = require('../lib/negotiator').Negotiator;
http = require('http');
representations = {
'text/html': '
Hello world!
',
'text/plain': 'Hello World!',
'application/json': JSON.stringify({
hello: 'world!'
})
};
availableMediaTypes = (function() {
var _results;
_results = [];
for (key in representations) {
val = representations[key];
_results.push(key);
}
return _results;
})();
server = http.createServer(function(req, res) {
var mediaType, negotiator;
negotiator = new Negotiator(req);
console.log("Accept: " + req.headers['accept']);
console.log("Preferred: " + (negotiator.mediaTypes()));
console.log("Possible: " + (negotiator.mediaTypes(availableMediaTypes)));
mediaType = negotiator.mediaType(availableMediaTypes);
console.log("Selected: " + mediaType);
if (mediaType) {
res.writeHead(200, {
'Content-Type': mediaType
});
return res.end(representations[mediaType]);
} else {
res.writeHead(406);
return res.end();
}
});
server.listen(8080);
}).call(this);
negotiator-0.4.8/examples/charset.js 0000664 0000000 0000000 00000002430 12412100246 0017472 0 ustar 00root root 0000000 0000000 (function() {
var Buffer, Iconv, Negotiator, availableCharsets, http, iconv, key, message, messages, server, val;
Negotiator = require('../lib/negotiator').Negotiator;
http = require('http');
Buffer = require('buffer').Buffer;
Iconv = require('iconv').Iconv;
iconv = new Iconv('UTF-8', 'ISO-8859-1');
message = "ë";
messages = {
'utf-8': message,
'iso-8859-1': iconv.convert(new Buffer(message))
};
availableCharsets = (function() {
var _results;
_results = [];
for (key in messages) {
val = messages[key];
_results.push(key);
}
return _results;
})();
server = http.createServer(function(req, res) {
var charset, negotiator;
negotiator = new Negotiator(req);
console.log("Accept-Charset: " + req.headers['accept-charset']);
console.log("Preferred: " + (negotiator.charsets()));
console.log("Possible: " + (negotiator.charsets(availableCharsets)));
charset = negotiator.charset(availableCharsets);
console.log("Selected: " + charset);
if (charset) {
res.writeHead(200, {
'Content-Type': "text/html; charset=" + charset
});
return res.end(messages[charset]);
} else {
res.writeHead(406);
return res.end();
}
});
server.listen(8080);
}).call(this);
negotiator-0.4.8/examples/encoding.js 0000664 0000000 0000000 00000002442 12412100246 0017632 0 ustar 00root root 0000000 0000000 (function() {
var Negotiator, gbuf, http, messages;
Negotiator = require('../lib/negotiator').Negotiator;
http = require('http');
gbuf = require('gzip-buffer');
messages = {
identity: 'Hello World'
};
gbuf.gzip(messages.identity, function(zipped) {
var availableEncodings, key, server, val;
messages.gzip = zipped;
availableEncodings = (function() {
var _results;
_results = [];
for (key in messages) {
val = messages[key];
_results.push(key);
}
return _results;
})();
console.log(availableEncodings);
server = http.createServer(function(req, res) {
var encoding, negotiator;
negotiator = new Negotiator(req);
console.log("Accept-Encoding: " + req.headers['accept-encoding']);
console.log("Preferred: " + (negotiator.encodings()));
console.log("Possible: " + (negotiator.encodings(availableEncodings)));
encoding = negotiator.encoding(availableEncodings);
console.log("Selected: " + encoding);
if (encoding) {
res.writeHead(200, {
'Content-Encoding': encoding
});
return res.end(messages[encoding]);
} else {
res.writeHead(406);
return res.end();
}
});
return server.listen(8080);
});
}).call(this);
negotiator-0.4.8/examples/language.js 0000664 0000000 0000000 00000002120 12412100246 0017620 0 ustar 00root root 0000000 0000000 (function() {
var Negotiator, availableLanguages, http, key, messages, server, val;
Negotiator = require('../lib/negotiator').Negotiator;
http = require('http');
messages = {
es: "¡Hola Mundo!",
en: "Hello World!"
};
availableLanguages = (function() {
var _results;
_results = [];
for (key in messages) {
val = messages[key];
_results.push(key);
}
return _results;
})();
server = http.createServer(function(req, res) {
var language, negotiator;
negotiator = new Negotiator(req);
console.log("Accept-Language: " + req.headers['accept-language']);
console.log("Preferred: " + (negotiator.languages()));
console.log("Possible: " + (negotiator.languages(availableLanguages)));
language = negotiator.language(availableLanguages);
console.log("Selected: " + language);
if (language) {
res.writeHead(200, {
'Content-Language': language
});
return res.end(messages[language]);
} else {
res.writeHead(406);
return res.end();
}
});
server.listen(8080);
}).call(this);
negotiator-0.4.8/lib/ 0000775 0000000 0000000 00000000000 12412100246 0014434 5 ustar 00root root 0000000 0000000 negotiator-0.4.8/lib/charset.js 0000664 0000000 0000000 00000003701 12412100246 0016424 0 ustar 00root root 0000000 0000000 module.exports = preferredCharsets;
preferredCharsets.preferredCharsets = preferredCharsets;
function parseAcceptCharset(accept) {
return accept.split(',').map(function(e, i) {
return parseCharset(e.trim(), i);
}).filter(function(e) {
return e;
});
}
function parseCharset(s, i) {
var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
if (!match) return null;
var charset = match[1];
var q = 1;
if (match[2]) {
var params = match[2].split(';')
for (var i = 0; i < params.length; i ++) {
var p = params[i].trim().split('=');
if (p[0] === 'q') {
q = parseFloat(p[1]);
break;
}
}
}
return {
charset: charset,
q: q,
i: i
};
}
function getCharsetPriority(charset, accepted) {
return (accepted.map(function(a) {
return specify(charset, a);
}).filter(Boolean).sort(function (a, b) {
if(a.s == b.s) {
return a.q > b.q ? -1 : 1;
} else {
return a.s > b.s ? -1 : 1;
}
})[0] || {s: 0, q:0});
}
function specify(charset, spec) {
var s = 0;
if(spec.charset.toLowerCase() === charset.toLowerCase()){
s |= 1;
} else if (spec.charset !== '*' ) {
return null
}
return {
s: s,
q: spec.q,
}
}
function preferredCharsets(accept, provided) {
// RFC 2616 sec 14.2: no header = *
accept = parseAcceptCharset(accept === undefined ? '*' : accept || '');
if (provided) {
return provided.map(function(type) {
return [type, getCharsetPriority(type, accept)];
}).filter(function(pair) {
return pair[1].q > 0;
}).sort(function(a, b) {
var pa = a[1];
var pb = b[1];
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
}).map(function(pair) {
return pair[0];
});
} else {
return accept.sort(function (a, b) {
// revsort
return (b.q - a.q) || (a.i - b.i);
}).filter(function(type) {
return type.q > 0;
}).map(function(type) {
return type.charset;
});
}
}
negotiator-0.4.8/lib/encoding.js 0000664 0000000 0000000 00000005100 12412100246 0016554 0 ustar 00root root 0000000 0000000 module.exports = preferredEncodings;
preferredEncodings.preferredEncodings = preferredEncodings;
function parseAcceptEncoding(accept) {
var acceptableEncodings;
if (accept) {
acceptableEncodings = accept.split(',').map(function(e, i) {
return parseEncoding(e.trim(), i);
});
} else {
acceptableEncodings = [];
}
if (!acceptableEncodings.some(function(e) {
return e && specify('identity', e);
})) {
/*
* If identity doesn't explicitly appear in the accept-encoding header,
* it's added to the list of acceptable encoding with the lowest q
*
*/
var lowestQ = 1;
for(var i = 0; i < acceptableEncodings.length; i++){
var e = acceptableEncodings[i];
if(e && e.q < lowestQ){
lowestQ = e.q;
}
}
acceptableEncodings.push({
encoding: 'identity',
q: lowestQ / 2,
});
}
return acceptableEncodings.filter(function(e) {
return e;
});
}
function parseEncoding(s, i) {
var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
if (!match) return null;
var encoding = match[1];
var q = 1;
if (match[2]) {
var params = match[2].split(';');
for (var i = 0; i < params.length; i ++) {
var p = params[i].trim().split('=');
if (p[0] === 'q') {
q = parseFloat(p[1]);
break;
}
}
}
return {
encoding: encoding,
q: q,
i: i
};
}
function getEncodingPriority(encoding, accepted) {
return (accepted.map(function(a) {
return specify(encoding, a);
}).filter(Boolean).sort(function (a, b) {
if(a.s == b.s) {
return a.q > b.q ? -1 : 1;
} else {
return a.s > b.s ? -1 : 1;
}
})[0] || {s: 0, q: 0});
}
function specify(encoding, spec) {
var s = 0;
if(spec.encoding.toLowerCase() === encoding.toLowerCase()){
s |= 1;
} else if (spec.encoding !== '*' ) {
return null
}
return {
s: s,
q: spec.q,
}
};
function preferredEncodings(accept, provided) {
accept = parseAcceptEncoding(accept || '');
if (provided) {
return provided.map(function(type) {
return [type, getEncodingPriority(type, accept)];
}).filter(function(pair) {
return pair[1].q > 0;
}).sort(function(a, b) {
var pa = a[1];
var pb = b[1];
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
}).map(function(pair) {
return pair[0];
});
} else {
return accept.sort(function (a, b) {
// revsort
return (b.q - a.q) || (a.i - b.i);
}).filter(function(type){
return type.q > 0;
}).map(function(type) {
return type.encoding;
});
}
}
negotiator-0.4.8/lib/language.js 0000664 0000000 0000000 00000004413 12412100246 0016557 0 ustar 00root root 0000000 0000000 module.exports = preferredLanguages;
preferredLanguages.preferredLanguages = preferredLanguages;
function parseAcceptLanguage(accept) {
return accept.split(',').map(function(e, i) {
return parseLanguage(e.trim(), i);
}).filter(function(e) {
return e;
});
}
function parseLanguage(s, i) {
var match = s.match(/^\s*(\S+?)(?:-(\S+?))?\s*(?:;(.*))?$/);
if (!match) return null;
var prefix = match[1],
suffix = match[2],
full = prefix;
if (suffix) full += "-" + suffix;
var q = 1;
if (match[3]) {
var params = match[3].split(';')
for (var i = 0; i < params.length; i ++) {
var p = params[i].split('=');
if (p[0] === 'q') q = parseFloat(p[1]);
}
}
return {
prefix: prefix,
suffix: suffix,
q: q,
i: i,
full: full
};
}
function getLanguagePriority(language, accepted) {
return (accepted.map(function(a){
return specify(language, a);
}).filter(Boolean).sort(function (a, b) {
if(a.s == b.s) {
return a.q > b.q ? -1 : 1;
} else {
return a.s > b.s ? -1 : 1;
}
})[0] || {s: 0, q: 0});
}
function specify(language, spec) {
var p = parseLanguage(language)
if (!p) return null;
var s = 0;
if(spec.full.toLowerCase() === p.full.toLowerCase()){
s |= 4;
} else if (spec.prefix.toLowerCase() === p.full.toLowerCase()) {
s |= 2;
} else if (spec.full.toLowerCase() === p.prefix.toLowerCase()) {
s |= 1;
} else if (spec.full !== '*' ) {
return null
}
return {
s: s,
q: spec.q,
}
};
function preferredLanguages(accept, provided) {
// RFC 2616 sec 14.4: no header = *
accept = parseAcceptLanguage(accept === undefined ? '*' : accept || '');
if (provided) {
var ret = provided.map(function(type) {
return [type, getLanguagePriority(type, accept)];
}).filter(function(pair) {
return pair[1].q > 0;
}).sort(function(a, b) {
var pa = a[1];
var pb = b[1];
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
}).map(function(pair) {
return pair[0];
});
return ret;
} else {
return accept.sort(function (a, b) {
// revsort
return (b.q - a.q) || (a.i - b.i);
}).filter(function(type) {
return type.q > 0;
}).map(function(type) {
return type.full;
});
}
}
negotiator-0.4.8/lib/mediaType.js 0000664 0000000 0000000 00000005116 12412100246 0016716 0 ustar 00root root 0000000 0000000 module.exports = preferredMediaTypes;
preferredMediaTypes.preferredMediaTypes = preferredMediaTypes;
function parseAccept(accept) {
return accept.split(',').map(function(e, i) {
return parseMediaType(e.trim(), i);
}).filter(function(e) {
return e;
});
};
function parseMediaType(s, i) {
var match = s.match(/\s*(\S+?)\/([^;\s]+)\s*(?:;(.*))?/);
if (!match) return null;
var type = match[1],
subtype = match[2],
full = "" + type + "/" + subtype,
params = {},
q = 1;
if (match[3]) {
params = match[3].split(';').map(function(s) {
return s.trim().split('=');
}).reduce(function (set, p) {
set[p[0]] = p[1];
return set
}, params);
if (params.q != null) {
q = parseFloat(params.q);
delete params.q;
}
}
return {
type: type,
subtype: subtype,
params: params,
q: q,
i: i,
full: full
};
}
function getMediaTypePriority(type, accepted) {
return (accepted.map(function(a) {
return specify(type, a);
}).filter(Boolean).sort(function (a, b) {
if(a.s == b.s) {
return a.q > b.q ? -1 : 1;
} else {
return a.s > b.s ? -1 : 1;
}
})[0] || {s: 0, q: 0});
}
function specify(type, spec) {
var p = parseMediaType(type);
var s = 0;
if (!p) {
return null;
}
if(spec.type.toLowerCase() == p.type.toLowerCase()) {
s |= 4
} else if(spec.type != '*') {
return null;
}
if(spec.subtype.toLowerCase() == p.subtype.toLowerCase()) {
s |= 2
} else if(spec.subtype != '*') {
return null;
}
var keys = Object.keys(spec.params);
if (keys.length > 0) {
if (keys.every(function (k) {
return spec.params[k] == '*' || spec.params[k].toLowerCase() == (p.params[k] || '').toLowerCase();
})) {
s |= 1
} else {
return null
}
}
return {
q: spec.q,
s: s,
}
}
function preferredMediaTypes(accept, provided) {
// RFC 2616 sec 14.2: no header = */*
accept = parseAccept(accept === undefined ? '*/*' : accept || '');
if (provided) {
return provided.map(function(type) {
return [type, getMediaTypePriority(type, accept)];
}).filter(function(pair) {
return pair[1].q > 0;
}).sort(function(a, b) {
var pa = a[1];
var pb = b[1];
return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
}).map(function(pair) {
return pair[0];
});
} else {
return accept.sort(function (a, b) {
// revsort
return (b.q - a.q) || (a.i - b.i);
}).filter(function(type) {
return type.q > 0;
}).map(function(type) {
return type.full;
});
}
}
negotiator-0.4.8/lib/negotiator.js 0000664 0000000 0000000 00000002065 12412100246 0017150 0 ustar 00root root 0000000 0000000 module.exports = Negotiator;
Negotiator.Negotiator = Negotiator;
function Negotiator(request) {
if (!(this instanceof Negotiator)) return new Negotiator(request);
this.request = request;
}
var set = { charset: 'accept-charset',
encoding: 'accept-encoding',
language: 'accept-language',
mediaType: 'accept' };
function capitalize(string){
return string.charAt(0).toUpperCase() + string.slice(1);
}
Object.keys(set).forEach(function (k) {
var header = set[k],
method = require('./'+k+'.js'),
singular = k,
plural = k + 's';
Negotiator.prototype[plural] = function (available) {
return method(this.request.headers[header], available);
};
Negotiator.prototype[singular] = function(available) {
var set = this[plural](available);
if (set) return set[0];
};
// Keep preferred* methods for legacy compatibility
Negotiator.prototype['preferred'+capitalize(plural)] = Negotiator.prototype[plural];
Negotiator.prototype['preferred'+capitalize(singular)] = Negotiator.prototype[singular];
})
negotiator-0.4.8/package.json 0000664 0000000 0000000 00000001354 12412100246 0016157 0 ustar 00root root 0000000 0000000 {
"name": "negotiator",
"description": "HTTP content negotiation",
"version": "0.4.8",
"author": "Federico Romero ",
"contributors": ["Isaac Z. Schlueter (http://blog.izs.me/)"],
"repository": "jshttp/negotiator",
"keywords": [
"http",
"content negotiation",
"accept",
"accept-language",
"accept-encoding",
"accept-charset"
],
"license": "MIT",
"devDependencies": {
"istanbul": "~0.3.2",
"nodeunit": "0.8.x"
},
"scripts": {
"test": "nodeunit test",
"test-cov": "istanbul cover ./node_modules/nodeunit/bin/nodeunit test"
},
"engines": {
"node": ">= 0.6"
},
"main": "lib/negotiator.js",
"files": [
"lib",
"LICENSE"
]
}
negotiator-0.4.8/test/ 0000775 0000000 0000000 00000000000 12412100246 0014645 5 ustar 00root root 0000000 0000000 negotiator-0.4.8/test/charset.js 0000664 0000000 0000000 00000004635 12412100246 0016644 0 ustar 00root root 0000000 0000000 (function() {
var configuration, preferredCharsets, testConfigurations, testCorrectCharset, _i, _len,
_this = this;
preferredCharsets = require('../lib/charset').preferredCharsets;
this["Should not return a charset when no charset is provided"] = function(test) {
test.deepEqual(preferredCharsets('*', []), []);
return test.done();
};
this["Should not return a charset when no charset is acceptable"] = function(test) {
test.deepEqual(preferredCharsets('ISO-8859-1', ['utf-8']), []);
return test.done();
};
this["Should not return a charset with q = 0"] = function(test) {
test.deepEqual(preferredCharsets('utf-8;q=0', ['utf-8']), []);
return test.done();
};
this["Should be case insensitive"] = function(test) {
test.deepEqual(preferredCharsets('iso-8859-1', ['ISO-8859-1']), ['ISO-8859-1']);
return test.done();
};
testCorrectCharset = function(c) {
return _this["Should return " + c.selected + " for accept-charset header " + c.accept + " with provided charset " + c.provided] = function(test) {
test.deepEqual(preferredCharsets(c.accept, c.provided), c.selected);
return test.done();
};
};
testConfigurations = [
{
accept: undefined,
provided: ['utf-8'],
selected: ['utf-8']
}, {
accept: 'utf-8',
provided: ['utf-8'],
selected: ['utf-8']
}, {
accept: '*',
provided: ['utf-8'],
selected: ['utf-8']
}, {
accept: 'utf-8',
provided: ['utf-8', 'ISO-8859-1'],
selected: ['utf-8']
}, {
accept: 'utf-8, ISO-8859-1',
provided: ['utf-8'],
selected: ['utf-8']
}, {
accept: 'utf-8;q=0.8, ISO-8859-1',
provided: ['utf-8', 'ISO-8859-1'],
selected: ['ISO-8859-1', 'utf-8']
}, {
accept: 'utf-8;q=0.8, ISO-8859-1',
provided: null,
selected: ['ISO-8859-1', 'utf-8']
}, {
accept: '*, utf-8;q=0',
provided: ['utf-8', 'ISO-8859-1'],
selected: ['ISO-8859-1']
}, {
accept : '*, utf-8',
provided: ['utf-8', 'ISO-8859-1' ],
selected: ['utf-8', 'ISO-8859-1' ]
}, {
accept : 'utf-8;q=0.9, ISO-8859-1;q=0.8, utf-8;q=0.7',
provided: ['utf-8', 'ISO-8859-1' ],
selected: ['utf-8', 'ISO-8859-1' ]
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectCharset(configuration);
}
}).call(this);
negotiator-0.4.8/test/encoding.js 0000664 0000000 0000000 00000006244 12412100246 0016777 0 ustar 00root root 0000000 0000000 (function() {
var configuration, preferredEncodings, testConfigurations, testCorrectEncoding, _i, _len,
_this = this;
preferredEncodings = require('../lib/encoding').preferredEncodings;
this["Should return identity encoding when no encoding is provided"] = function(test) {
test.deepEqual(preferredEncodings(null), ['identity']);
return test.done();
};
this["Should include the identity encoding even if not explicity listed"] = function(test) {
test.ok(preferredEncodings('gzip').indexOf('identity') !== -1);
return test.done();
};
this["Should not return identity encoding if q = 0"] = function(test) {
test.ok(preferredEncodings('identity;q=0').indexOf('identity') === -1);
return test.done();
};
this["Should not return identity encoding if * has q = 0"] = function(test) {
test.ok(preferredEncodings('*;q=0').indexOf('identity') === -1);
return test.done();
};
this["Should not return identity encoding if * has q = 0 but identity explicitly has q > 0"] = function(test) {
test.ok(preferredEncodings('*;q=0, identity;q=0.5').indexOf('identity') !== -1);
return test.done();
};
this["Should be case insensitive"] = function(test) {
test.deepEqual(preferredEncodings('IDENTITY', ['identity']), ['identity']);
return test.done();
};
testCorrectEncoding = function(c) {
return _this["Should return " + c.selected + " for accept-encoding header " + c.accept + " with provided encoding " + c.provided] = function(test) {
test.deepEqual(preferredEncodings(c.accept, c.provided), c.selected);
return test.done();
};
};
testConfigurations = [
{
accept: undefined,
provided: ['identity', 'gzip'],
selected: ['identity']
}, {
accept: 'gzip',
provided: ['identity', 'gzip'],
selected: ['gzip', 'identity']
}, {
accept: 'gzip, compress',
provided: ['compress'],
selected: ['compress']
}, {
accept: 'deflate',
provided: ['gzip', 'identity'],
selected: ['identity']
}, {
accept: '*',
provided: ['identity', 'gzip'],
selected: ['identity', 'gzip']
}, {
accept: 'gzip, compress',
provided: ['compress', 'identity'],
selected: ['compress', 'identity']
}, {
accept: 'gzip;q=0.8, identity;q=0.5, *;q=0.3',
provided: ['identity', 'gzip', 'compress'],
selected: ['gzip', 'identity', 'compress']
}, {
accept: 'gzip;q=0.8, compress',
provided: ['gzip', 'compress'],
selected: ['compress', 'gzip']
}, {
accept: '*, compress;q=0',
provided: ['gzip', 'compress'],
selected: ['gzip']
}, {
accept: 'gzip;q=0.8, compress',
provided: null,
selected: ['compress', 'gzip', 'identity']
}, {
accept : '*, compress',
provided : ['gzip', 'compress'],
selected : ['compress', 'gzip' ]
}, {
accept : 'gzip;q=0.9, compress;q=0.8, gzip;q=0.7',
provided : ['gzip', 'compress'],
selected : ['gzip', 'compress']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectEncoding(configuration);
}
}).call(this);
negotiator-0.4.8/test/language.js 0000664 0000000 0000000 00000006314 12412100246 0016772 0 ustar 00root root 0000000 0000000 (function() {
var configuration, preferredLanguages, testConfigurations, testCorrectType, _i, _len,
_this = this;
preferredLanguages = require('../lib/language').preferredLanguages;
this["Should return list of languages in order"] = function(test) {
test.deepEqual(preferredLanguages('nl;q=0.5,fr,de,en,it,es,pt,no,se,fi'), ['fr', 'de', 'en', 'it', 'es', 'pt', 'no', 'se', 'fi', 'nl']);
return test.done();
};
this["Should return list of languages in order (large list)"] = function(test) {
test.deepEqual(preferredLanguages('nl;q=0.5,fr,de,en,it,es,pt,no,se,fi,ro'), ['fr', 'de', 'en', 'it', 'es', 'pt', 'no', 'se', 'fi', 'ro', 'nl']);
return test.done();
};
this["Should return list of languages"] = function(test) {
test.deepEqual(preferredLanguages('nl;q=0.5,fr,de,en,it,es,pt,no,se,fi'), ['fr', 'de', 'en', 'it', 'es', 'pt', 'no', 'se', 'fi', 'nl']);
return test.done();
};
this["Should not return a language when no is provided"] = function(test) {
test.deepEqual(preferredLanguages('*', []), []);
return test.done();
};
this["Should not return a language when no language is acceptable"] = function(test) {
test.deepEqual(preferredLanguages('en', ['es']), []);
return test.done();
};
this["Should not return a language with q = 0"] = function(test) {
test.deepEqual(preferredLanguages('en;q=0', ['en']), []);
return test.done();
};
this["Should be case insensitive"] = function(test) {
test.deepEqual(preferredLanguages('en-us', ['en-US']), ['en-US']);
return test.done();
};
testCorrectType = function(c) {
return _this["Should return " + c.selected + " for accept-language header " + c.accept + " with provided language " + c.provided] = function(test) {
test.deepEqual(preferredLanguages(c.accept, c.provided), c.selected);
return test.done();
};
};
testConfigurations = [
{
accept: undefined,
provided: ['en'],
selected: ['en']
}, {
accept: 'en',
provided: ['en'],
selected: ['en']
}, {
accept: '*',
provided: ['en'],
selected: ['en']
}, {
accept: 'en-US, en;q=0.8',
provided: ['en-US', 'en-GB'],
selected: ['en-US', 'en-GB']
}, {
accept: 'en-US, en-GB',
provided: ['en-US'],
selected: ['en-US']
}, {
accept: 'en',
provided: ['en-US'],
selected: ['en-US']
}, {
accept: 'en;q=0.8, es',
provided: ['en', 'es'],
selected: ['es', 'en']
}, {
accept: 'en-US;q=0.8, es',
provided: ['en', 'es'],
selected: ['es', 'en']
}, {
accept: '*, en;q=0',
provided: ['en', 'es'],
selected: ['es']
}, {
accept: 'en-US;q=0.8, es',
provided: null,
selected: ['es', 'en-US']
}, {
accept: '*, en',
provided: ['es', 'en'],
selected: ['en', 'es']
}, {
accept: 'en',
provided: ['en', ''],
selected: ['en']
}, {
accept: 'en;q=0.9, es;q=0.8, en;q=0.7',
provided: ['en', 'es'],
selected: ['en', 'es']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectType(configuration);
}
}).call(this);
negotiator-0.4.8/test/mediaType.js 0000664 0000000 0000000 00000011713 12412100246 0017127 0 ustar 00root root 0000000 0000000 (function() {
var configuration, preferredMediaTypes, testConfigurations, testCorrectType, _i, _len,
_this = this;
preferredMediaTypes = require('../lib/mediaType').preferredMediaTypes;
this["Should not return a media type when no media type provided"] = function(test) {
test.deepEqual(preferredMediaTypes('*/*', []), []);
return test.done();
};
this["Should not return a media type when no media type is acceptable"] = function(test) {
test.deepEqual(preferredMediaTypes('application/json', ['text/html']), []);
return test.done();
};
this["Should not return a media type with q = 0"] = function(test) {
test.deepEqual(preferredMediaTypes('text/html;q=0', ['text/html']), []);
return test.done();
};
this["Should handle extra slashes on query params"] = function(test) {
var type = 'application/xhtml+xml;profile="http://www.wapforum.org/xhtml"'
test.deepEqual(preferredMediaTypes(type, ['application/xhtml+xml;profile="http://www.wapforum.org/xhtml"']), ['application/xhtml+xml;profile="http://www.wapforum.org/xhtml"']);
return test.done();
};
this["Should be case insensitive"] = function(test) {
test.deepEqual(preferredMediaTypes('application/JSON', ['application/json']), ['application/json']);
return test.done();
};
testCorrectType = function(c) {
return _this["Should return " + c.selected + " for access header " + c.accept + " with provided types " + c.provided] = function(test) {
test.deepEqual(preferredMediaTypes(c.accept, c.provided), c.selected);
return test.done();
};
};
testConfigurations = [
{
accept: undefined,
provided: ['text/html'],
selected: ['text/html']
}, {
accept: 'text/html',
provided: ['text/html'],
selected: ['text/html']
}, {
accept: '*/*',
provided: ['text/html'],
selected: ['text/html']
}, {
accept: 'text/*',
provided: ['text/html'],
selected: ['text/html']
}, {
accept: 'application/json, text/html',
provided: ['text/html'],
selected: ['text/html']
}, {
accept: 'text/html;q=0.1',
provided: ['text/html'],
selected: ['text/html']
}, {
accept: 'application/json, text/html',
provided: ['application/json', 'text/html'],
selected: ['application/json', 'text/html']
}, {
accept: 'application/json;q=0.2, text/html',
provided: ['application/json', 'text/html'],
selected: ['text/html', 'application/json']
}, {
accept: 'application/json;q=0.2, text/html',
provided: null,
selected: ['text/html', 'application/json']
}, {
accept: 'text/*, text/html;q=0',
provided: ['text/html', 'text/plain'],
selected: ['text/plain']
}, {
accept: 'text/*, text/html;q=0.5',
provided: ['text/html', 'text/plain'],
selected: ['text/plain', 'text/html']
}, {
accept: 'application/json, */*; q=0.01',
provided: ['text/html', 'application/json'],
selected: ['application/json', 'text/html']
}, {
accept: 'application/vnd.example;attribute=value',
provided: ['application/vnd.example;attribute=other', 'application/vnd.example;attribute=value'],
selected: ['application/vnd.example;attribute=value']
}, {
accept: 'application/vnd.example;attribute=other',
provided: ['application/vnd.example', 'application/vnd.example;attribute=other'],
selected: ['application/vnd.example;attribute=other']
}, {
accept: 'text/html;level=1',
provided: ['text/html;level=1;foo=bar'],
selected: ['text/html;level=1;foo=bar']
}, {
accept: 'text/html;level=1;foo=bar',
provided: ['text/html;level=1'],
selected: []
}, {
accept: 'text/html;level=2',
provided: ['text/html;level=1'],
selected: []
}, {
accept : 'text/html, text/html;level=1;q=0.1',
provided : ['text/html', 'text/html;level=1'],
selected : ['text/html', 'text/html;level=1']
}, {
accept : 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5',
provided : ['text/html;level=1', 'text/html', 'text/html;level=3', 'image/jpeg', 'text/html;level=2', 'text/plain'],
selected : ['text/html;level=1', 'text/html', 'text/html;level=3', 'image/jpeg', 'text/html;level=2', 'text/plain']
}, {
accept : 'text/html, application/xhtml+xml, */*',
provided : ['application/json', 'text/html'],
selected : ['text/html', 'application/json' ]
}, {
accept : 'text/html, application/json',
provided : ['text/html', 'boom'],
selected : ['text/html']
}, {
accept: 'application/json;q=0.9, text/html;q=0.8, application/json;q=0.7',
provided: ['application/json', 'text/html'],
selected: ['application/json', 'text/html']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectType(configuration);
}
}).call(this);