Skip to content

Commit

Permalink
Merge 259dd87 into 02f3cdf
Browse files Browse the repository at this point in the history
  • Loading branch information
clarakosi committed Oct 26, 2018
2 parents 02f3cdf + 259dd87 commit b267146
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/mwUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ mwUtil.createRelativeTitleRedirect = (path, req, options) => {
const backString = Array.apply(null, { length: pathSuffixCount }).map(() => '../').join('');
let location;
if (!options.dropPathAfterTitle) {
const pathPatternAfterTitle = path.substring(path.indexOf('{title}') - 1);
let pathPatternAfterTitle = path.substring(path.indexOf(`{${titleParamName}`) - 1);
pathPatternAfterTitle = pathPatternAfterTitle.replace(/term/, 'title');
location = backString
+ new URI(pathPatternAfterTitle, newReqParams, true).toString().substr(1)
+ mwUtil.getQueryString(req);
Expand Down
5 changes: 4 additions & 1 deletion lib/normalize_title_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = (hyper, req, next, options, specInfo) => {
}
});
}
rp.title = (options.title && rp[options.title]) || rp.title;
if (!rp.title) {
return next(hyper, req);
}
Expand Down Expand Up @@ -58,7 +59,9 @@ module.exports = (hyper, req, next, options, specInfo) => {
return P.resolve({
status: 301,
headers: {
location: mwUtil.createRelativeTitleRedirect(specInfo.path, req),
location: mwUtil.createRelativeTitleRedirect(specInfo.path, req, {
titleParamName: options.title
}),
'cache-control': options.redirect_cache_control || 'no-cache'
}
});
Expand Down
13 changes: 13 additions & 0 deletions test/features/pagecontent/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ describe('redirects', () => {
before(() => { return server.start(); });

describe('', () => {
it('should redirect to a normalized version of a title in wiktionary', () => {
return preq.get({
uri: `${server.config.wiktionaryURI}/page/definition/weekend%20warrior`,
followRedirect: false
})
.then((res) => {
assert.deepEqual(res.status, 301);
assert.deepEqual(res.headers.location, 'weekend_warrior');
assert.deepEqual(res.headers['cache-control'], 'test_purged_cache_control');
});
});

it('should redirect to a normalized version of a title', () => {
return preq.get({
uri: `${server.config.bucketURL}/html/Main%20Page?test=mwAQ`,
followRedirect: false
})
.then((res) => {
console.log(res);
assert.deepEqual(res.status, 301);
assert.deepEqual(res.headers.location, 'Main_Page?test=mwAQ');
assert.deepEqual(res.headers['cache-control'], 'test_purged_cache_control');
Expand Down
4 changes: 3 additions & 1 deletion test/utils/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const labsBucketURL = `${labsURL}/page`;
const variantsWikiURL = `${hostPort}/sr.wikipedia.beta.wmflabs.org/v1`;
const variantsWikiBucketURL = `${variantsWikiURL}/page`;
const parsoidURI = 'https://parsoid-beta.wmflabs.org';
const wiktionaryURI = `${hostPort}/en.wiktionary.org/v1`;

function loadConfig(path, forceSqlite) {
let confString = fs.readFileSync(path).toString();
Expand Down Expand Up @@ -54,7 +55,8 @@ const config = {
labsApiURL: 'https://en.wikipedia.beta.wmflabs.org/w/api.php',
logStream: logStream(),
conf: loadConfig(process.env.RB_TEST_CONFIG ? process.env.RB_TEST_CONFIG : `${__dirname}/../../config.test.yaml`),
parsoidURI
parsoidURI,
wiktionaryURI
};

config.conf.num_workers = 0;
Expand Down
1 change: 1 addition & 0 deletions v1/definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ paths:
/definition/{term}:
x-route-filters:
- path: ./lib/access_check_filter.js
- path: ./lib/normalize_title_filter.js
options:
title: term
redirect_cache_control: '{{options.response_cache_control}}'
Expand Down

0 comments on commit b267146

Please sign in to comment.