pax_global_header00006660000000000000000000000064124121002460014502gustar00rootroot0000000000000052 comment=4b0bc3f2fec38a839556bd4674f79024929ba256 negotiator-0.4.8/000077500000000000000000000000001241210024600136665ustar00rootroot00000000000000negotiator-0.4.8/.gitignore000066400000000000000000000002271241210024600156570ustar00rootroot00000000000000# 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.yml000066400000000000000000000002571241210024600160030ustar00rootroot00000000000000node_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/LICENSE000066400000000000000000000021411241210024600146710ustar00rootroot00000000000000(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.md000066400000000000000000000076561241210024600151630ustar00rootroot00000000000000# 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/000077500000000000000000000000001241210024600155045ustar00rootroot00000000000000negotiator-0.4.8/examples/accept.js000066400000000000000000000023141241210024600173010ustar00rootroot00000000000000(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.js000066400000000000000000000024301241210024600174720ustar00rootroot00000000000000(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.js000066400000000000000000000024421241210024600176320ustar00rootroot00000000000000(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.js000066400000000000000000000021201241210024600176200ustar00rootroot00000000000000(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/000077500000000000000000000000001241210024600144345ustar00rootroot00000000000000negotiator-0.4.8/lib/charset.js000066400000000000000000000037011241210024600164240ustar00rootroot00000000000000module.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.js000066400000000000000000000051001241210024600165540ustar00rootroot00000000000000module.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.js000066400000000000000000000044131241210024600165570ustar00rootroot00000000000000module.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.js000066400000000000000000000051161241210024600167160ustar00rootroot00000000000000module.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.js000066400000000000000000000020651241210024600171500ustar00rootroot00000000000000module.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.json000066400000000000000000000013541241210024600161570ustar00rootroot00000000000000{ "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/000077500000000000000000000000001241210024600146455ustar00rootroot00000000000000negotiator-0.4.8/test/charset.js000066400000000000000000000046351241210024600166440ustar00rootroot00000000000000(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.js000066400000000000000000000062441241210024600167770ustar00rootroot00000000000000(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.js000066400000000000000000000063141241210024600167720ustar00rootroot00000000000000(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.js000066400000000000000000000117131241210024600171270ustar00rootroot00000000000000(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);