Replaced request module with @tryghost/request

no issue
Part of the effort to break up Ghost into smaller, decoupled modules.
This commit is contained in:
Sam Lord 2021-06-16 12:54:14 +01:00
parent 24332c3d24
commit 3f0bab4389
10 changed files with 21 additions and 180 deletions

View File

@ -4,7 +4,7 @@ const debug = require('@tryghost/debug')('scheduling-default');
const SchedulingBase = require('./SchedulingBase');
const logging = require('@tryghost/logging');
const errors = require('@tryghost/errors');
const request = require('../../lib/request');
const request = require('@tryghost/request');
/**
* @description Default post scheduling implementation.

View File

@ -6,7 +6,7 @@ const events = require('../../lib/common/events');
const themeService = require('../../services/themes');
const limitService = require('../../services/limits');
const models = require('../../models');
const request = require('../../lib/request');
const request = require('@tryghost/request');
const errors = require('@tryghost/errors/lib/errors');
const i18n = require('../../../shared/i18n');

View File

@ -1,4 +1,4 @@
const request = require('../request');
const request = require('@tryghost/request');
const urlUtils = require('../../../shared/url-utils');
const storage = require('../../adapters/storage');
const storageUtils = require('../../adapters/storage/utils');

View File

@ -2,7 +2,7 @@ const errors = require('@tryghost/errors');
const events = require('../lib/common/events');
const i18n = require('../../shared/i18n');
const logging = require('@tryghost/logging');
const request = require('../lib/request');
const request = require('@tryghost/request');
const {blogIcon} = require('../lib/image');
const urlUtils = require('../../shared/url-utils');
const urlService = require('../../frontend/services/url');

View File

@ -1,7 +1,7 @@
const _ = require('lodash');
const debug = require('@tryghost/debug')('services:webhooks:trigger');
const logging = require('@tryghost/logging');
const request = require('../../lib/request');
const request = require('@tryghost/request');
const models = require('../../models');
const payload = require('./payload');

View File

@ -6,7 +6,7 @@ const errors = require('@tryghost/errors');
const events = require('../lib/common/events');
const i18n = require('../../shared/i18n');
const logging = require('@tryghost/logging');
const request = require('../lib/request');
const request = require('@tryghost/request');
const settingsCache = require('./settings/cache');
const sentry = require('../../shared/sentry');

View File

@ -7,7 +7,7 @@ const urlUtils = require('./../shared/url-utils');
const i18n = require('../shared/i18n');
const logging = require('@tryghost/logging');
const request = require('./lib/request');
const request = require('@tryghost/request');
const ghostVersion = require('@tryghost/version');
const UpdateCheckService = require('@tryghost/update-check-service');

View File

@ -68,6 +68,7 @@
"@tryghost/mw-session-from-token": "0.1.21",
"@tryghost/package-json": "1.0.0",
"@tryghost/promise": "0.1.9",
"@tryghost/request": "0.1.0",
"@tryghost/root-utils": "0.3.0",
"@tryghost/security": "0.2.9",
"@tryghost/session-service": "0.1.23",

View File

@ -1,171 +0,0 @@
const should = require('should');
const rewire = require('rewire');
const nock = require('nock');
const request = rewire('../../../core/server/lib/request');
describe('Request', function () {
it('[success] should return response for http request', function () {
const url = 'http://some-website.com/endpoint/';
const expectedResponse = {
body: 'Response body',
url: 'http://some-website.com/endpoint/',
statusCode: 200
};
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
const requestMock = nock('http://some-website.com')
.get('/endpoint/')
.reply(200, 'Response body');
return request(url, options).then(function (res) {
requestMock.isDone().should.be.true();
should.exist(res);
should.exist(res.body);
res.body.should.be.equal(expectedResponse.body);
should.exist(res.url);
res.statusCode.should.be.equal(expectedResponse.statusCode);
should.exist(res.statusCode);
res.url.should.be.equal(expectedResponse.url);
});
});
it('[success] can handle redirect', function () {
const url = 'http://some-website.com/endpoint/';
const expectedResponse = {
body: 'Redirected response',
url: 'http://someredirectedurl.com/files/',
statusCode: 200
};
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
const requestMock = nock('http://some-website.com')
.get('/endpoint/')
.reply(301, 'Oops, got redirected',
{
location: 'http://someredirectedurl.com/files/'
});
const secondRequestMock = nock('http://someredirectedurl.com')
.get('/files/')
.reply(200, 'Redirected response');
return request(url, options).then(function (res) {
requestMock.isDone().should.be.true();
secondRequestMock.isDone().should.be.true();
should.exist(res);
should.exist(res.body);
res.body.should.be.equal(expectedResponse.body);
should.exist(res.url);
res.statusCode.should.be.equal(expectedResponse.statusCode);
should.exist(res.statusCode);
res.url.should.be.equal(expectedResponse.url);
});
});
it('[failure] can handle invalid url', function () {
const url = 'test';
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
return request(url, options).then(() => {
throw new Error('Request should have rejected with invalid url message');
}, (err) => {
should.exist(err);
err.message.should.be.equal('URL empty or invalid.');
});
});
it('[failure] can handle empty url', function () {
const url = '';
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
return request(url, options).then(() => {
throw new Error('Request should have rejected with invalid url message');
}, (err) => {
should.exist(err);
err.message.should.be.equal('URL empty or invalid.');
});
});
it('[failure] can handle an error with statuscode not 200', function () {
const url = 'http://nofilehere.com/files/test.txt';
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
const requestMock = nock('http://nofilehere.com')
.get('/files/test.txt')
.reply(404);
return request(url, options).then(() => {
throw new Error('Request should have errored');
}, (err) => {
requestMock.isDone().should.be.true();
should.exist(err);
err.statusMessage.should.be.equal('Not Found');
});
});
it('[failure] returns error if request errors', function () {
const url = 'http://nofilehere.com/files/test.txt';
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
const requestMock = nock('http://nofilehere.com')
.get('/files/test.txt')
.times(3) // 1 original request + 2 default retries
.reply(500, {message: 'something awful happened', code: 'AWFUL_ERROR'});
return request(url, options).then(() => {
throw new Error('Request should have errored with an awful error');
}, (err) => {
requestMock.isDone().should.be.true();
should.exist(err);
err.statusMessage.should.be.equal('Internal Server Error');
err.body.should.match(/something awful happened/);
err.body.should.match(/AWFUL_ERROR/);
});
});
it('[failure] should timeout when taking too long', function () {
const url = 'http://some-website.com/endpoint/';
const options = {
headers: {
'User-Agent': 'Mozilla/5.0'
},
timeout: 1,
retry: 0 // got retries by default so we're disabling this behavior
};
nock('http://some-website.com')
.get('/endpoint/')
.delay(20)
.reply(200, 'Response body');
return request(url, options).then(() => {
throw new Error('Should have timed out');
}, (err) => {
err.code.should.be.equal('ETIMEDOUT');
});
});
});

View File

@ -958,6 +958,17 @@
dependencies:
bluebird "^3.7.2"
"@tryghost/request@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@tryghost/request/-/request-0.1.0.tgz#8d72b58b94bfa2ac75a2dfd2240efd1742e7d1f7"
integrity sha512-fhXxKWXKaRChGhJ+L7LETsBPUxMCKDKmnBPPsj4pZOdmTthPdbQIMkvsKEQBnqibiv6rwf2GUqL/U1BTnHWGdg==
dependencies:
"@tryghost/errors" "^0.2.12"
"@tryghost/validator" "^0.1.0"
"@tryghost/version" "^0.1.0"
got "9.6.0"
lodash "^4.17.21"
"@tryghost/root-utils@0.3.0", "@tryghost/root-utils@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@tryghost/root-utils/-/root-utils-0.3.0.tgz#94565b5063c296b7493e3a635f9b77a8f917076b"
@ -1044,7 +1055,7 @@
remark-footnotes "^1.0.0"
unist-util-visit "^2.0.0"
"@tryghost/validator@0.1.0":
"@tryghost/validator@0.1.0", "@tryghost/validator@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@tryghost/validator/-/validator-0.1.0.tgz#f089fed04998a4ceb4957350117f8d9bb626daf0"
integrity sha512-Np9EN3RayTOd81GjVna6Zv12LsTz98JlqeXbOnUVzrRphARRJHvNwcKi7Kh4EDqA2MWpYQDiU0+QwP7zyA0Gkg==
@ -1055,7 +1066,7 @@
moment-timezone "0.5.23"
validator "7.2.0"
"@tryghost/version@0.1.0":
"@tryghost/version@0.1.0", "@tryghost/version@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@tryghost/version/-/version-0.1.0.tgz#2d1ec5dd19206a5f7ec0f283588fa716980c666c"
integrity sha512-7IvYLn0IFRFNK61IeHLMHb1cPDbD/zfRMlJqUe9kLTFRlLG6D29390VnkXWHC/W+K4frXdP1kfAglXXfOsunmg==