feat: add __getLocaleOrigin (#35)

* chore: test on node 12
This commit is contained in:
Yiyu He
2019-04-29 16:15:14 +08:00
committed by fengmk2
parent 86514e3f97
commit 3043365e09
6 changed files with 56 additions and 4 deletions

View File

@@ -521,6 +521,34 @@ describe('koa-locales.test.js', function () {
.expect(200, done);
});
});
describe('__getLocale and __getLocaleOrigin', function() {
it('should __getLocale and __getLocaleOrigin from cookie', function () {
return request(app.callback())
.get('/methods')
.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')
.expect(200, { locale: 'de', localeOrigin: 'query' });
});
it('should __getLocale and __getLocaleOrigin from header', function () {
return request(app.callback())
.get('/methods')
.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')
.expect(200, { locale: 'en-us', localeOrigin: 'default' });
});
});
});
});
@@ -530,6 +558,15 @@ function createApp(options) {
const fname = options && options.functionName || '__';
app.use(function* () {
if (this.path === '/methods') {
assert(this.__getLocaleOrigin() === this.__getLocaleOrigin());
this.body = {
locale: this.__getLocale(),
localeOrigin: this.__getLocaleOrigin(),
};
return;
}
if (this.url === '/headerSent') {
this.body = 'foo';
const that = this;