Ghost/core/test/unit/middleware/cache-control_spec.js
Hannah Wolfe 61bf54ec88 🎉 Middleware refactor: Give the API its own express App (#7537)
refs #4172

* 🎨 Use bodyParser only where it is needed

This is a pretty extreme optimisation, however in the interests of killing middleware/index.js it
seemed prudent to move towards not having in there that wasn't strictly necessary 😁

We should reassess how apps do this sort of thing, but it seems pretty sane to declare bodyParsing
if and only if it is necessary.

* 🎨 Move all API code to API router

* 🎨 Refactor API into an App, not just a router

- Apps have their own rendering engines, only the frontend & the admin panel need views
- The API should be JSON only, with minimal middleware
- Individual sections within the API could/should be treated as Routers

* 🎨 Flatten API middleware inclusion

- get rid of the weird middleware object
- move the api-only middleware into the middleware/api folder
2016-10-11 10:36:00 +02:00

93 lines
3.1 KiB
JavaScript

var should = require('should'),
sinon = require('sinon'),
cacheControl = require('../../../server/middleware/cache-control');
describe('Middleware: cacheControl', function () {
var sandbox,
res;
beforeEach(function () {
sandbox = sinon.sandbox.create();
res = {
set: sinon.spy()
};
});
afterEach(function () {
sandbox.restore();
});
it('correctly sets the public profile headers', function (done) {
cacheControl('public')(null, res, function (a) {
should.not.exist(a);
res.set.calledOnce.should.be.true();
res.set.calledWith({'Cache-Control': 'public, max-age=0'});
done();
});
});
it('correctly sets the private profile headers', function (done) {
cacheControl('private')(null, res, function (a) {
should.not.exist(a);
res.set.calledOnce.should.be.true();
res.set.calledWith({
'Cache-Control':
'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
done();
});
});
it('will not set headers without a profile', function (done) {
cacheControl()(null, res, function (a) {
should.not.exist(a);
res.set.called.should.be.false();
done();
});
});
it('will not get confused between serving public and private', function (done) {
var publicCC = cacheControl('public'),
privateCC = cacheControl('private');
publicCC(null, res, function () {
res.set.calledOnce.should.be.true();
res.set.calledWith({'Cache-Control': 'public, max-age=0'});
privateCC(null, res, function () {
res.set.calledTwice.should.be.true();
res.set.calledWith({
'Cache-Control':
'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
publicCC(null, res, function () {
res.set.calledThrice.should.be.true();
res.set.calledWith({'Cache-Control': 'public, max-age=0'});
privateCC(null, res, function () {
res.set.calledWith({
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
done();
});
});
});
});
});
it('will override public with private for private blogs', function (done) {
res.isPrivateBlog = true;
cacheControl('public')(null, res, function (a) {
should.not.exist(a);
res.set.calledOnce.should.be.true();
res.set.calledWith({
'Cache-Control':
'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
done();
});
});
});