2020-04-25 23:06:33 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const servePublicFile = require('../../../../../core/server/web/site/middleware/serve-public-file');
|
2016-04-08 13:39:50 +03:00
|
|
|
|
2017-04-07 15:21:41 +03:00
|
|
|
describe('servePublicFile', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let res;
|
|
|
|
let req;
|
|
|
|
let next;
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
res = sinon.spy();
|
|
|
|
req = sinon.spy();
|
|
|
|
next = sinon.spy();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a middleware', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const result = servePublicFile('robots.txt', 'text/plain', 3600);
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
result.should.be.a.Function();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip if the request does NOT match the file', function () {
|
2020-04-25 23:06:33 +03:00
|
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
2015-12-15 13:41:53 +03:00
|
|
|
req.path = '/favicon.ico';
|
2015-07-15 19:01:23 +03:00
|
|
|
middleware(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should load the file and send it', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
|
|
const body = 'User-agent: * Disallow: /';
|
2015-12-15 13:41:53 +03:00
|
|
|
req.path = '/robots.txt';
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(fs, 'readFile').callsFake(function (file, cb) {
|
2015-07-15 19:01:23 +03:00
|
|
|
cb(null, body);
|
|
|
|
});
|
|
|
|
|
|
|
|
res = {
|
|
|
|
writeHead: sinon.spy(),
|
|
|
|
end: sinon.spy()
|
|
|
|
};
|
|
|
|
|
|
|
|
middleware(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
res.writeHead.called.should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
res.writeHead.args[0][0].should.equal(200);
|
2016-02-08 00:27:01 +03:00
|
|
|
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();
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
res.end.calledWith(body).should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should send the correct headers', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
|
|
const body = 'User-agent: * Disallow: /';
|
2015-12-15 13:41:53 +03:00
|
|
|
req.path = '/robots.txt';
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(fs, 'readFile').callsFake(function (file, cb) {
|
2015-07-15 19:01:23 +03:00
|
|
|
cb(null, body);
|
|
|
|
});
|
|
|
|
|
|
|
|
res = {
|
|
|
|
writeHead: sinon.spy(),
|
|
|
|
end: sinon.spy()
|
|
|
|
};
|
|
|
|
|
|
|
|
middleware(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
res.writeHead.called.should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
res.writeHead.args[0][0].should.equal(200);
|
2016-02-08 00:27:01 +03:00
|
|
|
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();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should replace {{blog-url}} in text/plain', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const middleware = servePublicFile('robots.txt', 'text/plain', 3600);
|
|
|
|
const body = 'User-agent: {{blog-url}}';
|
2015-12-15 13:41:53 +03:00
|
|
|
req.path = '/robots.txt';
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(fs, 'readFile').callsFake(function (file, cb) {
|
2015-07-15 19:01:23 +03:00
|
|
|
cb(null, body);
|
|
|
|
});
|
|
|
|
|
|
|
|
res = {
|
|
|
|
writeHead: sinon.spy(),
|
|
|
|
end: sinon.spy()
|
|
|
|
};
|
|
|
|
|
|
|
|
middleware(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
res.writeHead.called.should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
res.end.calledWith('User-agent: http://127.0.0.1:2369').should.be.true();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
});
|