hapi-locale-17
Advanced tools
Comparing version 2.0.0 to 2.0.1
{ | ||
"name": "hapi-locale-17", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Locale and language detection for Hapi v17", | ||
"main": "src/index.js", | ||
"author": "Frank Thelen", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"repository": { | ||
@@ -21,3 +21,5 @@ "type": "git", | ||
"lint": "eslint . --ignore-path ./.eslintignore", | ||
"test": "NODE_ENV=test istanbul cover _mocha -- --recursive test", | ||
"test": "NODE_ENV=test mocha --recursive test", | ||
"cover": "NODE_ENV=test istanbul cover _mocha -- --recursive test", | ||
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls", | ||
"preversion": "npm run lint && npm test" | ||
@@ -31,2 +33,3 @@ }, | ||
"chai-as-promised": "^7.1.1", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.13.1", | ||
@@ -39,3 +42,6 @@ "eslint-config-airbnb-base": "^12.1.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^4.0.1" | ||
"mocha": "^4.0.1", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"sinon": "^4.1.3", | ||
"sinon-chai": "^2.14.0" | ||
}, | ||
@@ -42,0 +48,0 @@ "dependencies": { |
@@ -5,2 +5,8 @@ # hapi-locale-17 | ||
[![build status](https://img.shields.io/travis/frankthelen/hapi-locale-17.svg)](http://travis-ci.org/frankthelen/hapi-locale-17) | ||
[![Coverage Status](https://coveralls.io/repos/github/frankthelen/hapi-locale-17/badge.svg?branch=master)](https://coveralls.io/github/frankthelen/hapi-locale-17?branch=master) | ||
[![dependencies Status](https://david-dm.org/frankthelen/hapi-locale-17/status.svg)](https://david-dm.org/frankthelen/hapi-locale-17) | ||
[![node](https://img.shields.io/node/v/hapi-locale-17.svg)]() | ||
[![License Status](http://img.shields.io/npm/l/hapi-locale-17.svg)]() | ||
Evaluates locale information from `accept-language` header and query or path parameter. | ||
@@ -7,0 +13,0 @@ Decorates Hapi request object with `request.getLocale()` available in all route handlers. |
@@ -7,3 +7,2 @@ const parser = require('accept-language-parser'); | ||
locales = [], | ||
fallback = locales[0], | ||
query = 'locale', | ||
@@ -14,2 +13,3 @@ path = 'locale', | ||
server.decorate('request', method, function f() { | ||
const fallback = locales[0]; | ||
const request = this; | ||
@@ -16,0 +16,0 @@ try { |
const Hapi = require('hapi'); | ||
const chai = require('chai'); | ||
const chaiAsPromised = require('chai-as-promised'); | ||
const sinon = require('sinon'); | ||
const sinonChai = require('sinon-chai'); | ||
const parser = require('accept-language-parser'); // to be mocked | ||
const locale = require('../src/index'); | ||
chai.use(chaiAsPromised); | ||
chai.use(sinonChai); | ||
global.chai = chai; | ||
global.sinon = sinon; | ||
global.expect = chai.expect; | ||
@@ -49,7 +54,7 @@ global.should = chai.should(); | ||
afterEach(async () => { | ||
server.stop(); | ||
await server.stop(); | ||
}); | ||
it('should provide `request.getLocale()` with supported `de`', () => { | ||
return server | ||
it('should provide `request.getLocale()` with supported `de`', () => | ||
server | ||
.inject({ | ||
@@ -61,9 +66,7 @@ url: '/test', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('de'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('de'))); | ||
it('should provide `request.getLocale()` with supported `de` / query param', () => { | ||
return server | ||
it('should provide `request.getLocale()` with supported `de` / query param', () => | ||
server | ||
.inject({ | ||
@@ -75,9 +78,7 @@ url: '/test?locale=de', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('de'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('de'))); | ||
it('should provide `request.getLocale()` with supported `de` / path param', () => { | ||
return server | ||
it('should provide `request.getLocale()` with supported `de` / path param', () => | ||
server | ||
.inject({ | ||
@@ -89,9 +90,7 @@ url: '/media/de', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('de'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('de'))); | ||
it('should provide `request.getLocale()` with default `es` / query param with unsupported locale', () => { | ||
return server | ||
it('should provide `request.getLocale()` with default `es` / query param with unsupported locale', () => | ||
server | ||
.inject({ | ||
@@ -103,9 +102,7 @@ url: '/test?locale=tr', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('es'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('es'))); | ||
it('should provide `request.getLocale()` with default `es` / path param with unsupported locale', () => { | ||
return server | ||
it('should provide `request.getLocale()` with default `es` / path param with unsupported locale', () => | ||
server | ||
.inject({ | ||
@@ -117,6 +114,23 @@ url: '/media/tr', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('es'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('es'))); | ||
it('should provide `request.getLocale()` with default `es` / no indocation', () => | ||
server | ||
.inject({ | ||
url: '/test', | ||
}) | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('es'))); | ||
it('should provide `request.getLocale()` with default `es` / invalid header', () => | ||
server | ||
.inject({ | ||
url: '/test', | ||
headers: { | ||
'Accept-Language': 'cq#brw/hdbjhfd,bkaq8ö?347r;z12lekw:vmcöar-fvic', | ||
}, | ||
}) | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('es'))); | ||
}); | ||
@@ -135,7 +149,7 @@ | ||
after(async () => { | ||
server.stop(); | ||
await server.stop(); | ||
}); | ||
it('should provide user-defined `request.getLang()`', () => { | ||
return server | ||
it('should provide user-defined `request.getLang()`', () => | ||
server | ||
.inject({ | ||
@@ -147,6 +161,4 @@ url: '/test', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLang()).to.be.equal('en'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLang()).to.be.equal('en'))); | ||
}); | ||
@@ -167,7 +179,7 @@ | ||
after(async () => { | ||
server.stop(); | ||
await server.stop(); | ||
}); | ||
it('should accept user-defined query param `lang`', () => { | ||
return server | ||
it('should accept user-defined query param `lang`', () => | ||
server | ||
.inject({ | ||
@@ -179,6 +191,4 @@ url: '/test?lang=de', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLang()).to.be.equal('de'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLang()).to.be.equal('de'))); | ||
}); | ||
@@ -199,7 +209,7 @@ | ||
after(async () => { | ||
server.stop(); | ||
await server.stop(); | ||
}); | ||
it('should accept user-defined path param `lang`', () => { | ||
return server | ||
it('should accept user-defined path param `lang`', () => | ||
server | ||
.inject({ | ||
@@ -211,6 +221,4 @@ url: '/media2/de', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLang()).to.be.equal('de'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLang()).to.be.equal('de'))); | ||
}); | ||
@@ -228,7 +236,7 @@ | ||
after(async () => { | ||
server.stop(); | ||
await server.stop(); | ||
}); | ||
it('should accept locale `en-US`', () => { | ||
return server | ||
it('should accept locale `en-US`', () => | ||
server | ||
.inject({ | ||
@@ -240,9 +248,7 @@ url: '/test', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('en-US'); | ||
}); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('en-US'))); | ||
it('should accept locale `en-US` / query param', () => { | ||
return server | ||
it('should accept locale `en-US` / query param', () => | ||
server | ||
.inject({ | ||
@@ -254,6 +260,31 @@ url: '/test?locale=en', | ||
}) | ||
.should.be.fulfilled.then((response) => { | ||
expect(response.request.getLocale()).to.be.equal('en-US'); | ||
}); | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('en-US'))); | ||
}); | ||
describe('hapi-locale-17 with error in dependency', async () => { | ||
let server; | ||
before(async () => { | ||
server = await setup({ | ||
locales: ['en', 'es'], | ||
}); | ||
sinon.stub(parser, 'pick').throws('Error'); | ||
}); | ||
after(async () => { | ||
await server.stop(); | ||
parser.pick.restore(); | ||
}); | ||
it('should catch error and return default locale', () => | ||
server | ||
.inject({ | ||
url: '/test', | ||
headers: { | ||
'Accept-Language': 'en-GB;q=0.8,en-US;q=0.7,en;q=0.6', | ||
}, | ||
}) | ||
.should.be.fulfilled.then(response => | ||
expect(response.request.getLocale()).to.be.equal('en'))); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12220
8
273
77
14