feat: ctx.__setLocale (#36)

This commit is contained in:
Yiyu He
2019-04-30 20:11:00 +08:00
committed by GitHub
parent 5b00e90a11
commit 0767037b3c
3 changed files with 50 additions and 15 deletions

View File

@@ -525,30 +525,40 @@ describe('koa-locales.test.js', function () {
describe('__getLocale and __getLocaleOrigin', function() {
it('should __getLocale and __getLocaleOrigin from cookie', function () {
return request(app.callback())
.get('/methods')
.get('/origin')
.set('cookie', 'locale=de')
.expect(200, { locale: 'de', localeOrigin: 'cookie' });
});
it('should __getLocale and __getLocaleOrigin from query', function () {
return request(app.callback())
.get('/methods?locale=de')
.get('/origin?locale=de')
.expect(200, { locale: 'de', localeOrigin: 'query' });
});
it('should __getLocale and __getLocaleOrigin from header', function () {
return request(app.callback())
.get('/methods')
.get('/origin')
.set('Accept-Language', 'zh-cn')
.expect(200, { locale: 'zh-cn', localeOrigin: 'header' });
});
it('should __getLocale and __getLocaleOrigin from default', function () {
return request(app.callback())
.get('/methods')
.get('/origin')
.expect(200, { locale: 'en-us', localeOrigin: 'default' });
});
});
describe('__setLocale', function() {
it('should set locale and cookie', function () {
return request(app.callback())
.get('/set')
.set('cookie', 'locale=de')
.expect(200, { locale: 'zh-hk', localeOrigin: 'set' })
.expect('Set-Cookie', /^locale=zh\-hk; path=\/; expires=[^;]+ GMT$/);
});
});
});
});
@@ -558,7 +568,7 @@ function createApp(options) {
const fname = options && options.functionName || '__';
app.use(function* () {
if (this.path === '/methods') {
if (this.path === '/origin') {
assert(this.__getLocaleOrigin() === this.__getLocaleOrigin());
this.body = {
locale: this.__getLocale(),
@@ -567,6 +577,17 @@ function createApp(options) {
return;
}
if (this.path === '/set') {
this.__getLocale();
this.__setLocale('zh-tw');
this.__setLocale('zh-hk');
this.body = {
locale: this.__getLocale(),
localeOrigin: this.__getLocaleOrigin(),
};
return;
}
if (this.url === '/headerSent') {
this.body = 'foo';
const that = this;