feat: add writeCookie option (#28)

master
Tao Xu 2018-01-12 11:45:59 +08:00 committed by fengmk2
parent 043fab2b97
commit f89c6755c1
2 changed files with 47 additions and 12 deletions

View File

@ -15,6 +15,7 @@ const DEFAULT_OPTIONS = {
queryField: 'locale',
cookieField: 'locale',
localeAlias: {},
writeCookie: true,
cookieMaxAge: '1y',
dir: undefined,
dirs: [ path.join(process.cwd(), 'locales') ],
@ -27,6 +28,7 @@ module.exports = function (app, options) {
const queryField = options.queryField;
const cookieField = options.cookieField;
const localeAlias = options.localeAlias;
const writeCookie = options.writeCookie;
const cookieMaxAge = ms(options.cookieMaxAge);
const localeDir = options.dir;
const localeDirs = options.dirs;
@ -190,7 +192,7 @@ module.exports = function (app, options) {
}
// if header not send, set the locale cookie
if (cookieLocale !== locale && !this.headerSent) {
if (writeCookie && cookieLocale !== locale && !this.headerSent) {
// locale change, need to set cookie
this.cookies.set(cookieField, locale, {
// make sure brower javascript can read the cookie

View File

@ -61,6 +61,10 @@ describe('koa-locales.test.js', function () {
'de-de': 'de',
},
});
const appNotWriteCookie = createApp({
dirs: [__dirname + '/locales', __dirname + '/other-locales'],
writeCookie: false,
});
it('should use default locale: en-US', function (done) {
request(app.callback())
@ -214,18 +218,17 @@ describe('koa-locales.test.js', function () {
.expect(200, done);
});
it('should use localeAlias header', function (done) {
request(cookieFieldMapApp.callback())
.get('/')
.set('Accept-Language', 'ja,de-de;q=0.8')
it('should use query locale and response without set-cookie', function (done) {
request(appNotWriteCookie.callback())
.get('/?locale=zh-CN')
.expect({
email: 'Emailde',
hello: 'Hallo fengmk2, wie geht es dir heute?',
message: 'Hallo fengmk2, wie geht es dir heute? Wie war dein 18.',
email: '邮箱1',
hello: 'fengmk2今天过得如何',
message: 'Hello fengmk2, how are you today? How was your 18.',
empty: '',
notexists_key: 'key not exists',
empty_string: '',
empty_value: 'emptyValue',
empty_value: '',
novalue: 'key %s ok',
arguments3: '1 2 3',
arguments4: '1 2 3 4',
@ -233,10 +236,14 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
'gender': '性别',
'name': '姓名',
})
.expect(function(res) {
if(res.headers['set-cookie'] || res.headers['Set-Cookie']){
throw new Error('should not write cookie');
}
})
.expect('Set-Cookie', /^locale=de; path=\/; expires=\w+/)
.expect(200, done);
});
@ -401,6 +408,32 @@ describe('koa-locales.test.js', function () {
.expect(200, done);
});
it('should work with "Accept-Language: de-de" by localeAlias', function (done) {
request(cookieFieldMapApp.callback())
.get('/')
.set('Accept-Language', 'ja,de-de;q=0.8')
.expect({
email: 'Emailde',
hello: 'Hallo fengmk2, wie geht es dir heute?',
message: 'Hallo fengmk2, wie geht es dir heute? Wie war dein 18.',
empty: '',
notexists_key: 'key not exists',
empty_string: '',
empty_value: 'emptyValue',
novalue: 'key %s ok',
arguments3: '1 2 3',
arguments4: '1 2 3 4',
arguments5: '1 2 3 4 5',
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
.expect('Set-Cookie', /^locale=de; path=\/; expires=\w+/)
.expect(200, done);
});
it('should mock acceptsLanguages return string', function (done) {
mm(app.request, 'acceptsLanguages', function () {
return 'zh-TW';