0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Uncapitalise respects subdirectories & no encoding

no issue

- Uncapitalise was dropping the subdirectory when redirecting - so the base url has been added where present
- Uncapitalise was also working differently in node 0.10 and 0.12 - so the path is decoded before testing for uppercase
- Adds some test coverage
This commit is contained in:
Hannah Wolfe 2015-09-24 14:40:48 +01:00
parent 07e18cbdff
commit 8895f41ee2
3 changed files with 32 additions and 2 deletions

View file

@ -24,9 +24,14 @@ uncapitalise = function uncapitalise(req, res, next) {
pathToTest = isAPI[1];
}
if (/[A-Z]/.test(pathToTest)) {
/**
* In node < 0.11.1 req.path is not encoded, afterwards, it is always encoded such that | becomes %7C etc.
* That encoding isn't useful here, as it triggers an extra uncapitalise redirect, so we decode the path first
*/
if (/[A-Z]/.test(decodeURIComponent(pathToTest))) {
res.set('Cache-Control', 'public, max-age=' + utils.ONE_YEAR_S);
res.redirect(301, req.url.replace(pathToTest, pathToTest.toLowerCase()));
// Adding baseUrl ensures subdirectories are kept
res.redirect(301, (req.baseUrl ? req.baseUrl : '') + req.url.replace(pathToTest, pathToTest.toLowerCase()));
} else {
next();
}

View file

@ -94,6 +94,14 @@ describe('Frontend Routing', function () {
.expect(/Page not found/)
.end(doEnd(done));
});
it('should 404 for encoded char not 301 from uncapitalise', function (done) {
request.get('/|/')
.expect('Cache-Control', testUtils.cacheRules['private'])
.expect(404)
.expect(/Page not found/)
.end(doEnd(done));
});
});
describe('Home', function () {
@ -146,6 +154,7 @@ describe('Frontend Routing', function () {
it('should redirect uppercase', function (done) {
request.get('/Welcome-To-Ghost/')
.expect('Location', '/welcome-to-ghost/')
.expect('Cache-Control', testUtils.cacheRules.year)
.expect(301)
.end(doEnd(done));
});
@ -321,6 +330,7 @@ describe('Frontend Routing', function () {
request.get('/p/2ac6b4f6-e1f3-406c-9247-c94a0496d39d/')
.expect(301)
.expect('Location', '/short-and-sweet/')
.expect('Cache-Control', testUtils.cacheRules.public)
.end(doEnd(done));
});
@ -763,6 +773,7 @@ describe('Frontend Routing', function () {
request.get('/blog/welcome-to-ghost')
.expect(301)
.expect('Location', '/blog/welcome-to-ghost/')
.expect('Cache-Control', testUtils.cacheRules.year)
.end(doEnd(done));
});
@ -776,6 +787,7 @@ describe('Frontend Routing', function () {
request.get('/blog/tag/getting-started')
.expect(301)
.expect('Location', '/blog/tag/getting-started/')
.expect('Cache-Control', testUtils.cacheRules.year)
.end(doEnd(done));
});
@ -837,6 +849,7 @@ describe('Frontend Routing', function () {
request.get('/blog/welcome-to-ghost')
.expect(301)
.expect('Location', '/blog/welcome-to-ghost/')
.expect('Cache-Control', testUtils.cacheRules.year)
.end(doEnd(done));
});
@ -850,6 +863,7 @@ describe('Frontend Routing', function () {
request.get('/blog/tag/getting-started')
.expect(301)
.expect('Location', '/blog/tag/getting-started/')
.expect('Cache-Control', testUtils.cacheRules.year)
.end(doEnd(done));
});
@ -858,6 +872,14 @@ describe('Frontend Routing', function () {
.expect(200)
.end(doEnd(done));
});
it('should uncapitalise correctly with 301 to subdir', function (done) {
request.get('/blog/AAA/')
.expect('Location', '/blog/aaa/')
.expect('Cache-Control', testUtils.cacheRules.year)
.expect(301)
.end(doEnd(done));
});
});
// we'll use X-Forwarded-Proto: https to simulate an 'https://' request behind a proxy

View file

@ -1,8 +1,11 @@
/*globals describe, beforeEach, afterEach, it*/
/*jshint expr:true*/
var sinon = require('sinon'),
should = require('should'),
uncapitalise = require('../../../server/middleware/uncapitalise');
should.equal(true, true);
describe('Middleware: uncapitalise', function () {
var sandbox,
res,