2020-04-25 21:06:33 +01:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const cacheControl = require('../../../../../core/server/web/shared/middlewares/cache-control');
|
2015-05-18 19:12:42 +01:00
|
|
|
|
|
|
|
describe('Middleware: cacheControl', function () {
|
2020-04-25 21:06:33 +01:00
|
|
|
let res;
|
2015-05-18 19:12:42 +01:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
res = {
|
2019-01-21 17:53:44 +01:00
|
|
|
set: sinon.spy()
|
2015-05-18 19:12:42 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 17:53:44 +01:00
|
|
|
sinon.restore();
|
2015-05-18 19:12:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly sets the public profile headers', function (done) {
|
2016-10-11 09:36:00 +01:00
|
|
|
cacheControl('public')(null, res, function (a) {
|
2015-05-18 19:12:42 +01:00
|
|
|
should.not.exist(a);
|
2016-02-07 21:27:01 +00:00
|
|
|
res.set.calledOnce.should.be.true();
|
2020-04-22 17:27:43 +01:00
|
|
|
res.set.calledWith({'Cache-Control': 'public, max-age=0'}).should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly sets the public profile headers with custom maxAge', function (done) {
|
|
|
|
cacheControl('public', {maxAge: 123456})(null, res, function (a) {
|
|
|
|
should.not.exist(a);
|
|
|
|
res.set.calledOnce.should.be.true();
|
|
|
|
res.set.calledWith({'Cache-Control': 'public, max-age=123456'}).should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly sets the private profile headers', function (done) {
|
2016-10-11 09:36:00 +01:00
|
|
|
cacheControl('private')(null, res, function (a) {
|
2015-05-18 19:12:42 +01:00
|
|
|
should.not.exist(a);
|
2016-02-07 21:27:01 +00:00
|
|
|
res.set.calledOnce.should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
res.set.calledWith({
|
2017-03-21 08:24:11 +00:00
|
|
|
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
2020-04-22 17:27:43 +01:00
|
|
|
}).should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will not set headers without a profile', function (done) {
|
2016-10-11 09:36:00 +01:00
|
|
|
cacheControl()(null, res, function (a) {
|
2015-05-18 19:12:42 +01:00
|
|
|
should.not.exist(a);
|
2016-02-07 21:27:01 +00:00
|
|
|
res.set.called.should.be.false();
|
2015-05-18 19:12:42 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will not get confused between serving public and private', function (done) {
|
2020-04-25 21:06:33 +01:00
|
|
|
const publicCC = cacheControl('public');
|
|
|
|
const privateCC = cacheControl('private');
|
2015-05-18 19:12:42 +01:00
|
|
|
|
2015-10-12 17:54:15 +01:00
|
|
|
publicCC(null, res, function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
res.set.calledOnce.should.be.true();
|
2020-04-22 17:27:43 +01:00
|
|
|
res.set.calledWith({'Cache-Control': 'public, max-age=0'}).should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
|
2015-10-12 17:54:15 +01:00
|
|
|
privateCC(null, res, function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
res.set.calledTwice.should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
res.set.calledWith({
|
2017-03-21 08:24:11 +00:00
|
|
|
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
2020-04-22 17:27:43 +01:00
|
|
|
}).should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
|
2015-10-12 17:54:15 +01:00
|
|
|
publicCC(null, res, function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
res.set.calledThrice.should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
res.set.calledWith({'Cache-Control': 'public, max-age=0'});
|
|
|
|
|
2015-10-12 17:54:15 +01:00
|
|
|
privateCC(null, res, function () {
|
2015-05-18 19:12:42 +01:00
|
|
|
res.set.calledWith({
|
|
|
|
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
2020-04-22 17:27:43 +01:00
|
|
|
}).should.be.true();
|
2015-05-18 19:12:42 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-09-03 17:42:15 +02:00
|
|
|
});
|
2015-05-18 19:12:42 +01:00
|
|
|
});
|