From 3f4d13577bbca02e3b8d4bdd156a7854a95c7f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Sun, 15 Mar 2020 19:39:38 +0100 Subject: [PATCH] fix: add tests for forwardedHost change, deprecate setting instead of removing --- src/index.js | 5 +++++ test/module.test.js | 28 +++++++++++++++++++++++++--- types/nuxt-i18n.d.ts | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 4977e88b2..e53262c39 100644 --- a/src/index.js +++ b/src/index.js @@ -78,6 +78,11 @@ module.exports = function (userOptions) { console.warn('[' + options.MODULE_NAME + '] The `differentDomains` option and `no_prefix` strategy are not compatible. Change strategy or disable `differentDomains` option.') } + if ('forwardedHost' in options) { + // eslint-disable-next-line no-console + console.warn('[' + options.MODULE_NAME + '] The `forwardedHost` option is deprecated. You can safely remove it. See: https://github.com/nuxt-community/nuxt-i18n/pull/630.') + } + // Plugins for (const file of requiredPlugins) { this.addPlugin({ diff --git a/test/module.test.js b/test/module.test.js index 8dfca4ed1..cb93bf6b3 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -812,7 +812,7 @@ describe('differentDomains enabled', () => { await nuxt.close() }) - test('matches domain\'s locale (en)', async () => { + test('host matches locale\'s domain (en)', async () => { const requestOptions = { headers: { Host: 'en.nuxt-app.localhost' @@ -824,7 +824,7 @@ describe('differentDomains enabled', () => { await expect(dom.querySelector('head meta[property="og-locale"]')).toBe(null) }) - test('matches domain\'s locale (fr)', async () => { + test('host matches locale\'s domain (fr)', async () => { const requestOptions = { headers: { Host: 'fr.nuxt-app.localhost' @@ -833,7 +833,29 @@ describe('differentDomains enabled', () => { const html = await get('/', requestOptions) const dom = getDom(html) await expect(dom.querySelector('body').textContent).toContain('page: Accueil') - await expect(dom.querySelector('head meta[property="og-locale"]')).toBe(null) + }) + + test('x-forwarded-host does not match locale\'s domain', async () => { + const requestOptions = { + headers: { + 'X-Forwarded-Host': 'xx.nuxt-app.localhost' + } + } + const html = await get('/', requestOptions) + const dom = getDom(html) + // Falls back to english. + await expect(dom.querySelector('body').textContent).toContain('page: Homepage') + }) + + test('x-forwarded-host does match locale\'s domain (fr)', async () => { + const requestOptions = { + headers: { + 'X-Forwarded-Host': 'fr.nuxt-app.localhost' + } + } + const html = await get('/', requestOptions) + const dom = getDom(html) + await expect(dom.querySelector('body').textContent).toContain('page: Accueil') }) }) diff --git a/types/nuxt-i18n.d.ts b/types/nuxt-i18n.d.ts index 29fdaf597..c9f8eca03 100644 --- a/types/nuxt-i18n.d.ts +++ b/types/nuxt-i18n.d.ts @@ -46,6 +46,7 @@ declare namespace NuxtVueI18n { defaultLocale?: null | Locale locales?: Array differentDomains?: boolean + forwardedHost?: boolean onLanguageSwitched?: (oldLocale: string, newLocale: string) => void }