diff --git a/index.js b/index.js index 16fd6b7..91ef946 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ 'use strict' -var path = require('path'); +var { URL } = require('url') var axios = require('axios') var buildURL = require('axios/lib/helpers/buildURL') var axiosDebug = require('debug')('axios') const getURL = (config) => { - const url = path.join(config.baseURL || '', config.url) - return buildURL(url, config.params, config.paramsSerializer) + const fullURL = new URL(config.url, config.baseURL) + return buildURL(fullURL.toString(), config.params, config.paramsSerializer) } var options = { diff --git a/test.js b/test.js index 32bd2d7..07614ed 100644 --- a/test.js +++ b/test.js @@ -206,6 +206,27 @@ it('should add custom debug logger to axios instance', () => { }) }) +it('should logging request with baseURL', async () => { + return axios.create()({ + method: 'BAZ', + baseURL: 'http://example.com/', + url: '/path', + adapter: config => Promise.resolve({ + status: 200, + statusText: 'QUX', + config + }) + }).then(() => { + debug.log.should.be.calledTwice() + debug.log.firstCall.should.be.calledWithExactly( + 'BAZ http://example.com/path' + ) + debug.log.secondCall.should.be.calledWithExactly( + '200 QUX', '(BAZ http://example.com/path)' + ) + }) +}) + it('should be able to set format of response & response logging', () => { const requestLogger = sinon.spy((debug, config) => debug(config.method.toUpperCase())) const responseLogger = sinon.spy((debug, response) => debug(response.statusText.toUpperCase()))