[MediaWiki-commits] [Gerrit] mediawiki...parsoid[master]: T169006: Preserve request method when redirecting to oldid
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/395880 ) Change subject: T169006: Preserve request method when redirecting to oldid .. T169006: Preserve request method when redirecting to oldid Change-Id: Ifab6e8d003a5e2afa11ca4077d2beafb684ca1a5 --- M lib/api/apiUtils.js M tests/mocha/api.js 2 files changed, 42 insertions(+), 33 deletions(-) Approvals: Subramanya Sastry: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/api/apiUtils.js b/lib/api/apiUtils.js index bdc85d2..d2a5b13 100644 --- a/lib/api/apiUtils.js +++ b/lib/api/apiUtils.js @@ -369,7 +369,7 @@ }); }; -apiUtils._redirect = function(req, res, target, processRedirect) { +apiUtils._redirect = function(req, res, target, httpStatus, processRedirect) { var locals = res.locals; var path = processRedirect([ '', @@ -382,7 +382,7 @@ // Don't cache redirect requests apiUtils.setHeader(res, 'Cache-Control', 'private,no-cache,s-maxage=0'); - apiUtils.relativeRedirect(res, path); + apiUtils.relativeRedirect(res, path, httpStatus); }; /** @@ -392,26 +392,24 @@ */ apiUtils.redirectToOldid = function(req, res) { var env = res.locals.env; - return this._redirect( - req, - res, - env.normalizeAndResolvePageTitle(), - function(redirPath) { - var revid = env.page.meta.revision.revid; - redirPath += '/' + revid; - if (Object.keys(req.query).length > 0) { - redirPath += '?' + qs.stringify(req.query); - } - var format = res.locals.opts.format; - env.log('info', 'redirecting to revision', revid, 'for', format); - var metrics = env.conf.parsoid.metrics; - if (metrics) { - metrics.increment('redirectToOldid.' + format.toLowerCase()); - } - return redirPath; + var target = env.normalizeAndResolvePageTitle(); + // Preserve the request method since we currently don't allow GETing the + // "lint" format. See T169006 + var httpStatus = (req.method === 'GET') ? 302 : 307; + return this._redirect(req, res, target, httpStatus, function(redirPath) { + var revid = env.page.meta.revision.revid; + redirPath += '/' + revid; + if (Object.keys(req.query).length > 0) { + redirPath += '?' + qs.stringify(req.query); } - ); - + var format = res.locals.opts.format; + env.log('info', 'redirecting to revision', revid, 'for', format); + var metrics = env.conf.parsoid.metrics; + if (metrics) { + metrics.increment('redirectToOldid.' + format.toLowerCase()); + } + return redirPath; + }); }; /** @@ -421,15 +419,10 @@ * @param {Response} res */ apiUtils._redirectToPage = function(title, req, res) { - return this._redirect( - req, - res, - title, - function(path) { - res.locals.env.log('info', 'redirecting to ', path); - return path; - } - ); + return this._redirect(req, res, title, undefined, function(path) { + res.locals.env.log('info', 'redirecting to ', path); + return path; + }); }; /** diff --git a/tests/mocha/api.js b/tests/mocha/api.js index 7965320..46a8df0 100644 --- a/tests/mocha/api.js +++ b/tests/mocha/api.js @@ -466,6 +466,22 @@ .end(done); }); + it('should redirect title to latest revision (lint)', function(done) { + request(api) + .post(mockDomain + '/v3/transform/wikitext/to/lint/') + .send({ + original: { + title: 'Lint_Page', + }, + }) + .expect(307) // no revid or wikitext source provided + .expect(function(res) { + res.headers.should.have.property('location'); + res.headers.location.should.equal('/' + mockDomain + '/v3/page/lint/Lint_Page/102'); + }) + .end(done); + }); + }); describe("wt2html", function() { @@ -648,7 +664,7 @@ title: 'Main_Page', }, }) - .expect(302) // no revid
[MediaWiki-commits] [Gerrit] mediawiki...parsoid[master]: T169006: Preserve request method when redirecting to oldid
Arlolra has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/395880 ) Change subject: T169006: Preserve request method when redirecting to oldid .. T169006: Preserve request method when redirecting to oldid Change-Id: Ifab6e8d003a5e2afa11ca4077d2beafb684ca1a5 --- M lib/api/apiUtils.js M tests/mocha/api.js 2 files changed, 42 insertions(+), 33 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/80/395880/1 diff --git a/lib/api/apiUtils.js b/lib/api/apiUtils.js index bdc85d2..d2a5b13 100644 --- a/lib/api/apiUtils.js +++ b/lib/api/apiUtils.js @@ -369,7 +369,7 @@ }); }; -apiUtils._redirect = function(req, res, target, processRedirect) { +apiUtils._redirect = function(req, res, target, httpStatus, processRedirect) { var locals = res.locals; var path = processRedirect([ '', @@ -382,7 +382,7 @@ // Don't cache redirect requests apiUtils.setHeader(res, 'Cache-Control', 'private,no-cache,s-maxage=0'); - apiUtils.relativeRedirect(res, path); + apiUtils.relativeRedirect(res, path, httpStatus); }; /** @@ -392,26 +392,24 @@ */ apiUtils.redirectToOldid = function(req, res) { var env = res.locals.env; - return this._redirect( - req, - res, - env.normalizeAndResolvePageTitle(), - function(redirPath) { - var revid = env.page.meta.revision.revid; - redirPath += '/' + revid; - if (Object.keys(req.query).length > 0) { - redirPath += '?' + qs.stringify(req.query); - } - var format = res.locals.opts.format; - env.log('info', 'redirecting to revision', revid, 'for', format); - var metrics = env.conf.parsoid.metrics; - if (metrics) { - metrics.increment('redirectToOldid.' + format.toLowerCase()); - } - return redirPath; + var target = env.normalizeAndResolvePageTitle(); + // Preserve the request method since we currently don't allow GETing the + // "lint" format. See T169006 + var httpStatus = (req.method === 'GET') ? 302 : 307; + return this._redirect(req, res, target, httpStatus, function(redirPath) { + var revid = env.page.meta.revision.revid; + redirPath += '/' + revid; + if (Object.keys(req.query).length > 0) { + redirPath += '?' + qs.stringify(req.query); } - ); - + var format = res.locals.opts.format; + env.log('info', 'redirecting to revision', revid, 'for', format); + var metrics = env.conf.parsoid.metrics; + if (metrics) { + metrics.increment('redirectToOldid.' + format.toLowerCase()); + } + return redirPath; + }); }; /** @@ -421,15 +419,10 @@ * @param {Response} res */ apiUtils._redirectToPage = function(title, req, res) { - return this._redirect( - req, - res, - title, - function(path) { - res.locals.env.log('info', 'redirecting to ', path); - return path; - } - ); + return this._redirect(req, res, title, undefined, function(path) { + res.locals.env.log('info', 'redirecting to ', path); + return path; + }); }; /** diff --git a/tests/mocha/api.js b/tests/mocha/api.js index 7965320..46a8df0 100644 --- a/tests/mocha/api.js +++ b/tests/mocha/api.js @@ -466,6 +466,22 @@ .end(done); }); + it('should redirect title to latest revision (lint)', function(done) { + request(api) + .post(mockDomain + '/v3/transform/wikitext/to/lint/') + .send({ + original: { + title: 'Lint_Page', + }, + }) + .expect(307) // no revid or wikitext source provided + .expect(function(res) { + res.headers.should.have.property('location'); + res.headers.location.should.equal('/' + mockDomain + '/v3/page/lint/Lint_Page/102'); + }) + .end(done); + }); + }); describe("wt2html", function() { @@ -648,7 +664,7 @@ title: 'Main_Page', }, }) - .expect(302) // no revid