mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
closes #7688 - Use `/favicon.ico` and `/favicon.png` in blog app. Depending on type of storage (custom upload = local file storage), serves either from storage adapter with `read()` method or reads the bytes via `fs`. - Redirects requests for `favicon.ico` to `favicon.png` if custom `png` icon is uploaded and vice versa. - Redirect requests for `favicon.png` to `favicon.ico` if default icon is used (in `core/shared`). - Changes the `{{asset}}` helper for favicon to not serve from theme assets anymore. It will either be served the custom blog-icon or the default one. - The `{{@blog.icon}}` helper renders the url of the **uploaded** blog icon. It won't render the default icon.
175 lines
5.6 KiB
JavaScript
175 lines
5.6 KiB
JavaScript
var sinon = require('sinon'),
|
|
should = require('should'),
|
|
express = require('express'),
|
|
serveFavicon = require('../../../server/middleware/serve-favicon'),
|
|
configUtils = require('../../utils/configUtils'),
|
|
path = require('path'),
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
should.equal(true, true);
|
|
|
|
describe('Serve Favicon', function () {
|
|
var req, res, next, blogApp;
|
|
|
|
beforeEach(function () {
|
|
req = sinon.spy();
|
|
res = sinon.spy();
|
|
next = sinon.spy();
|
|
blogApp = express();
|
|
req.app = blogApp;
|
|
});
|
|
|
|
afterEach(function () {
|
|
sandbox.restore();
|
|
configUtils.restore();
|
|
});
|
|
|
|
describe('serveFavicon', function () {
|
|
it('should return a middleware', function () {
|
|
var middleware = serveFavicon();
|
|
|
|
middleware.should.be.a.Function();
|
|
});
|
|
|
|
it('should skip if the request does NOT match the file', function () {
|
|
var middleware = serveFavicon();
|
|
req.path = '/robots.txt';
|
|
middleware(req, res, next);
|
|
next.called.should.be.true();
|
|
});
|
|
describe('serves', function () {
|
|
it('custom uploaded favicon.png', function (done) {
|
|
var middleware = serveFavicon();
|
|
req.path = '/favicon.png';
|
|
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
|
|
|
|
configUtils.set({
|
|
theme: {
|
|
icon: 'favicon.png'
|
|
}
|
|
});
|
|
|
|
res = {
|
|
writeHead: function (statusCode) {
|
|
statusCode.should.eql(200);
|
|
},
|
|
end: function (body) {
|
|
body.length.should.eql(6792);
|
|
done();
|
|
}
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
});
|
|
it('custom uploaded favicon.ico', function (done) {
|
|
var middleware = serveFavicon();
|
|
req.path = '/favicon.ico';
|
|
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
|
|
|
|
configUtils.set({
|
|
theme: {
|
|
icon: 'favicon.ico'
|
|
}
|
|
});
|
|
|
|
res = {
|
|
writeHead: function (statusCode) {
|
|
statusCode.should.eql(200);
|
|
},
|
|
end: function (body) {
|
|
body.length.should.eql(15086);
|
|
done();
|
|
}
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
});
|
|
it('default favicon.ico', function (done) {
|
|
var middleware = serveFavicon();
|
|
req.path = '/favicon.ico';
|
|
configUtils.set('paths:corePath', path.join(__dirname, '../../../test/utils/fixtures/'));
|
|
|
|
configUtils.set({
|
|
theme: {
|
|
icon: ''
|
|
}
|
|
});
|
|
|
|
res = {
|
|
writeHead: function (statusCode) {
|
|
statusCode.should.eql(200);
|
|
},
|
|
end: function (body) {
|
|
body.length.should.eql(15086);
|
|
done();
|
|
}
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
});
|
|
});
|
|
describe('redirects', function () {
|
|
it('to custom favicon.ico when favicon.png is requested', function (done) {
|
|
var middleware = serveFavicon();
|
|
req.path = '/favicon.png';
|
|
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
|
|
|
|
configUtils.set({
|
|
theme: {
|
|
icon: 'favicon.ico'
|
|
}
|
|
});
|
|
|
|
res = {
|
|
redirect: function (statusCode) {
|
|
statusCode.should.eql(302);
|
|
done();
|
|
}
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
});
|
|
it('to custom favicon.png when favicon.ico is requested', function (done) {
|
|
var middleware = serveFavicon();
|
|
req.path = '/favicon.ico';
|
|
configUtils.set('paths:contentPath', path.join(__dirname, '../../../test/utils/fixtures/'));
|
|
|
|
configUtils.set({
|
|
theme: {
|
|
icon: 'favicon.png'
|
|
}
|
|
});
|
|
|
|
res = {
|
|
redirect: function (statusCode) {
|
|
statusCode.should.eql(302);
|
|
done();
|
|
}
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
});
|
|
|
|
it('to favicon.ico when favicon.png is requested', function (done) {
|
|
var middleware = serveFavicon();
|
|
req.path = '/favicon.png';
|
|
configUtils.set('paths:corePath', path.join(__dirname, '../../../test/utils/fixtures/'));
|
|
|
|
configUtils.set({
|
|
theme: {
|
|
icon: ''
|
|
}
|
|
});
|
|
|
|
res = {
|
|
redirect: function (statusCode) {
|
|
statusCode.should.eql(302);
|
|
done();
|
|
}
|
|
};
|
|
|
|
middleware(req, res, next);
|
|
});
|
|
});
|
|
});
|
|
});
|