You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
import test from 'ava';
|
|
import nock from 'nock';
|
|
import Onionoo from '../';
|
|
import data from './fixtures/data';
|
|
|
|
test('Handle HTML responses for errors', async t => {
|
|
const onionoo = new Onionoo();
|
|
|
|
const defaultEndpoint = data.defaultEndpoints[0];
|
|
const scope = nock(data.defaultBaseUrl)
|
|
.get(`/${defaultEndpoint}`)
|
|
.reply(400, data.dummy400Response);
|
|
|
|
try {
|
|
await onionoo[defaultEndpoint]();
|
|
} catch (err) {
|
|
t.deepEqual(err.response.body, data.dummy400Response);
|
|
}
|
|
|
|
t.truthy(scope.isDone());
|
|
});
|
|
|
|
test('Throw useful errors for HTTP response codes', async t => {
|
|
const onionoo = new Onionoo();
|
|
|
|
const defaultEndpoint = data.defaultEndpoints[0];
|
|
const responseCodes = {
|
|
400: 'Bad Request',
|
|
404: 'Not Found',
|
|
500: 'Internal Server Error',
|
|
503: 'Service Unavailable'
|
|
};
|
|
|
|
Object.keys(responseCodes).forEach(async responseCode => {
|
|
const scope = nock(data.defaultBaseUrl)
|
|
.get(`/${defaultEndpoint}`)
|
|
.reply(responseCode);
|
|
|
|
try {
|
|
await onionoo[defaultEndpoint]();
|
|
} catch (err) {
|
|
t.is(err.message, `Response code ${responseCode} (${responseCodes[responseCode]})`);
|
|
t.is(err.statusCode, parseInt(responseCode, 10));
|
|
t.is(err.statusMessage, responseCodes[responseCode]);
|
|
}
|
|
t.truthy(scope.isDone());
|
|
});
|
|
});
|
|
|