Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
felipewmartins committed Sep 6, 2019
2 parents a21527a + 89bd3ab commit a1aa760
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 19 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
@@ -1,13 +1,11 @@
services:
- xvfb
language: node_js
node_js:
- node
email:
on_failure: change
on_success: never
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start
after_success:
- npm run coveralls
env:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -413,7 +413,7 @@ The response for a request contains the following information.

// `request` is the request that generated this response
// It is the last ClientRequest instance in node.js (in redirects)
// and an XMLHttpRequest instance the browser
// and an XMLHttpRequest instance in the browser
request: {}
}
```
Expand Down Expand Up @@ -500,7 +500,7 @@ axios.interceptors.response.use(function (response) {
});
```

If you may need to remove an interceptor later you can.
If you need to remove an interceptor later you can.

```js
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
Expand Down
4 changes: 3 additions & 1 deletion lib/adapters/http.js
Expand Up @@ -2,6 +2,7 @@

var utils = require('./../utils');
var settle = require('./../core/settle');
var buildFullPath = require('../core/buildFullPath');
var buildURL = require('./../helpers/buildURL');
var http = require('http');
var https = require('https');
Expand Down Expand Up @@ -64,7 +65,8 @@ module.exports = function httpAdapter(config) {
}

// Parse url
var parsed = url.parse(config.url);
var fullPath = buildFullPath(config.baseURL, config.url);
var parsed = url.parse(fullPath);
var protocol = parsed.protocol || 'http:';

if (!auth && parsed.auth) {
Expand Down
6 changes: 4 additions & 2 deletions lib/adapters/xhr.js
Expand Up @@ -3,6 +3,7 @@
var utils = require('./../utils');
var settle = require('./../core/settle');
var buildURL = require('./../helpers/buildURL');
var buildFullPath = require('../core/buildFullPath');
var parseHeaders = require('./../helpers/parseHeaders');
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
var createError = require('../core/createError');
Expand All @@ -25,7 +26,8 @@ module.exports = function xhrAdapter(config) {
requestHeaders.Authorization = 'Basic ' + btoa(username + ':' + password);
}

request.open(config.method.toUpperCase(), buildURL(config.url, config.params, config.paramsSerializer), true);
var fullPath = buildFullPath(config.baseURL, config.url);
request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true);

// Set the request timeout in MS
request.timeout = config.timeout;
Expand Down Expand Up @@ -100,7 +102,7 @@ module.exports = function xhrAdapter(config) {
var cookies = require('./../helpers/cookies');

// Add xsrf header
var xsrfValue = (config.withCredentials || isURLSameOrigin(config.url)) && config.xsrfCookieName ?
var xsrfValue = (config.withCredentials || isURLSameOrigin(fullPath)) && config.xsrfCookieName ?
cookies.read(config.xsrfCookieName) :
undefined;

Expand Down
20 changes: 20 additions & 0 deletions lib/core/buildFullPath.js
@@ -0,0 +1,20 @@
'use strict';

var isAbsoluteURL = require('../helpers/isAbsoluteURL');
var combineURLs = require('../helpers/combineURLs');

/**
* Creates a new URL by combining the baseURL with the requestedURL,
* only when the requestedURL is not already an absolute URL.
* If the requestURL is absolute, this function returns the requestedURL untouched.
*
* @param {string} baseURL The base URL
* @param {string} requestedURL Absolute or relative URL to combine
* @returns {string} The combined full path
*/
module.exports = function buildFullPath(baseURL, requestedURL) {
if (baseURL && !isAbsoluteURL(requestedURL)) {
return combineURLs(baseURL, requestedURL);
}
return requestedURL;
};
7 changes: 0 additions & 7 deletions lib/core/dispatchRequest.js
Expand Up @@ -4,8 +4,6 @@ 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 @@ -25,11 +23,6 @@ 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
2 changes: 1 addition & 1 deletion lib/core/mergeConfig.js
Expand Up @@ -34,7 +34,7 @@ module.exports = function mergeConfig(config1, config2) {
});

utils.forEach([
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'maxContentLength',
'validateStatus', 'maxRedirects', 'httpAgent', 'httpsAgent', 'cancelToken',
Expand Down
20 changes: 20 additions & 0 deletions test/specs/core/buildFullPath.spec.js
@@ -0,0 +1,20 @@
var buildFullPath = require('../../../lib/core/buildFullPath');

describe('helpers::buildFullPath', function () {
it('should combine URLs when the requestedURL is relative', function () {
expect(buildFullPath('https://api.github.com', '/users')).toBe('https://api.github.com/users');
});

it('should return the requestedURL when it is absolute', function () {
expect(buildFullPath('https://api.github.com', 'https://api.example.com/users')).toBe('https://api.example.com/users');
});

it('should not combine URLs when the baseURL is not configured', function () {
expect(buildFullPath(undefined, '/users')).toBe('/users');
});

it('should combine URLs when the baseURL and requestedURL are relative', function () {
expect(buildFullPath('/api', '/users')).toBe('/api/users');
});

});
2 changes: 0 additions & 2 deletions test/specs/core/mergeConfig.spec.js
Expand Up @@ -32,13 +32,11 @@ describe('core::mergeConfig', function() {

it('should not inherit request options', function() {
var localDefaults = {
url: '__sample url__',
method: '__sample method__',
params: '__sample params__',
data: { foo: true }
};
var merged = mergeConfig(localDefaults, {});
expect(merged.url).toEqual(undefined);
expect(merged.method).toEqual(undefined);
expect(merged.params).toEqual(undefined);
expect(merged.data).toEqual(undefined);
Expand Down
13 changes: 13 additions & 0 deletions test/specs/instance.spec.js
Expand Up @@ -37,6 +37,19 @@ describe('instance', function () {
});
});

it('should make an http request with url instead of baseURL', function () {
var instance = axios.create({
url: 'https://api.example.com'
});

instance('/foo');

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

it('should make an http request', function (done) {
var instance = axios.create();

Expand Down
24 changes: 24 additions & 0 deletions test/specs/requests.spec.js
Expand Up @@ -245,6 +245,30 @@ describe('requests', function () {
});
});

it('should not modify the config url with relative baseURL', function (done) {
var config;

axios.get('/foo', {
baseURL: '/api'
}).catch(function (error) {
config = error.config;
});

getAjaxRequest().then(function (request) {
request.respondWith({
status: 404,
statusText: 'NOT FOUND',
responseText: 'Resource not found'
});

setTimeout(function () {
expect(config.baseURL).toEqual('/api');
expect(config.url).toEqual('/foo');
done();
}, 100);
});
});

it('should allow overriding Content-Type header case-insensitive', function (done) {
var response;
var contentType = 'application/vnd.myapp.type+json';
Expand Down
14 changes: 14 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -646,6 +646,20 @@ describe('supports http with nodejs', function () {
});
});
});

it('should combine baseURL and url', function (done) {
server = http.createServer(function (req, res) {
res.end();
}).listen(4444, function () {
axios.get('/foo', {
baseURL: 'http://localhost:4444/',
}).then(function (res) {
assert.equal(res.config.baseURL, 'http://localhost:4444/');
assert.equal(res.config.url, '/foo');
done();
});
});
});
});


0 comments on commit a1aa760

Please sign in to comment.