Ghost/core/test/utils/urlUtils.js
Kevin Ansfield 4f3391cd04
Updated @tryghost/url-utils to v0.2.0 (#11004)
no issue

- updates `@tryghost/url-utils` following an internal refactor of the package
- renames `makeAbsoluteUrls` to `htmlRelativeToAbsolute` to better reflect what the function is doing
- renames `getBlogUrl` to `getSiteUrl`
- updates UrlUtils test stubbing util to work with a class
- fixes use of invalid port numbers in tests (max port number is 65535, any higher is an invalid URL that will error with some parsers)
2019-08-12 09:31:42 +01:00

61 lines
1.8 KiB
JavaScript

const _ = require('lodash');
const sinon = require('sinon');
const UrlUtils = require('@tryghost/url-utils');
const config = require('../../server/config');
const urlUtils = require('../../server/lib/url-utils');
const defaultSandbox = sinon.createSandbox();
const getInstance = (options) => {
const opts = {
url: options.url,
adminUrl: options.adminUrl,
apiVersions: options.apiVersions,
slugs: options.slugs,
redirectCacheMaxAge: options.redirectCacheMaxAge,
baseApiPath: '/ghost/api'
};
return new UrlUtils(opts);
};
const stubUrlUtils = (options, sandbox) => {
const stubInstance = getInstance(options);
const classPropNames = Object.getOwnPropertyNames(Object.getPrototypeOf(urlUtils))
.filter(name => name !== 'constructor');
classPropNames.forEach((key) => {
if (typeof urlUtils[key] === 'function') {
sandbox.stub(urlUtils, key).callsFake(function () {
return stubInstance[key](...arguments);
});
} else {
sandbox.stub(urlUtils, key).get(function () {
return stubInstance[key];
});
}
});
};
// Method for regressions tests must be used with restore method
const stubUrlUtilsFromConfig = () => {
const options = {
url: config.get('url'),
adminUrl: config.get('admin:url'),
apiVersions: config.get('api:versions'),
slugs: config.get('slugs').protected,
redirectCacheMaxAge: config.get('caching:301:maxAge'),
baseApiPath: '/ghost/api'
};
stubUrlUtils(options, defaultSandbox);
};
const restore = () => {
defaultSandbox.restore();
};
module.exports.stubUrlUtils = stubUrlUtils;
module.exports.stubUrlUtilsFromConfig = stubUrlUtilsFromConfig;
module.exports.restore = restore;
module.exports.getInstance = getInstance;