Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #70 from Wikia/XW-3247
Browse files Browse the repository at this point in the history
XW-3247 | Handle externaltest and showcase subdomains
  • Loading branch information
kvas-damian committed Apr 12, 2017
1 parent 7b7515b commit 7cf61b2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 40 deletions.
5 changes: 3 additions & 2 deletions app/routes/application.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions app/utils/domain.js
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
19 changes: 19 additions & 0 deletions app/utils/host.js
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 0 additions & 28 deletions tests/unit/utils/domain-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/utils/host-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 7cf61b2

Please sign in to comment.