Skip to content

Commit

Permalink
updated normalize filter and added a test
Browse files Browse the repository at this point in the history
Updated lib/normalize_title_filter.js to better follow format of lib/mwUtil.js

Modified lib/mwUtil.js to use {term} if present

Removed previous wiktionary test and replaced with one test that checks filter
  • Loading branch information
clarakosi committed Oct 26, 2018
1 parent 3fedd1b commit 259dd87
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 44 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
6 changes: 4 additions & 2 deletions lib/normalize_title_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (hyper, req, next, options, specInfo) => {
}
});
}
rp.title = rp[options.title] ? rp[options.title] : rp.title;
rp.title = (options.title && rp[options.title]) || rp.title;
if (!rp.title) {
return next(hyper, req);
}
Expand Down Expand Up @@ -59,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
44 changes: 3 additions & 41 deletions test/features/pagecontent/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,14 @@ describe('redirects', () => {
before(() => { return server.start(); });

describe('', () => {
it('should direct to a lowercase version of a title in wiktionary', () => {
return preq.get({
uri: `${server.config.wiktionaryURI}/page/definition/cat`,
followRedirect: false
})
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.headers['content-location'],
'https://en.wiktionary.org/api/rest_v1/page/definition/cat');
assert.deepEqual(res.headers['cache-control'], 'test_purged_cache_control');
});
});

it('should direct to an uppercase version of a title in wiktionary', () => {
return preq.get({
uri: `${server.config.wiktionaryURI}/page/definition/Cat`,
followRedirect: false
})
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.headers['content-location'],
'https://en.wiktionary.org/api/rest_v1/page/definition/Cat');
assert.deepEqual(res.headers['cache-control'], 'test_purged_cache_control');
});
});

it('should redirect to a normalized version of a title in wiktionary', () => {
return preq.get({
uri: `${server.config.wiktionaryURI}/page/definition/Appendix%20Glossary`,
followRedirect: false
})
.then((res) => {
assert.deepEqual(res.status, 301);
assert.deepEqual(res.headers.location,
'../../../en.wiktionary.org/v1/page/definition/Appendix%20Glossary');
assert.deepEqual(res.headers['cache-control'], 'test_purged_cache_control');
});
});
it('should redirect to a normalized version of a title in wiktionary, #2', () => {
return preq.get({
uri: `${server.config.wiktionaryURI}/page/definition/Wiktionary%20ELE`,
uri: `${server.config.wiktionaryURI}/page/definition/weekend%20warrior`,
followRedirect: false
})
.then((res) => {
assert.deepEqual(res.status, 301);
assert.deepEqual(res.headers.location,
'../../../en.wiktionary.org/v1/page/definition/Wiktionary%20ELE');
assert.deepEqual(res.headers.location, 'weekend_warrior');
assert.deepEqual(res.headers['cache-control'], 'test_purged_cache_control');
});
});
Expand All @@ -66,6 +27,7 @@ describe('redirects', () => {
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

0 comments on commit 259dd87

Please sign in to comment.