diff --git a/src/handle_request.js b/src/handle_request.js index 26dea2c..a90ca74 100644 --- a/src/handle_request.js +++ b/src/handle_request.js @@ -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 @@ -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, @@ -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); diff --git a/test/pass_through.spec.js b/test/pass_through.spec.js index 3e3ba3a..ca7a765 100644 --- a/test/pass_through.spec.js +++ b/test/pass_through.spec.js @@ -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; @@ -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"); + } }); }); diff --git a/test/pass_through_on_no_match.spec.js b/test/pass_through_on_no_match.spec.js index a3bc015..9e8d938 100644 --- a/test/pass_through_on_no_match.spec.js +++ b/test/pass_through_on_no_match.spec.js @@ -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; @@ -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"); + } }); });