diff --git a/core/server/adapters/scheduling/SchedulingDefault.js b/core/server/adapters/scheduling/SchedulingDefault.js index 8892dd7743..2bfb18f8e3 100644 --- a/core/server/adapters/scheduling/SchedulingDefault.js +++ b/core/server/adapters/scheduling/SchedulingDefault.js @@ -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. diff --git a/core/server/api/canary/themes.js b/core/server/api/canary/themes.js index d94dc2fc42..af95bd94c3 100644 --- a/core/server/api/canary/themes.js +++ b/core/server/api/canary/themes.js @@ -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'); diff --git a/core/server/lib/image/index.js b/core/server/lib/image/index.js index be30fcf82e..d829c7b605 100644 --- a/core/server/lib/image/index.js +++ b/core/server/lib/image/index.js @@ -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'); diff --git a/core/server/services/slack.js b/core/server/services/slack.js index 2d5aa431fa..c018e4413f 100644 --- a/core/server/services/slack.js +++ b/core/server/services/slack.js @@ -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'); diff --git a/core/server/services/webhooks/trigger.js b/core/server/services/webhooks/trigger.js index 111dbc7c2d..f107891436 100644 --- a/core/server/services/webhooks/trigger.js +++ b/core/server/services/webhooks/trigger.js @@ -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'); diff --git a/core/server/services/xmlrpc.js b/core/server/services/xmlrpc.js index 7e46b599e1..2dc75a8af1 100644 --- a/core/server/services/xmlrpc.js +++ b/core/server/services/xmlrpc.js @@ -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'); diff --git a/core/server/update-check.js b/core/server/update-check.js index 1b76ad5ee6..c96b8dda7e 100644 --- a/core/server/update-check.js +++ b/core/server/update-check.js @@ -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'); diff --git a/package.json b/package.json index 0eea135651..b33b840474 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/unit/lib/request_spec.js b/test/unit/lib/request_spec.js deleted file mode 100644 index aa17533e84..0000000000 --- a/test/unit/lib/request_spec.js +++ /dev/null @@ -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'); - }); - }); -}); diff --git a/yarn.lock b/yarn.lock index e7e1123122..e24c05179a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==