mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Fix merging of params (#2656)
* Name function to avoid ESLint func-names warning * Switch params config to merge list and update tests * Restore testing of both false and null * Restore test cases for keys without defaults * Include test for non-object values that aren't false-y.
This commit is contained in:
@@ -20,7 +20,7 @@ module.exports = function enhanceError(error, config, code, request, response) {
|
|||||||
error.response = response;
|
error.response = response;
|
||||||
error.isAxiosError = true;
|
error.isAxiosError = true;
|
||||||
|
|
||||||
error.toJSON = function() {
|
error.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
// Standard
|
// Standard
|
||||||
message: this.message,
|
message: this.message,
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ module.exports = function mergeConfig(config1, config2) {
|
|||||||
config2 = config2 || {};
|
config2 = config2 || {};
|
||||||
var config = {};
|
var config = {};
|
||||||
|
|
||||||
var valueFromConfig2Keys = ['url', 'method', 'params', 'data'];
|
var valueFromConfig2Keys = ['url', 'method', 'data'];
|
||||||
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy'];
|
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
|
||||||
var defaultToConfig2Keys = [
|
var defaultToConfig2Keys = [
|
||||||
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
|
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
|
||||||
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
|
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
|
||||||
|
|||||||
@@ -33,30 +33,32 @@ describe('core::mergeConfig', function() {
|
|||||||
it('should not inherit request options', function() {
|
it('should not inherit request options', function() {
|
||||||
var localDefaults = {
|
var localDefaults = {
|
||||||
method: '__sample method__',
|
method: '__sample method__',
|
||||||
params: '__sample params__',
|
|
||||||
data: { foo: true }
|
data: { foo: true }
|
||||||
};
|
};
|
||||||
var merged = mergeConfig(localDefaults, {});
|
var merged = mergeConfig(localDefaults, {});
|
||||||
expect(merged.method).toEqual(undefined);
|
expect(merged.method).toEqual(undefined);
|
||||||
expect(merged.params).toEqual(undefined);
|
|
||||||
expect(merged.data).toEqual(undefined);
|
expect(merged.data).toEqual(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should merge auth, headers, proxy with defaults', function() {
|
['auth', 'headers', 'params', 'proxy'].forEach(key => {
|
||||||
expect(mergeConfig({ auth: undefined }, { auth: { user: 'foo', pass: 'test' } })).toEqual({
|
it(`should set new config for ${key} without default`, function() {
|
||||||
auth: { user: 'foo', pass: 'test' }
|
expect(mergeConfig({ [key]: undefined }, { [key]: { user: 'foo', pass: 'test' } })).toEqual({
|
||||||
|
[key]: { user: 'foo', pass: 'test' }
|
||||||
|
});
|
||||||
});
|
});
|
||||||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: { pass: 'foobar' } })).toEqual({
|
|
||||||
auth: { user: 'foo', pass: 'foobar' }
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should overwrite auth, headers, proxy with a non-object value', function() {
|
it(`should merge ${key} with defaults`, function() {
|
||||||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: false })).toEqual({
|
expect(mergeConfig({ [key]: { user: 'foo', pass: 'bar' } }, { [key]: { pass: 'test' } })).toEqual({
|
||||||
auth: false
|
[key]: { user: 'foo', pass: 'test' }
|
||||||
|
});
|
||||||
});
|
});
|
||||||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: null })).toEqual({
|
|
||||||
auth: null
|
it(`should overwrite default ${key} with a non-object value`, function() {
|
||||||
|
[false, null, 123].forEach(value => {
|
||||||
|
expect(mergeConfig({ [key]: { user: 'foo', pass: 'test' } }, { [key]: value })).toEqual({
|
||||||
|
[key]: value
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user