Skip to content

Commit

Permalink
Tests: Stub image request for dimensions (#8973)
Browse files Browse the repository at this point in the history
no issue
- test cases were trying to fetch image sizes for `localhost:port/favicon.ico` but no server is running so they time out
- stub the `getImageSizeFromUrl` method so it resolves instantly
  • Loading branch information
kirrg001 authored and kevinansfield committed Sep 5, 2017
1 parent 5c2e116 commit 1fe87a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 2 additions & 3 deletions core/server/utils/cached-image-size-from-url.js
@@ -1,8 +1,7 @@
var Promise = require('bluebird'),
sizeOf = require('./image-size'),
imageSize = require('./image-size'),
logging = require('../logging'),
errors = require('../errors'),
getImageSizeFromUrl = sizeOf.getImageSizeFromUrl,
imageSizeCache = {};

/**
Expand All @@ -20,7 +19,7 @@ function getCachedImageSizeFromUrl(url) {

// image size is not in cache
if (!imageSizeCache[url]) {
return getImageSizeFromUrl(url).then(function (res) {
return imageSize.getImageSizeFromUrl(url).then(function (res) {
imageSizeCache[url] = res;

return Promise.resolve(imageSizeCache[url]);
Expand Down
11 changes: 9 additions & 2 deletions core/test/unit/server_helpers/ghost_head_spec.js
Expand Up @@ -5,6 +5,7 @@ var should = require('should'), // jshint ignore:line
moment = require('moment'),
configUtils = require('../../utils/configUtils'),
helpers = require('../../../server/helpers'),
imageSize = require('../../../server/utils/image-size'),
proxy = require('../../../server/helpers/proxy'),
settingsCache = proxy.settingsCache,
api = proxy.api,
Expand All @@ -18,9 +19,15 @@ describe('{{ghost_head}} helper', function () {
configUtils.restore();
});

// TODO: stub `getImageDimensions` to make things faster
beforeEach(function () {
sandbox.stub(api.clients, 'read').returns(new Promise.resolve({
/**
* Each test case here requests the image dimensions.
* The image path is e.g. localhost:port/favicon.ico, but no server is running.
* If we don't mock the image size utility, we run into lot's of timeouts.
*/
sandbox.stub(imageSize, 'getImageSizeFromUrl').returns(Promise.resolve());

sandbox.stub(api.clients, 'read').returns(Promise.resolve({
clients: [
{slug: 'ghost-frontend', secret: 'a1bcde23cfe5', status: 'enabled'}
]
Expand Down
6 changes: 3 additions & 3 deletions core/test/unit/utils/cached-image-size-from-url_spec.js
Expand Up @@ -3,7 +3,7 @@ var should = require('should'),
Promise = require('bluebird'),
rewire = require('rewire'),

// Stuff we are testing
// Stuff we are testing
getCachedImageSizeFromUrl = rewire('../../../server/utils/cached-image-size-from-url'),

sandbox = sinon.sandbox.create();
Expand All @@ -30,7 +30,7 @@ describe('getCachedImageSizeFromUrl', function () {
type: 'jpg'
}));

getCachedImageSizeFromUrl.__set__('getImageSizeFromUrl', sizeOfStub);
getCachedImageSizeFromUrl.__set__('imageSize.getImageSizeFromUrl', sizeOfStub);

getCachedImageSizeFromUrl(url).then(function () {
// first call to get result from `getImageSizeFromUrl`
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('getCachedImageSizeFromUrl', function () {

sizeOfStub.returns(new Promise.reject('error'));

getCachedImageSizeFromUrl.__set__('getImageSizeFromUrl', sizeOfStub);
getCachedImageSizeFromUrl.__set__('imageSize.getImageSizeFromUrl', sizeOfStub);

getCachedImageSizeFromUrl(url)
.then(function () {
Expand Down

0 comments on commit 1fe87a6

Please sign in to comment.