Browse Source

add unit tests for language

pull/819/head
Peter Johnson 8 years ago
parent
commit
1847fb3692
  1. 97
      test/unit/middleware/requestLanguage.js
  2. 1
      test/unit/run.js

97
test/unit/middleware/requestLanguage.js

@ -0,0 +1,97 @@
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);
}
};

1
test/unit/run.js

@ -39,6 +39,7 @@ var tests = [
require('./middleware/normalizeParentIds'),
require('./middleware/trimByGranularity'),
require('./middleware/trimByGranularityStructured'),
require('./middleware/requestLanguage'),
require('./query/autocomplete'),
require('./query/autocomplete_defaults'),
require('./query/search_defaults'),

Loading…
Cancel
Save