mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-27 12:53:13 +03:00
f08a55c21f
refs: https://github.com/TryGhost/Team/issues/856 refs: https://github.com/TryGhost/Team/issues/756 - The .test.js extension is better than _spec.js as it's more obvious that it's an extension - It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test` - It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856) - Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
105 lines
3.5 KiB
JavaScript
105 lines
3.5 KiB
JavaScript
const should = require('should');
|
|
const sinon = require('sinon');
|
|
const fs = require('fs-extra');
|
|
const servePublicFile = require('../../../../../core/server/web/site/middleware/serve-public-file');
|
|
|
|
describe('servePublicFile', function () {
|
|
let res;
|
|
let req;
|
|
let next;
|
|
|
|
beforeEach(function () {
|
|
res = sinon.spy();
|
|
req = sinon.spy();
|
|
next = sinon.spy();
|
|
});
|
|
|
|
afterEach(function () {
|
|
sinon.restore();
|
|
});
|
|
|
|
it('should return a middleware', function () {
|
|
const result = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
|
|
result.should.be.a.Function();
|
|
});
|
|
|
|
it('should skip if the request does NOT match the file', function () {
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
req.path = '/favicon.ico';
|
|
middleware(req, res, next);
|
|
next.called.should.be.true();
|
|
});
|
|
|
|
it('should load the file and send it', function () {
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
const body = 'User-agent: * Disallow: /';
|
|
req.path = '/robots.txt';
|
|
|
|
sinon.stub(fs, 'readFile').callsFake(function (file, cb) {
|
|
cb(null, body);
|
|
});
|
|
|
|
res = {
|
|
writeHead: sinon.spy(),
|
|
end: sinon.spy()
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
next.called.should.be.false();
|
|
res.writeHead.called.should.be.true();
|
|
res.writeHead.args[0][0].should.equal(200);
|
|
res.writeHead.calledWith(200, sinon.match.has('Content-Type')).should.be.true();
|
|
res.writeHead.calledWith(200, sinon.match.has('Content-Length')).should.be.true();
|
|
res.writeHead.calledWith(200, sinon.match.has('ETag')).should.be.true();
|
|
res.writeHead.calledWith(200, sinon.match.has('Cache-Control', 'public, max-age=3600')).should.be.true();
|
|
|
|
res.end.calledWith(body).should.be.true();
|
|
});
|
|
|
|
it('should send the correct headers', function () {
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
const body = 'User-agent: * Disallow: /';
|
|
req.path = '/robots.txt';
|
|
|
|
sinon.stub(fs, 'readFile').callsFake(function (file, cb) {
|
|
cb(null, body);
|
|
});
|
|
|
|
res = {
|
|
writeHead: sinon.spy(),
|
|
end: sinon.spy()
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
next.called.should.be.false();
|
|
res.writeHead.called.should.be.true();
|
|
res.writeHead.args[0][0].should.equal(200);
|
|
res.writeHead.calledWith(200, sinon.match.has('Content-Type')).should.be.true();
|
|
res.writeHead.calledWith(200, sinon.match.has('Content-Length')).should.be.true();
|
|
res.writeHead.calledWith(200, sinon.match.has('ETag')).should.be.true();
|
|
res.writeHead.calledWith(200, sinon.match.has('Cache-Control', 'public, max-age=3600')).should.be.true();
|
|
});
|
|
|
|
it('should replace {{blog-url}} in text/plain', function () {
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
const body = 'User-agent: {{blog-url}}';
|
|
req.path = '/robots.txt';
|
|
|
|
sinon.stub(fs, 'readFile').callsFake(function (file, cb) {
|
|
cb(null, body);
|
|
});
|
|
|
|
res = {
|
|
writeHead: sinon.spy(),
|
|
end: sinon.spy()
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
next.called.should.be.false();
|
|
res.writeHead.called.should.be.true();
|
|
|
|
res.end.calledWith('User-agent: http://127.0.0.1:2369').should.be.true();
|
|
});
|
|
});
|