Skip to content

Commit

Permalink
Fix tests for the axios v1.2, make new passthrough mode compatible wi…
Browse files Browse the repository at this point in the history
…th axios v0.17
  • Loading branch information
marcbachmann committed Mar 26, 2023
1 parent 75e0f68 commit 314b994
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
38 changes: 20 additions & 18 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ function makeResponse(result, config) {
};
}

function passThroughRequest (mockAdapter, resolve, reject, config) {
// Axios v0.17 mutates the url to include the baseURL for non hostnames
// but does not remove the baseURL from the config
var baseURL = config.baseURL;
if (config.baseURL && !/^https?:/.test(config.baseURL)) {
baseURL = undefined;
}

mockAdapter.axiosInstance(Object.assign({}, config, {
baseURL: baseURL,
// Use the original adapter, not the mock adapter
adapter: mockAdapter.originalAdapter,
// The request transformation runs on the original axios handler already
transformRequest: [],
transformResponse: []
})).then(resolve, reject);
}

function handleRequest(mockAdapter, resolve, reject, config) {
var url = config.url || "";
// TODO we're not hitting this `if` in any of the tests, investigate
Expand Down Expand Up @@ -63,15 +81,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {

if (handler.length === 2) {
// passThrough handler
mockAdapter.axiosInstance({
...config,
// Use the original adapter, not the mock adapter
adapter: mockAdapter.originalAdapter,
// The request and url transformation runs on the original axios handler already
baseURL: null,
transformRequest: [],
transformResponse: []
}).then(resolve, reject);
passThroughRequest(mockAdapter, resolve, reject, config);
} else if (typeof handler[3] !== "function") {
utils.settle(
resolve,
Expand Down Expand Up @@ -127,15 +137,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
// handler not found
switch (mockAdapter.onNoMatch) {
case "passthrough":
mockAdapter.axiosInstance({
...config,
// Use the original adapter, not the mock adapter
adapter: mockAdapter.originalAdapter,
// The request and url transformation runs on the original axios handler already
baseURL: null,
transformRequest: [],
transformResponse: []
}).then(resolve, reject);
passThroughRequest(mockAdapter, resolve, reject, config);
break;
case "throwException":
throw utils.createCouldNotFindMockError(config);
Expand Down
8 changes: 7 additions & 1 deletion test/pass_through.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var axios = require("axios");
var isLegacyAxios = require("axios/package.json").version[0] === "0";
var expect = require("chai").expect;
var createServer = require("http").createServer;

Expand Down Expand Up @@ -117,7 +118,12 @@ describe("passThrough tests (requires Node)", function () {
mock.onAny().passThrough();
return instance.get("/foo").then(function (response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal("http://null/test/foo");

if (isLegacyAxios) {
expect(response.data).to.equal("http://null/test/foo");
} else {
expect(response.data).to.equal("http://localhost/test/foo");
}
});
});

Expand Down
7 changes: 6 additions & 1 deletion test/pass_through_on_no_match.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var axios = require("axios");
var isLegacyAxios = require("axios/package.json").version[0] === "0";
var expect = require("chai").expect;
var createServer = require("http").createServer;

Expand Down Expand Up @@ -122,7 +123,11 @@ describe("onNoMatch=passthrough option tests (requires Node)", function () {

return instance.get("/foo").then(function (response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal("http://null/test/foo");
if (isLegacyAxios) {
expect(response.data).to.equal("http://null/test/foo");
} else {
expect(response.data).to.equal("http://localhost/test/foo");
}
});
});

Expand Down

0 comments on commit 314b994

Please sign in to comment.