Skip to content

Commit

Permalink
fix(server): correct discrepancy between cookie lang and locale param
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Markus Enbuska committed Oct 15, 2019
1 parent bb6fd12 commit 31e04e7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 34 deletions.
33 changes: 26 additions & 7 deletions server/reittiopasParameterMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,24 @@ export function validateParams(req, config) {
return url;
}

export const dropLanguageAndRedirect = (req, res, lang) => {
const { path, query } = req;
const fixLocaleParam = (req, lang) => {
// override locale query param with the selected language
query.locale = lang === 'slangi' ? 'fi' : lang;
const trimmedPath = path.replace(`/${lang}/`, '/') + formatQuery(query);
res.redirect(trimmedPath);
req.query.locale = lang === 'slangi' ? 'fi' : lang;
return formatQuery(req.query);
};

export const dropPathLanguageAndFixLocaleParam = (req, lang) => {
return req.path.replace(`/${lang}/`, '/') + fixLocaleParam(req, lang);
};

const dropPathLanguageAndRedirect = (req, res, lang) => {
const trimmedUrl = dropPathLanguageAndFixLocaleParam(req, lang);
res.redirect(trimmedUrl);
};

const fixLocaleParamAndRedirect = (req, res, lang) => {
const fixedUrl = req.path + fixLocaleParam(req, lang);
res.redirect(fixedUrl);
};

export default function reittiopasParameterMiddleware(req, res, next) {
Expand All @@ -95,9 +107,16 @@ export default function reittiopasParameterMiddleware(req, res, next) {
) {
oldParamParser(req.query, config).then(url => res.redirect(url));
} else if (['fi', 'en', 'sv', 'ru', 'slangi'].includes(lang)) {
dropLanguageAndRedirect(req, res, lang);
dropPathLanguageAndRedirect(req, res, lang);
} else {
next();
const { locale } = req.query;
const cookieLang = req.cookies.lang;

if (cookieLang && locale && cookieLang !== locale) {
fixLocaleParamAndRedirect(req, res, cookieLang);
} else {
next();
}
}
} else {
next();
Expand Down
70 changes: 43 additions & 27 deletions test/unit/reittiopasParameterMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';
import {
validateParams,
langParamParser,
dropPathLanguageAndFixLocaleParam,
} from '../../server/reittiopasParameterMiddleware';

import config from '../../app/configurations/config.default';

const req = {
query: {
minTransferTime: '60',
modes: 'BUS,TRAM,RAIL,SUBWAY,FERRY,WALK,CITYBIKE',
transferPenalty: '0',
walkBoardCost: '540',
walkReluctance: '1.5',
walkSpeed: '1.5',
},
};

// validateParams returns an url if it is modified and it removes invalid
// parameteres from req.query => two ways to check if it did what it should

describe('reittiopasParameterMiddleware', () => {
describe('validateParams', () => {
const req = {
query: {
minTransferTime: '60',
modes: 'BUS,TRAM,RAIL,SUBWAY,FERRY,WALK,CITYBIKE',
transferPenalty: '0',
walkBoardCost: '540',
walkReluctance: '1.5',
walkSpeed: '1.5',
},
};

it('should not modify valid url', () => {
const url = validateParams(req, config);
expect(url).to.be.a('undefined');
Expand All @@ -44,28 +44,44 @@ describe('reittiopasParameterMiddleware', () => {
expect(req.query.modes).to.be.an('undefined');
});
});
describe('langParamParser', () => {
it('should return empty path', () => {
const path = '/en/';
const newPath = langParamParser(path);
expect(newPath).to.equal('/');

describe('dropLanguageAndSetLocaleParam', () => {
const req = {
path: '/en/',
query: {
locale: 'fi',
},
};

it('should return empty path with "locale" query param', () => {
const relativeUrl = dropPathLanguageAndFixLocaleParam(req, 'en');
expect(relativeUrl).to.equal('/?locale=en');
});

it('should return path without language parameter', () => {
const path =
it('should return path without language', () => {
req.path =
'/sv/reitti/Rautatientori%2C%20Helsinki%3A%3A60.171283%2C24.942572/Pasila%2C%20Helsinki%3A%3A60.199017%2C24.933973';
const newPath = langParamParser(path);
expect(newPath).to.equal(
'/reitti/Rautatientori%2C%20Helsinki%3A%3A60.171283%2C24.942572/Pasila%2C%20Helsinki%3A%3A60.199017%2C24.933973',
const relativeUrl = dropPathLanguageAndFixLocaleParam(req, 'sv');
expect(relativeUrl).to.equal(
'/reitti/Rautatientori%2C%20Helsinki%3A%3A60.171283%2C24.942572/Pasila%2C%20Helsinki%3A%3A60.199017%2C24.933973?locale=sv',
);
});

it('should not ignore URL parameters', () => {
const path =
'/en/reitti/Otaniemi,%20Espoo::60.187938,24.83182/Rautatientori,%20Asemanaukio%202,%20Helsinki::60.170384,24.939846?time=1565074800&arriveBy=false&utm_campaign=hsl.fi&utm_source=etusivu-reittihaku&utm_medium=referral';
const newPath = langParamParser(path);
expect(newPath).to.equal(
'/reitti/Otaniemi,%20Espoo::60.187938,24.83182/Rautatientori,%20Asemanaukio%202,%20Helsinki::60.170384,24.939846?time=1565074800&arriveBy=false&utm_campaign=hsl.fi&utm_source=etusivu-reittihaku&utm_medium=referral',
req.path =
'/en/reitti/Otaniemi,%20Espoo::60.187938,24.83182/Rautatientori,%20Asemanaukio%202,%20Helsinki::60.170384,24.939846';
req.query = {
time: 1565074800,
arriveBy: false,
utm_campaign: 'hsl.fi',
utm_source: 'etusivu-reittihaku',
utm_medium: 'referral',
locale: 'fi',
};

const relativeUrl = dropPathLanguageAndFixLocaleParam(req, 'en');
expect(relativeUrl).to.equal(
'/reitti/Otaniemi,%20Espoo::60.187938,24.83182/Rautatientori,%20Asemanaukio%202,%20Helsinki::60.170384,24.939846?time=1565074800&arriveBy=false&utm_campaign=hsl.fi&utm_source=etusivu-reittihaku&utm_medium=referral&locale=en',
);
});
});
Expand Down

0 comments on commit 31e04e7

Please sign in to comment.