mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 00:11:49 +03:00
83f084608f
refs #8221 Instead of serving our shared assets from a `shared/` folder, we move the file, which are used server side to `server/public`. Adds a new `config.paths` entry: `publicFilePath` and renames the middleware to serve the files to reflect the changes. Adds `404-ghost.png` images to be used by the server side rendered default template `error.hbs`.
105 lines
3.5 KiB
JavaScript
105 lines
3.5 KiB
JavaScript
var should = require('should'), // jshint ignore:line
|
|
sinon = require('sinon'),
|
|
fs = require('fs'),
|
|
servePublicFile = require('../../../server/middleware/serve-public-file'),
|
|
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
describe('servePublicFile', function () {
|
|
var res, req, next;
|
|
|
|
beforeEach(function () {
|
|
res = sinon.spy();
|
|
req = sinon.spy();
|
|
next = sinon.spy();
|
|
});
|
|
|
|
afterEach(function () {
|
|
sandbox.restore();
|
|
});
|
|
|
|
it('should return a middleware', function () {
|
|
var result = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
|
|
result.should.be.a.Function();
|
|
});
|
|
|
|
it('should skip if the request does NOT match the file', function () {
|
|
var 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 () {
|
|
var middleware = servePublicFile('robots.txt', 'text/plain', 3600),
|
|
body = 'User-agent: * Disallow: /';
|
|
req.path = '/robots.txt';
|
|
|
|
sandbox.stub(fs, 'readFile', 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 () {
|
|
var middleware = servePublicFile('robots.txt', 'text/plain', 3600),
|
|
body = 'User-agent: * Disallow: /';
|
|
req.path = '/robots.txt';
|
|
|
|
sandbox.stub(fs, 'readFile', 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 () {
|
|
var middleware = servePublicFile('robots.txt', 'text/plain', 3600),
|
|
body = 'User-agent: {{blog-url}}';
|
|
req.path = '/robots.txt';
|
|
|
|
sandbox.stub(fs, 'readFile', 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();
|
|
});
|
|
});
|