0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Merge pull request #5420 from hex337/move-uncapitalize

Move uncapitalize into its own file.
This commit is contained in:
Hannah Wolfe 2015-06-11 23:41:32 +01:00
commit 464c16f2aa
3 changed files with 137 additions and 23 deletions

View file

@ -24,6 +24,7 @@ var api = require('../api'),
utils = require('../utils'),
sitemapHandler = require('../data/xml/sitemap/handler'),
decideIsAdmin = require('./decide-is-admin'),
uncapitalise = require('./uncapitalise'),
blogApp,
setupMiddleware;
@ -152,29 +153,6 @@ function redirectToSetup(req, res, next) {
});
}
// Detect uppercase in req.path
function uncapitalise(req, res, next) {
var pathToTest = req.path,
isSignupOrReset = req.path.match(/(\/ghost\/(signup|reset)\/)/i),
isAPI = req.path.match(/(\/ghost\/api\/v[\d\.]+\/.*?\/)/i);
if (isSignupOrReset) {
pathToTest = isSignupOrReset[1];
}
// Do not lowercase anything after /api/v0.1/ to protect :key/:slug
if (isAPI) {
pathToTest = isAPI[1];
}
if (/[A-Z]/.test(pathToTest)) {
res.set('Cache-Control', 'public, max-age=' + utils.ONE_YEAR_S);
res.redirect(301, req.url.replace(pathToTest, pathToTest.toLowerCase()));
} else {
next();
}
}
// ### ServeSharedFile Middleware
// Handles requests to robots.txt and favicon.ico (and caches them)
function serveSharedFile(file, type, maxAge) {

View file

@ -0,0 +1,35 @@
// # uncapitalise Middleware
// Usage: uncapitalise(req, res, next)
// After:
// Before:
// App: Admin|Blog|API
//
// Detect upper case in req.path.
var utils = require('../utils'),
uncapitalise;
uncapitalise = function uncapitalise(req, res, next) {
/*jslint unparam:true*/
var pathToTest = req.path,
isSignupOrReset = req.path.match(/(\/ghost\/(signup|reset)\/)/i),
isAPI = req.path.match(/(\/ghost\/api\/v[\d\.]+\/.*?\/)/i);
if (isSignupOrReset) {
pathToTest = isSignupOrReset[1];
}
// Do not lowercase anything after /api/v0.1/ to protect :key/:slug
if (isAPI) {
pathToTest = isAPI[1];
}
if (/[A-Z]/.test(pathToTest)) {
res.set('Cache-Control', 'public, max-age=' + utils.ONE_YEAR_S);
res.redirect(301, req.url.replace(pathToTest, pathToTest.toLowerCase()));
} else {
next();
}
};
module.exports = uncapitalise;

View file

@ -0,0 +1,101 @@
/*globals describe, beforeEach, afterEach, it*/
/*jshint expr:true*/
var sinon = require('sinon'),
uncapitalise = require('../../../server/middleware/uncapitalise');
describe('Middleware: uncapitalise', function () {
var sandbox,
res,
req,
next;
beforeEach(function () {
sandbox = sinon.sandbox.create();
res = sinon.spy();
req = sinon.spy();
next = sinon.spy();
});
afterEach(function () {
sandbox.restore();
});
describe('A signup or reset request', function () {
it('does nothing if there are no capital letters', function (done) {
req.path = '/ghost/signup';
uncapitalise(req, res, next);
next.should.be.calledOnce;
done();
});
it('redirects to the lower case slug if there are capital letters', function (done) {
req.path = '/ghost/SignUP';
req.url = 'http://localhost' + req.path;
res = {
redirect: sinon.spy(),
set: sinon.spy()
};
uncapitalise(req, res, next);
next.should.not.be.called;
res.redirect.should.be.calledOnce;
res.redirect.calledWith(301, 'http://localhost/ghost/signup').should.be.true;
done();
});
});
describe('An API request', function () {
it('does nothing if there are no capital letters', function (done) {
req.path = '/ghost/api/v0.1';
uncapitalise(req, res, next);
next.should.be.calledOnce;
done();
});
it('redirects to the lower case slug if there are capital letters', function (done) {
req.path = '/ghost/api/v0.1/ASDfJ';
req.url = 'http://localhost' + req.path;
res = {
redirect: sinon.spy(),
set: sinon.spy()
};
uncapitalise(req, res, next);
next.should.not.be.called;
res.redirect.should.be.calledOnce;
res.redirect.calledWith(301, 'http://localhost/ghost/api/v0.1/asdfj').should.be.true;
done();
});
});
describe('Any other request', function () {
it('does nothing if there are no capital letters', function (done) {
req.path = '/this-is-my-blog-post';
uncapitalise(req, res, next);
next.should.be.calledOnce;
done();
});
it('redirects to the lower case slug if there are capital letters', function (done) {
req.path = '/THis-iS-my-BLOg-poSt';
req.url = 'http://localhost' + req.path;
res = {
redirect: sinon.spy(),
set: sinon.spy()
};
uncapitalise(req, res, next);
next.should.not.be.called;
res.redirect.should.be.calledOnce;
res.redirect.calledWith(301, 'http://localhost/this-is-my-blog-post').should.be.true;
done();
});
});
});