Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions __tests__/util/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ test('RequestManager.execute Request 403 error', async () => {
});
});

test('RequestManager.execute Request 4xx or 5xx error', async () => {
jest.resetModules();
const config = await Config.create({}, new Reporter());
jest.mock('request', factory => options => {
const errorStatusCode = Math.floor(Math.random() * (600 - 400) + 400); // random error code between 400 and 599
options.callback('', {statusCode: errorStatusCode}, '');
return {
on: () => {},
};
});
await config.requestManager.execute({
params: {
url: `https://localhost:port/?nocache`,
headers: {Connection: 'close'},
},
resolve: body => {},
reject: err => {
expect(
err.message.startsWith('https://localhost:port/?nocache: Request "https://localhost:port/?nocache" returned a'),
).toBe(true);
},
});
});

test('RequestManager.request with offlineNoRequests', async () => {
const config = await Config.create({offline: true}, new Reporter());
try {
Expand Down
12 changes: 5 additions & 7 deletions src/util/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,21 +388,19 @@ export default class RequestManager {
successHosts[parts.hostname] = true;

this.reporter.verbose(this.reporter.lang('verboseRequestFinish', params.url, res.statusCode));

if (body && typeof body.error === 'string') {
reject(new Error(body.error));
return;
}

if (res.statusCode === 403) {
if ([400, 401, 404].concat(params.rejectStatusCode || []).indexOf(res.statusCode) !== -1) {
body = false;
} else if (res.statusCode >= 400) {
const errMsg = (body && body.message) || reporter.lang('requestError', params.url, res.statusCode);
reject(new Error(errMsg));
} else {
if ([400, 401, 404].concat(params.rejectStatusCode || []).indexOf(res.statusCode) !== -1) {
body = false;
}
resolve(body);
}

resolve(body);
};
}

Expand Down