Skip to content

Commit

Permalink
Fixing baseURL not working in interceptors (#950)
Browse files Browse the repository at this point in the history
* Fixing baseURL not working in interceptors

* add test for  modify base URL in request interceptor
  • Loading branch information
Baoyx007 authored and rubennorte committed Aug 12, 2017
1 parent 6508280 commit 2b85626
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
7 changes: 0 additions & 7 deletions lib/core/Axios.js
Expand Up @@ -4,8 +4,6 @@ var defaults = require('./../defaults');
var utils = require('./../utils');
var InterceptorManager = require('./InterceptorManager');
var dispatchRequest = require('./dispatchRequest');
var isAbsoluteURL = require('./../helpers/isAbsoluteURL');
var combineURLs = require('./../helpers/combineURLs');

/**
* Create a new instance of Axios
Expand Down Expand Up @@ -37,11 +35,6 @@ Axios.prototype.request = function request(config) {
config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
config.method = config.method.toLowerCase();

// Support baseURL config
if (config.baseURL && !isAbsoluteURL(config.url)) {
config.url = combineURLs(config.baseURL, config.url);
}

// Hook up interceptors middleware
var chain = [dispatchRequest, undefined];
var promise = Promise.resolve(config);
Expand Down
7 changes: 7 additions & 0 deletions lib/core/dispatchRequest.js
Expand Up @@ -4,6 +4,8 @@ var utils = require('./../utils');
var transformData = require('./transformData');
var isCancel = require('../cancel/isCancel');
var defaults = require('../defaults');
var isAbsoluteURL = require('./../helpers/isAbsoluteURL');
var combineURLs = require('./../helpers/combineURLs');

/**
* Throws a `Cancel` if cancellation has been requested.
Expand All @@ -23,6 +25,11 @@ function throwIfCancellationRequested(config) {
module.exports = function dispatchRequest(config) {
throwIfCancellationRequested(config);

// Support baseURL config
if (config.baseURL && !isAbsoluteURL(config.url)) {
config.url = combineURLs(config.baseURL, config.url);
}

// Ensure headers exist
config.headers = config.headers || {};

Expand Down
18 changes: 18 additions & 0 deletions test/specs/interceptors.spec.js
Expand Up @@ -252,4 +252,22 @@ describe('interceptors', function () {
done();
});
});

it('should modify base URL in request interceptor', function (done) {
var instance = axios.create({
baseURL: 'http://test.com/'
});

instance.interceptors.request.use(function (config) {
config.baseURL = 'http://rebase.com/';
return config;
});

instance.get('/foo');

getAjaxRequest().then(function (request) {
expect(request.url).toBe('http://rebase.com/foo');
done();
});
});
});

0 comments on commit 2b85626

Please sign in to comment.