|
| 1 | +import {ExpressKit, Request, Response} from '..'; |
| 2 | +import {NodeKit, USER_LANGUAGE_PARAM_NAME} from '@gravity-ui/nodekit'; |
| 3 | +import request from 'supertest'; |
| 4 | + |
| 5 | +const setupApp = (langConfig: NodeKit['config'] = {}) => { |
| 6 | + const nodekit = new NodeKit({ |
| 7 | + config: { |
| 8 | + appDefaultLang: 'ru', |
| 9 | + appAllowedLangs: ['ru', 'en'], |
| 10 | + ...langConfig, |
| 11 | + }, |
| 12 | + }); |
| 13 | + const routes = { |
| 14 | + 'GET /test': { |
| 15 | + handler: (req: Request, res: Response) => { |
| 16 | + res.status(200); |
| 17 | + const lang = req.ctx.get(USER_LANGUAGE_PARAM_NAME); |
| 18 | + res.send({lang}); |
| 19 | + }, |
| 20 | + }, |
| 21 | + }; |
| 22 | + |
| 23 | + const app = new ExpressKit(nodekit, routes); |
| 24 | + |
| 25 | + return app; |
| 26 | +}; |
| 27 | + |
| 28 | +describe('langMiddleware with default options', () => { |
| 29 | + it('should set default lang if no hostname or accept-language header', async () => { |
| 30 | + const app = setupApp(); |
| 31 | + const res = await request.agent(app.express).get('/test'); |
| 32 | + |
| 33 | + expect(res.text).toBe('{"lang":"ru"}'); |
| 34 | + expect(res.status).toBe(200); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should set lang for en domains by tld', async () => { |
| 38 | + const app = setupApp({appLangByTld: {com: 'en', ru: 'ru'}}); |
| 39 | + const res = await request.agent(app.express).host('www.foo.com').get('/test'); |
| 40 | + |
| 41 | + expect(res.text).toBe('{"lang":"en"}'); |
| 42 | + expect(res.status).toBe(200); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should set lang for ru domains by tld ', async () => { |
| 46 | + const app = setupApp({appLangByTld: {com: 'en', ru: 'ru'}}); |
| 47 | + const res = await request.agent(app.express).host('www.foo.ru').get('/test'); |
| 48 | + |
| 49 | + expect(res.text).toBe('{"lang":"ru"}'); |
| 50 | + expect(res.status).toBe(200); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should set default lang for other domains by tld ', async () => { |
| 54 | + const app = setupApp({appLangByTld: {com: 'en', ru: 'ru'}}); |
| 55 | + const res = await request.agent(app.express).host('www.foo.jp').get('/test'); |
| 56 | + |
| 57 | + expect(res.text).toBe('{"lang":"ru"}'); |
| 58 | + expect(res.status).toBe(200); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('langMiddleware with getLangByHostname is set', () => { |
| 63 | + it('should set lang by known hostname if getLangByHostname is set', async () => { |
| 64 | + const app = setupApp({ |
| 65 | + appGetLangByHostname: (hostname) => (hostname === 'www.foo.com' ? 'en' : undefined), |
| 66 | + }); |
| 67 | + const res = await request.agent(app.express).host('www.foo.com').get('/test'); |
| 68 | + |
| 69 | + expect(res.text).toBe('{"lang":"en"}'); |
| 70 | + expect(res.status).toBe(200); |
| 71 | + }); |
| 72 | + it("shouldn't set default lang for unknown hostname if getLangByHostname is set", async () => { |
| 73 | + const app = setupApp({ |
| 74 | + appGetLangByHostname: (hostname) => (hostname === 'www.foo.com' ? 'en' : undefined), |
| 75 | + }); |
| 76 | + const res = await request.agent(app.express).host('www.bar.com').get('/test'); |
| 77 | + |
| 78 | + expect(res.text).toBe('{"lang":"ru"}'); |
| 79 | + expect(res.status).toBe(200); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('langMiddleware with accept-language header', () => { |
| 84 | + it('should set lang if known accept-language', async () => { |
| 85 | + const app = setupApp({ |
| 86 | + appGetLangByHostname: (hostname) => (hostname === 'www.foo.com' ? 'en' : undefined), |
| 87 | + }); |
| 88 | + const res = await request |
| 89 | + .agent(app.express) |
| 90 | + .host('www.foo.com') |
| 91 | + .set('accept-language', 'ru-RU, ru;q=0.9, en-US;q=0.8, en;q=0.7, fr;q=0.6') |
| 92 | + .get('/test'); |
| 93 | + |
| 94 | + expect(res.text).toBe('{"lang":"ru"}'); |
| 95 | + expect(res.status).toBe(200); |
| 96 | + }); |
| 97 | + it('should set tld lang for unknown accept-language', async () => { |
| 98 | + const app = setupApp({ |
| 99 | + appGetLangByHostname: (hostname) => (hostname === 'www.foo.com' ? 'en' : undefined), |
| 100 | + }); |
| 101 | + const res = await request |
| 102 | + .agent(app.express) |
| 103 | + .host('www.foo.com') |
| 104 | + .set('accept-language', 'fr;q=0.6') |
| 105 | + .get('/test'); |
| 106 | + |
| 107 | + expect(res.text).toBe('{"lang":"en"}'); |
| 108 | + expect(res.status).toBe(200); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe('langMiddleware with lang query param', () => { |
| 113 | + it('should set lang if known accept-language', async () => { |
| 114 | + const app = setupApp({ |
| 115 | + appLangQueryParamName: '_lang', |
| 116 | + }); |
| 117 | + const res = await request |
| 118 | + .agent(app.express) |
| 119 | + .host('www.foo.com') |
| 120 | + .set('accept-language', 'ru-RU, ru;q=0.9, en-US;q=0.8, en;q=0.7, fr;q=0.6') |
| 121 | + .get('/test?_lang=en'); |
| 122 | + |
| 123 | + expect(res.text).toBe('{"lang":"en"}'); |
| 124 | + expect(res.status).toBe(200); |
| 125 | + }); |
| 126 | +}); |
0 commit comments