forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage-tests.js
67 lines (59 loc) · 1.81 KB
/
language-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const assert = require('assert');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const config = {
l10n_dev: false // prod configuration
};
const pkg = {
availableLanguages: ['en-US', 'fr', 'it', 'es-ES']
};
function request(acceptLang) {
return {
headers: {
'accept-language': acceptLang
}
};
}
const langMiddleware = proxyquire('../../server/middleware/language', {
'../config': config,
'../../package.json': pkg
});
describe('Language Middleware', function() {
it('defaults to en-US when no header is present', function() {
const req = request();
const next = sinon.stub();
langMiddleware(req, null, next);
assert.equal(req.language, 'en-US');
sinon.assert.calledOnce(next);
});
it('sets req.language to en-US when accept-language > 255 chars', function() {
const accept = Array(257).join('a');
assert.equal(accept.length, 256);
const req = request(accept);
const next = sinon.stub();
langMiddleware(req, null, next);
assert.equal(req.language, 'en-US');
sinon.assert.calledOnce(next);
});
it('defaults to en-US when no accept-language is available', function() {
const req = request('fa,cs,ja');
const next = sinon.stub();
langMiddleware(req, null, next);
assert.equal(req.language, 'en-US');
sinon.assert.calledOnce(next);
});
it('prefers higher q values', function() {
const req = request('fa;q=0.5, it;q=0.9');
const next = sinon.stub();
langMiddleware(req, null, next);
assert.equal(req.language, 'it');
sinon.assert.calledOnce(next);
});
it('uses likely subtags', function() {
const req = request('es-MX');
const next = sinon.stub();
langMiddleware(req, null, next);
assert.equal(req.language, 'es-ES');
sinon.assert.calledOn(next);
});
});