Skip to content

Commit

Permalink
Merge pull request #296 from marcbachmann/validateStatus-null-support
Browse files Browse the repository at this point in the history
Support for validateStatus: null
  • Loading branch information
ctimmerm authored Aug 11, 2021
2 parents b2b7d17 + 43e35b6 commit e5e7e19
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,37 +115,39 @@ function purgeIfReplyOnce(mock, handler) {

function settle(resolve, reject, response, delay) {
if (delay > 0) {
setTimeout(function () {
settle(resolve, reject, response);
}, delay);
setTimeout(settle, delay, resolve, reject, response);
return;
}

if (response.config && response.config.validateStatus) {
response.config.validateStatus(response.status)
? resolve(response)
: reject(
createAxiosError(
"Request failed with status code " + response.status,
response.config,
response
)
);
// Support for axios < 0.13
if (!rejectWithError && (!response.config || !response.config.validateStatus)) {
if (response.status >= 200 && response.status < 300) {
resolve(response);
} else {
reject(response);
}
return;
}

// Support for axios < 0.11
if (response.status >= 200 && response.status < 300) {
if (
!response.config.validateStatus ||
response.config.validateStatus(response.status)
) {
resolve(response);
} else {
reject(response);
if (!rejectWithError) {
return reject(response);
}

reject(createAxiosError(
'Request failed with status code ' + response.status,
response.config,
response
));
}
}

function createAxiosError(message, config, response, code) {
// Support for axios < 0.13.0
if (!rejectWithError) return response;

var error = new Error(message);
error.isAxiosError = true;
error.config = config;
Expand Down
9 changes: 9 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,15 @@ describe("MockAdapter basics", function () {
});
});

it("supports providing a validateStatus null value", function () {
instance.defaults.validateStatus = null;
mock.onGet("/foo").reply(500);

return instance.get("/foo").then(function (response) {
expect(response.status).to.equal(500);
});
});

it("respects validatesStatus when requesting unhandled urls", function () {
instance.defaults.validateStatus = function () {
return true;
Expand Down

0 comments on commit e5e7e19

Please sign in to comment.