2018-10-24 08:14:44 +03:00
|
|
|
const should = require('should');
|
|
|
|
const rewire = require('rewire');
|
|
|
|
const nock = require('nock');
|
2020-03-30 18:26:47 +03:00
|
|
|
const request = rewire('../../../core/server/lib/request');
|
2017-09-07 14:17:24 +03:00
|
|
|
|
|
|
|
describe('Request', function () {
|
2018-10-24 08:14:44 +03:00
|
|
|
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')
|
2017-09-07 14:17:24 +03:00
|
|
|
.get('/endpoint/')
|
|
|
|
.reply(200, 'Response body');
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
return request(url, options).then(function (res) {
|
2017-09-07 14:17:24 +03:00
|
|
|
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);
|
2018-10-24 08:14:44 +03:00
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
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')
|
2017-09-07 14:17:24 +03:00
|
|
|
.get('/endpoint/')
|
|
|
|
.reply(301, 'Oops, got redirected',
|
2019-08-19 14:41:09 +03:00
|
|
|
{
|
|
|
|
location: 'http://someredirectedurl.com/files/'
|
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
const secondRequestMock = nock('http://someredirectedurl.com')
|
2017-09-07 14:17:24 +03:00
|
|
|
.get('/files/')
|
|
|
|
.reply(200, 'Redirected response');
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
return request(url, options).then(function (res) {
|
2017-09-07 14:17:24 +03:00
|
|
|
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);
|
2018-10-24 08:14:44 +03:00
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
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.');
|
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
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.');
|
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
};
|
2017-09-07 14:17:24 +03:00
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
const requestMock = nock('http://nofilehere.com')
|
2017-09-07 14:17:24 +03:00
|
|
|
.get('/files/test.txt')
|
|
|
|
.reply(404);
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
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');
|
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
it('[failure] returns error if request errors', function () {
|
|
|
|
const url = 'http://nofilehere.com/files/test.txt';
|
|
|
|
const options = {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'Mozilla/5.0'
|
|
|
|
}
|
|
|
|
};
|
2017-09-07 14:17:24 +03:00
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
const requestMock = nock('http://nofilehere.com')
|
2017-09-07 14:17:24 +03:00
|
|
|
.get('/files/test.txt')
|
2019-08-19 14:41:09 +03:00
|
|
|
.times(3) // 1 original request + 2 default retries
|
2019-07-04 11:36:51 +03:00
|
|
|
.reply(500, {message: 'something awful happened', code: 'AWFUL_ERROR'});
|
2017-09-07 14:17:24 +03:00
|
|
|
|
2018-10-24 08:14:44 +03:00
|
|
|
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');
|
2019-07-04 11:36:51 +03:00
|
|
|
err.body.should.match(/something awful happened/);
|
|
|
|
err.body.should.match(/AWFUL_ERROR/);
|
2018-10-24 08:14:44 +03:00
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|
2020-12-09 15:19:22 +03:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2017-09-07 14:17:24 +03:00
|
|
|
});
|