pax_global_header 0000666 0000000 0000000 00000000064 12230313015 0014500 g ustar 00root root 0000000 0000000 52 comment=b894c2009430605fe38eba811ec5aff59e517942
negotiator-0.3.0/ 0000775 0000000 0000000 00000000000 12230313015 0013653 5 ustar 00root root 0000000 0000000 negotiator-0.3.0/LICENSE 0000664 0000000 0000000 00000002213 12230313015 0014656 0 ustar 00root root 0000000 0000000 Original "Negotiator" program Copyright Federico Romero
Port to JavaScript Copyright Isaac Z. Schlueter
All rights reserved.
MIT License
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.3.0/examples/ 0000775 0000000 0000000 00000000000 12230313015 0015471 5 ustar 00root root 0000000 0000000 negotiator-0.3.0/examples/accept.js 0000664 0000000 0000000 00000002347 12230313015 0017274 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.preferredMediaTypes()));
console.log("Possible: " + (negotiator.preferredMediaTypes(availableMediaTypes)));
mediaType = negotiator.preferredMediaType(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.3.0/examples/charset.js 0000664 0000000 0000000 00000002463 12230313015 0017465 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.preferredCharsets()));
console.log("Possible: " + (negotiator.preferredCharsets(availableCharsets)));
charset = negotiator.preferredCharset(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.3.0/examples/encoding.js 0000664 0000000 0000000 00000002475 12230313015 0017625 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.preferredEncodings()));
console.log("Possible: " + (negotiator.preferredEncodings(availableEncodings)));
encoding = negotiator.preferredEncoding(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.3.0/examples/language.js 0000664 0000000 0000000 00000002153 12230313015 0017613 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.preferredLanguages()));
console.log("Possible: " + (negotiator.preferredLanguages(availableLanguages)));
language = negotiator.preferredLanguage(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.3.0/lib/ 0000775 0000000 0000000 00000000000 12230313015 0014421 5 ustar 00root root 0000000 0000000 negotiator-0.3.0/lib/charset.js 0000664 0000000 0000000 00000003161 12230313015 0016411 0 ustar 00root root 0000000 0000000 module.exports = preferredCharsets;
preferredCharsets.preferredCharsets = preferredCharsets;
function parseAcceptCharset(accept) {
return accept.split(',').map(function(e) {
return parseCharset(e.trim());
}).filter(function(e) {
return e && e.q > 0;
});
}
function parseCharset(s) {
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
};
}
function getCharsetPriority(charset, accepted) {
return (accepted.filter(function(a) {
return specify(charset, a);
}).sort(function (a, b) {
// revsort
return a.q === b.q ? 0 : a.q > b.q ? -1 : 1;
})[0] || {q:0}).q;
}
function specify(charset, spec) {
if (spec.charset === '*' || spec.charset === charset) {
return spec;
}
};
function preferredCharsets(accept, provided) {
accept = parseAcceptCharset(accept || '');
if (provided) {
return provided.map(function(type) {
return [type, getCharsetPriority(type, accept)];
}).filter(function(pair) {
return pair[1] > 0;
}).sort(function(a, b) {
// revsort
return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
}).map(function(pair) {
return pair[0];
});
} else {
return accept.sort(function (a, b) {
// revsort
return a.q < b.q ? 1 : -1;
}).map(function(type) {
return type.charset;
});
}
}
negotiator-0.3.0/lib/encoding.js 0000664 0000000 0000000 00000003712 12230313015 0016550 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) {
return parseEncoding(e.trim());
});
} else {
acceptableEncodings = [];
}
if (!acceptableEncodings.some(function(e) {
return e && e.encoding === 'identity';
})) {
acceptableEncodings.push({
encoding: 'identity',
q: 0.1
});
}
return acceptableEncodings.filter(function(e) {
return e && e.q > 0;
});
}
function parseEncoding(s) {
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
};
}
function getEncodingPriority(encoding, accepted) {
return (accepted.filter(function(a) {
return specify(encoding, a);
}).sort(function (a, b) {
// revsort
return a.q === b.q ? 0 : a.q > b.q ? -1 : 1;
})[0] || {q:0}).q;
}
function specify(encoding, spec) {
if (spec.encoding === '*' || spec.encoding === encoding) {
return spec;
}
}
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] > 0;
}).sort(function(a, b) {
// revsort
return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
}).map(function(pair) {
return pair[0];
});
} else {
return accept.sort(function (a, b) {
// revsort
return a.q < b.q ? 1 : -1;
}).map(function(type) {
return type.encoding;
});
}
}
negotiator-0.3.0/lib/language.js 0000664 0000000 0000000 00000004130 12230313015 0016540 0 ustar 00root root 0000000 0000000 module.exports = preferredLanguages;
preferredLanguages.preferredLanguages = preferredLanguages;
function parseAcceptLanguage(accept) {
return accept.split(',').map(function(e) {
return parseLanguage(e.trim());
}).filter(function(e) {
return e && e.q > 0;
});
}
function parseLanguage(s) {
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,
full: full
};
}
function getLanguagePriority(language, accepted) {
var match = getClosestMatch(language, accepted);
return match ? match.q : 0;
}
function getClosestMatch(language, accepted) {
var parsed = parseLanguage(language);
var matches = accepted.filter(function(a) {
return a.full === parsed.full;
});
if (matches.length) return matches[0];
matches = accepted.filter(function(a) {
return a.prefix === parsed.prefix && !a.suffix;
});
if (matches.length) return matches[0];
matches = accepted.filter(function(a) {
return a.prefix === parsed.prefix;
});
if (matches.length) return matches[0];
matches = accepted.filter(function(a) {
return a.prefix === '*';
});
return matches[0];
}
function preferredLanguages(accept, provided) {
accept = parseAcceptLanguage(accept || '');
if (provided) {
var ret = provided.map(function(type) {
return [type, getLanguagePriority(type, accept)];
}).filter(function(pair) {
return pair[1] > 0;
}).sort(function(a, b) {
// revsort
return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
}).map(function(pair) {
return pair[0];
});
return ret;
} else {
return accept.sort(function (a, b) {
// revsort
return a.q < b.q ? 1 : -1;
}).map(function(type) {
return type.full;
});
}
}
negotiator-0.3.0/lib/mediaType.js 0000664 0000000 0000000 00000004177 12230313015 0016711 0 ustar 00root root 0000000 0000000 module.exports = preferredMediaTypes;
preferredMediaTypes.preferredMediaTypes = preferredMediaTypes;
function parseAccept(accept) {
return accept.split(',').map(function(e) {
return parseMediaType(e.trim());
}).filter(function(e) {
return e && e.q > 0;
});
};
function parseMediaType(s) {
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,
full: full
};
}
function getMediaTypePriority(type, accepted) {
return (accepted.filter(function(a) {
return specify(type, a);
}).sort(function (a, b) {
// revsort
return a.q > b.q ? -1 : 1;
})[0] || {q:0}).q;
}
function specifies(spec, type) {
return spec === '*' || spec === type;
}
function specify(type, spec) {
var p = parseMediaType(type);
if (spec.params) {
var keys = Object.keys(spec.params);
if (keys.some(function (k) {
return !specifies(spec.params[k], p.params[k]);
})) {
// some didn't specify.
return null;
}
}
if (specifies(spec.type, p.type) &&
specifies(spec.subtype, p.subtype)) {
return spec;
}
}
function preferredMediaTypes(accept, provided) {
accept = parseAccept(accept || '');
if (provided) {
return provided.map(function(type) {
return [type, getMediaTypePriority(type, accept)];
}).filter(function(pair) {
return pair[1] > 0;
}).sort(function(a, b) {
// revsort
return a[1] === b[1] ? 0 : a[1] > b[1] ? -1 : 1;
}).map(function(pair) {
return pair[0];
});
} else {
return accept.sort(function (a, b) {
// revsort
return a.q < b.q ? 1 : -1;
}).map(function(type) {
return type.full;
});
}
}
negotiator-0.3.0/lib/negotiator.js 0000664 0000000 0000000 00000001606 12230313015 0017135 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 = { preferredCharset: [require('./charset.js'), 'accept-charset'],
preferredEncoding: [require('./encoding.js'), 'accept-encoding'],
preferredLanguage: [require('./language.js'), 'accept-language'],
preferredMediaType: [require('./mediaType.js'), 'accept'] };
Object.keys(set).forEach(function (k) {
var mh = set[k],
method = mh[0],
header = mh[1],
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];
};
})
negotiator-0.3.0/package.json 0000664 0000000 0000000 00000001314 12230313015 0016140 0 ustar 00root root 0000000 0000000 {
"name": "negotiator",
"description": "HTTP content negotiation",
"version": "0.3.0",
"author": "Federico Romero ",
"contributors": ["Isaac Z. Schlueter (http://blog.izs.me/)"],
"repository": {
"type": "git",
"url": "git://github.com/federomero/negotiator.git"
},
"keywords": [
"http",
"content negotiation",
"accept",
"accept-language",
"accept-encoding",
"accept-charset"
],
"engine": "node >= 0.6",
"license": "MIT",
"devDependencies": {
"nodeunit": "0.6.x"
},
"scripts": {
"test": "nodeunit test"
},
"optionalDependencies": {},
"engines": {
"node": "*"
},
"main": "lib/negotiator.js"
}
negotiator-0.3.0/readme.md 0000664 0000000 0000000 00000006534 12230313015 0015442 0 ustar 00root root 0000000 0000000 # Negotiator
An HTTP content negotiator for node.js written in javascript.
# Accept Negotiation
Negotiator = require('negotiator')
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.preferredMediaTypes()
// -> ['text/html', 'image/jpeg', 'application/*']
negotiator.preferredMediaTypes(availableMediaTypes)
// -> ['text/html', 'application/json']
negotiator.preferredMediaType(availableMediaTypes)
// -> 'text/html'
You can check a working example at `examples/accept.js`.
## Methods
`preferredMediaTypes(availableMediaTypes)`:
Returns an array of preferred media types ordered by priority from a list of available media types.
`preferredMediaType(availableMediaType)`:
Returns the top preferred media type from a list of available media types.
# Accept-Language Negotiation
Negotiator = require('negotiator')
negotiator = new Negotiator(request)
availableLanguages = 'en', 'es', 'fr'
// Let's say Accept-Language header is 'en;q=0.8, es, pt'
negotiator.preferredLanguages()
// -> ['es', 'pt', 'en']
negotiator.preferredLanguages(availableLanguages)
// -> ['es', 'en']
language = negotiator.preferredLanguage(availableLanguages)
// -> 'es'
You can check a working example at `examples/language.js`.
## Methods
`preferredLanguages(availableLanguages)`:
Returns an array of preferred languages ordered by priority from a list of available languages.
`preferredLanguage(availableLanguages)`:
Returns the top preferred language from a list of available languages.
# Accept-Charset Negotiation
Negotiator = require('negotiator')
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.preferredCharsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']
negotiator.preferredCharsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']
negotiator.preferredCharset(availableCharsets)
// -> 'utf-8'
You can check a working example at `examples/charset.js`.
## Methods
`preferredCharsets(availableCharsets)`:
Returns an array of preferred charsets ordered by priority from a list of available charsets.
`preferredCharset(availableCharsets)`:
Returns the top preferred charset from a list of available charsets.
# Accept-Encoding Negotiation
Negotiator = require('negotiator').Negotiator
availableEncodings = ['identity', 'gzip']
negotiator = new Negotiator(request)
// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'
negotiator.preferredEncodings()
// -> ['gzip', 'identity', 'compress']
negotiator.preferredEncodings(availableEncodings)
// -> ['gzip', 'identity']
negotiator.preferredEncoding(availableEncodings)
// -> 'gzip'
You can check a working example at `examples/encoding.js`.
## Methods
`preferredEncodings(availableEncodings)`:
Returns an array of preferred encodings ordered by priority from a list of available encodings.
`preferredEncoding(availableEncodings)`:
Returns the top preferred encoding from a list of available encodings.
# License
MIT
negotiator-0.3.0/test/ 0000775 0000000 0000000 00000000000 12230313015 0014632 5 ustar 00root root 0000000 0000000 negotiator-0.3.0/test/charset.js 0000664 0000000 0000000 00000003434 12230313015 0016625 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();
};
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: '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']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectCharset(configuration);
}
}).call(this);
negotiator-0.3.0/test/encoding.js 0000664 0000000 0000000 00000004224 12230313015 0016760 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();
};
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: '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: 'gzip;q=0.8, compress',
provided: null,
selected: ['compress', 'gzip', 'identity']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectEncoding(configuration);
}
}).call(this);
negotiator-0.3.0/test/language.js 0000664 0000000 0000000 00000003610 12230313015 0016753 0 ustar 00root root 0000000 0000000 (function() {
var configuration, preferredLanguages, testConfigurations, testCorrectType, _i, _len,
_this = this;
preferredLanguages = require('../lib/language').preferredLanguages;
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();
};
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: '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-US;q=0.8, es',
provided: null,
selected: ['es', 'en-US']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectType(configuration);
}
}).call(this);
negotiator-0.3.0/test/mediaType.js 0000664 0000000 0000000 00000004220 12230313015 0017107 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();
};
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: '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']
}
];
for (_i = 0, _len = testConfigurations.length; _i < _len; _i++) {
configuration = testConfigurations[_i];
testCorrectType(configuration);
}
}).call(this);