You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.1 KiB

var middleware = require('../../../middleware/requestLanguage');
module.exports.tests = {};
var DEFAULTS = {
defaulted: true,
iso6391: 'en',
iso6392B: 'eng',
iso6392T: 'eng',
iso6393: 'eng',
name: 'English',
scope: 'individual',
type: 'living'
};
module.exports.tests.defaults = function(test, common) {
test('default language', function(t) {
var req = { headers: {} };
middleware(req, {}, function () {
t.deepEqual( req.language, DEFAULTS, '$req.language set' );
t.end();
});
});
};
module.exports.tests.invalid = function(test, common) {
test('headers: invalid language', function(t) {
var req = { headers: {
'accept-language': 'invalid language'
}};
middleware(req, {}, function () {
t.deepEqual( req.language, DEFAULTS, '$req.language set' );
t.end();
});
});
};
module.exports.tests.valid = function(test, common) {
test('headers: valid language - french', function(t) {
var req = { headers: {
'accept-language': 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5'
}};
var expected = {
defaulted: false,
iso6391: 'fr',
iso6392B: 'fre',
iso6392T: 'fra',
iso6393: 'fra',
name: 'French',
scope: 'individual',
type: 'living'
};
middleware(req, {}, function () {
t.deepEqual( req.language, expected, '$req.language set' );
t.end();
});
});
test('headers: valid language - english', function(t) {
var req = { headers: {
'accept-language': 'en'
}};
var expected = {
defaulted: false,
iso6391: 'en',
iso6392B: 'eng',
iso6392T: 'eng',
iso6393: 'eng',
name: 'English',
scope: 'individual',
type: 'living'
};
middleware(req, {}, function () {
t.deepEqual( req.language, expected, '$req.language set' );
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] requestLanguage: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};