2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const rewire = require('rewire');
|
|
|
|
const imageLib = require('../../../../core/server/lib/image');
|
|
|
|
const settingsCache = require('../../../../core/server/services/settings/cache');
|
|
|
|
const configUtils = require('../../../utils/configUtils');
|
|
|
|
const urlUtils = require('../../../utils/urlUtils');
|
|
|
|
const config = configUtils.config;
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2020-03-30 18:26:47 +03:00
|
|
|
const getAssetUrl = rewire('../../../../core/frontend/meta/asset_url');
|
2019-06-18 16:13:55 +03:00
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
describe('getAssetUrl', function () {
|
2017-04-10 12:30:21 +03:00
|
|
|
afterEach(function () {
|
|
|
|
configUtils.restore();
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2017-04-10 12:30:21 +03:00
|
|
|
});
|
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
it('should return asset url with just context', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js');
|
2016-09-13 18:41:14 +03:00
|
|
|
testUrl.should.equal('/assets/myfile.js?v=' + config.get('assetHash'));
|
2016-01-17 13:07:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return asset url with just context even with leading /', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('/myfile.js');
|
2016-09-13 18:41:14 +03:00
|
|
|
testUrl.should.equal('/assets/myfile.js?v=' + config.get('assetHash'));
|
2016-01-17 13:07:52 +03:00
|
|
|
});
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
it('should not add asset to url if ghost.css for default templates', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('public/ghost.css');
|
2017-04-07 15:21:41 +03:00
|
|
|
testUrl.should.equal('/public/ghost.css?v=' + config.get('assetHash'));
|
2017-04-04 12:06:38 +03:00
|
|
|
});
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
it('should not add asset to url has public in it', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('public/myfile.js');
|
2017-04-07 15:21:41 +03:00
|
|
|
testUrl.should.equal('/public/myfile.js?v=' + config.get('assetHash'));
|
2016-01-17 13:07:52 +03:00
|
|
|
});
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
describe('favicon', function () {
|
|
|
|
it('should not add asset to url if favicon.ico', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('favicon.ico');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/favicon.ico');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add asset to url if favicon.png', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('favicon.png');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/favicon.ico');
|
|
|
|
});
|
|
|
|
|
2017-04-10 14:04:46 +03:00
|
|
|
it('should correct favicon path for custom png', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('favicon.ico');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/favicon.png');
|
|
|
|
});
|
2016-01-17 13:07:52 +03:00
|
|
|
});
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
describe('minify', function () {
|
|
|
|
it('should return asset minified url when hasMinFile & useMinFiles are both set to true', function () {
|
|
|
|
configUtils.set('useMinFiles', true);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js', true);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/assets/myfile.min.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT return asset minified url when hasMinFile true but useMinFiles is false', function () {
|
|
|
|
configUtils.set('useMinFiles', false);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js', true);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/assets/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT return asset minified url when hasMinFile false but useMinFiles is true', function () {
|
|
|
|
configUtils.set('useMinFiles', true);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js', false);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/assets/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add min to anything besides the last .', function () {
|
|
|
|
configUtils.set('useMinFiles', true);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('test.page/myfile.js', true);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/assets/test.page/myfile.min.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with /blog subdirectory', function () {
|
|
|
|
beforeEach(function () {
|
2019-08-12 11:31:42 +03:00
|
|
|
getAssetUrl.__set__('urlUtils', urlUtils.getInstance({url: 'http://localhost:65535/blog'}));
|
2017-04-10 12:30:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return asset url with just context', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/assets/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return asset url with just context even with leading /', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('/myfile.js');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/assets/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add asset to url if ghost.css for default templates', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('public/ghost.css');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/public/ghost.css?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add asset to url has public in it', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('public/myfile.js');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/public/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('favicon', function () {
|
|
|
|
it('should not add asset to url if favicon.ico', function () {
|
2019-06-18 16:13:55 +03:00
|
|
|
sinon.stub(imageLib.blogIcon, 'getIconUrl').returns('/blog/favicon.ico');
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('favicon.ico');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/favicon.ico');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add asset to url if favicon.png', function () {
|
2019-06-18 16:13:55 +03:00
|
|
|
sinon.stub(imageLib.blogIcon, 'getIconUrl').returns('/blog/favicon.ico');
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('favicon.png');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/favicon.ico');
|
|
|
|
});
|
|
|
|
|
2017-04-10 14:04:46 +03:00
|
|
|
it('should return correct favicon path for custom png', function () {
|
2019-06-18 16:13:55 +03:00
|
|
|
sinon.stub(imageLib.blogIcon, 'getIconUrl').returns('/blog/favicon.png');
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('favicon.ico');
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/favicon.png');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('minify', function () {
|
|
|
|
it('should return asset minified url when hasMinFile & useMinFiles are both set to true', function () {
|
|
|
|
configUtils.set('useMinFiles', true);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js', true);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/assets/myfile.min.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT return asset minified url when hasMinFile true but useMinFiles is false', function () {
|
|
|
|
configUtils.set('useMinFiles', false);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js', true);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/assets/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT return asset minified url when hasMinFile false but useMinFiles is true', function () {
|
|
|
|
configUtils.set('useMinFiles', true);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('myfile.js', false);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/assets/myfile.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not add min to anything besides the last .', function () {
|
|
|
|
configUtils.set('useMinFiles', true);
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUrl = getAssetUrl('test.page/myfile.js', true);
|
2017-04-10 12:30:21 +03:00
|
|
|
testUrl.should.equal('/blog/assets/test.page/myfile.min.js?v=' + config.get('assetHash'));
|
|
|
|
});
|
|
|
|
});
|
2016-01-17 13:07:52 +03:00
|
|
|
});
|
|
|
|
});
|