Ghost/core/test/utils/urlUtils.js
Naz Gargol 5b59c7b542
🔥 Removed v0.1 controllers & routes (#11103)
no issue 

- Removed v0.1 controllers
- Removed 0.1 API unit tests
- Removed 0.1 API app and mount point
- Removed leftover use of v0.1 in entry-lookup test suite
- Removed frontend client API enpoints and related code (middleware)
- Fixed prev/next test suites to use v2 API
- Set default API version to explicit v2 in UrlUtils
- Removed v0.1 API regex from public files middleware
2019-09-11 19:10:10 +02:00

63 lines
1.9 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,
defaultApiVersion: 'v2',
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'),
defaultApiVersion: 'v2',
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;