From cede8446ae7835cbcbb90aab299eb65131e43f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20J=C3=B3=C5=BAwiak?= Date: Wed, 12 Apr 2017 15:04:37 +0200 Subject: [PATCH] Merge pull request #70 from Wikia/XW-3247 XW-3247 | Handle externaltest and showcase subdomains --- app/routes/application.js | 5 ++-- app/utils/domain.js | 10 ------- app/utils/host.js | 19 +++++++++++++ tests/unit/utils/domain-test.js | 28 ------------------- tests/unit/utils/host-test.js | 48 +++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 40 deletions(-) create mode 100644 app/utils/host.js create mode 100644 tests/unit/utils/host-test.js diff --git a/app/routes/application.js b/app/routes/application.js index 33da9080371..c8979f296d8 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -1,8 +1,9 @@ import Ember from 'ember'; import ArticleModel from '../models/wiki/article'; import WikiVariablesModel from '../models/wiki-variables'; -import getLinkInfo from '../utils/article-link'; import HeadTagsStaticMixin from '../mixins/head-tags-static'; +import getHostFromRequest from '../utils/host'; +import getLinkInfo from '../utils/article-link'; import {normalizeToUnderscore} from '../utils/string'; import {track, trackActions} from '../utils/track'; import {getQueryString} from '../utils/url'; @@ -47,7 +48,7 @@ export default Route.extend( if (this.get('fastboot.isFastBoot')) { const request = this.get('fastboot.request'); - return WikiVariablesModel.get(request.get('headers').get('x-original-host') || request.get('host')) + return WikiVariablesModel.get(getHostFromRequest(request)) .then((wikiVariablesModel) => { shoebox.put('wikiVariables', wikiVariablesModel); this.get('wikiVariables').setProperties(wikiVariablesModel); diff --git a/app/utils/domain.js b/app/utils/domain.js index 0d9a88f2bab..f2824237efb 100644 --- a/app/utils/domain.js +++ b/app/utils/domain.js @@ -1,15 +1,5 @@ import Ember from 'ember'; -/** - * @param {string} [hostname=window.location.hostname] - * @returns {string} - */ -export function getDomain(hostname = window.location.hostname) { - const domain = (/[^.]+\.[^.]+$/).exec(hostname); - - return Ember.isArray(domain) ? domain[0] : hostname; -} - /** * @param {string} url * @returns {string} diff --git a/app/utils/host.js b/app/utils/host.js new file mode 100644 index 00000000000..082ac8e79ea --- /dev/null +++ b/app/utils/host.js @@ -0,0 +1,19 @@ +/** + * @param {Object} request - FastBoot request + * @returns {string} + */ +export default function getHostFromRequest(request) { + // We use two special domain prefixes for Ad Operations and Sales reasons + // Their purpose is to allow separate targeting by having a different domain in the browser + // We still want to call production API with non-prefixed host + // See https://github.com/Wikia/wikia-vcl/blob/master/wikia.com/control-stage.vcl + const headers = request.get('headers'); + // One of our layers cuts out sandbox-* prefix from the host, use x-original-host instead + let host = headers.get('x-original-host') || request.get('host'); + + if (headers.get('x-staging') === 'externaltest') { + host = host.replace(/^(externaltest|showcase)\./, ''); + } + + return host; +} diff --git a/tests/unit/utils/domain-test.js b/tests/unit/utils/domain-test.js index 3d6ca09c3a5..aa69019f8c0 100644 --- a/tests/unit/utils/domain-test.js +++ b/tests/unit/utils/domain-test.js @@ -2,34 +2,6 @@ import {module} from 'qunit'; import {test} from 'ember-qunit'; module('Unit | Utility | domain', () => { - test('gets domain from provided hosts', (assert) => { - const testCasesForGetDomain = [ - { - hostname: 'witcher.wikia.com', - expected: 'wikia.com', - }, - { - hostname: 'fallout.warkot.wikia-dev.pl', - expected: 'wikia-dev.pl', - }, - { - hostname: 'fallout.wikia-staging.com', - expected: 'wikia-staging.com', - }, - { - hostname: 'no-dots-here', - expected: 'no-dots-here', - }, - { - expected: window.location.hostname, - }, - ]; - - testCasesForGetDomain.forEach((testCase) => { - assert.strictEqual(require('mobile-wiki/utils/domain').getDomain(testCase.hostname), testCase.expected); - }); - }); - test('extracts domain from provided urls', (assert) => { const testCasesForExtractDomainFromUrl = [ { diff --git a/tests/unit/utils/host-test.js b/tests/unit/utils/host-test.js new file mode 100644 index 00000000000..e07e33f843d --- /dev/null +++ b/tests/unit/utils/host-test.js @@ -0,0 +1,48 @@ +import {module} from 'qunit'; +import {test} from 'ember-qunit'; + +module('Unit | Utility | host', () => { + test('returns correct host', (assert) => { + const testCases = [ + { + headers: {}, + host: 'starwars.wikia.com', + expected: 'starwars.wikia.com', + }, + { + headers: { + 'x-original-host': 'sandbox-xw1.starwars.wikia.com' + }, + host: 'starwars.wikia.com', + expected: 'sandbox-xw1.starwars.wikia.com', + }, + { + headers: { + 'x-original-host': 'externaltest.starwars.wikia.com', + 'x-staging': 'externaltest' + }, + host: 'starwars.wikia.com', + expected: 'starwars.wikia.com', + }, + { + headers: { + 'x-original-host': 'starwars.wikia.com', + 'x-staging': 'externaltest' + }, + host: 'showcase.starwars.wikia.com', + expected: 'starwars.wikia.com', + }, + ]; + + testCases.forEach((testCase) => { + const request = Ember.Object.create(); + + request.setProperties({ + headers: Ember.Object.create(testCase.headers), + host: testCase.host + }); + + assert.strictEqual(require('mobile-wiki/utils/host').default(request), testCase.expected); + }); + }); +});