mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
Merge pull request #1395 from codeclown/instance-options
Fixing #385 - Keep defaults local to instance
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
var defaults = require('../../../lib/defaults');
|
||||
var mergeConfig = require('../../../lib/core/mergeConfig');
|
||||
|
||||
describe('core::mergeConfig', function() {
|
||||
it('should accept undefined for second argument', function() {
|
||||
expect(mergeConfig(defaults, undefined)).toEqual(defaults);
|
||||
});
|
||||
|
||||
it('should accept an object for second argument', function() {
|
||||
expect(mergeConfig(defaults, {})).toEqual(defaults);
|
||||
});
|
||||
|
||||
it('should not leave references', function() {
|
||||
var merged = mergeConfig(defaults, {});
|
||||
expect(merged).not.toBe(defaults);
|
||||
expect(merged.headers).not.toBe(defaults.headers);
|
||||
});
|
||||
|
||||
it('should allow setting request options', function() {
|
||||
var config = {
|
||||
url: '__sample url__',
|
||||
method: '__sample method__',
|
||||
params: '__sample params__',
|
||||
data: { foo: true }
|
||||
};
|
||||
var merged = mergeConfig(defaults, config);
|
||||
expect(merged.url).toEqual(config.url);
|
||||
expect(merged.method).toEqual(config.method);
|
||||
expect(merged.params).toEqual(config.params);
|
||||
expect(merged.data).toEqual(config.data);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it('should merge auth, headers, proxy with defaults', function() {
|
||||
expect(mergeConfig({ auth: undefined }, { auth: { user: 'foo', pass: 'test' } })).toEqual({
|
||||
auth: { 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() {
|
||||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: false })).toEqual({
|
||||
auth: false
|
||||
});
|
||||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: null })).toEqual({
|
||||
auth: null
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow setting other options', function() {
|
||||
var merged = mergeConfig(defaults, { timeout: 123 });
|
||||
expect(merged.timeout).toEqual(123);
|
||||
});
|
||||
});
|
||||
@@ -148,14 +148,14 @@ describe('defaults', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be used by custom instance if set after instance created', function (done) {
|
||||
it('should not be used by custom instance if set after instance created', function (done) {
|
||||
var instance = axios.create();
|
||||
axios.defaults.baseURL = 'http://example.org/';
|
||||
|
||||
instance.get('/foo');
|
||||
|
||||
getAjaxRequest().then(function (request) {
|
||||
expect(request.url).toBe('http://example.org/foo');
|
||||
expect(request.url).toBe('/foo');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,4 +81,32 @@ describe('options', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should change only the baseURL of the specified instance', function() {
|
||||
var instance1 = axios.create();
|
||||
var instance2 = axios.create();
|
||||
|
||||
instance1.defaults.baseURL = 'http://instance1.example.com/';
|
||||
|
||||
expect(instance2.defaults.baseURL).not.toBe('http://instance1.example.com/');
|
||||
});
|
||||
|
||||
it('should change only the headers of the specified instance', function() {
|
||||
var instance1 = axios.create();
|
||||
var instance2 = axios.create();
|
||||
|
||||
instance1.defaults.headers.common.Authorization = 'faketoken';
|
||||
instance2.defaults.headers.common.Authorization = 'differentfaketoken';
|
||||
|
||||
instance1.defaults.headers.common['Content-Type'] = 'application/xml';
|
||||
instance2.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
expect(axios.defaults.headers.common.Authorization).toBe(undefined);
|
||||
expect(instance1.defaults.headers.common.Authorization).toBe('faketoken');
|
||||
expect(instance2.defaults.headers.common.Authorization).toBe('differentfaketoken');
|
||||
|
||||
expect(axios.defaults.headers.common['Content-Type']).toBe(undefined);
|
||||
expect(instance1.defaults.headers.common['Content-Type']).toBe('application/xml');
|
||||
expect(instance2.defaults.headers.common['Content-Type']).toBe('application/x-www-form-urlencoded');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
var deepMerge = require('../../../lib/utils').deepMerge;
|
||||
|
||||
describe('utils::deepMerge', function () {
|
||||
it('should be immutable', function () {
|
||||
var a = {};
|
||||
var b = {foo: 123};
|
||||
var c = {bar: 456};
|
||||
|
||||
deepMerge(a, b, c);
|
||||
|
||||
expect(typeof a.foo).toEqual('undefined');
|
||||
expect(typeof a.bar).toEqual('undefined');
|
||||
expect(typeof b.bar).toEqual('undefined');
|
||||
expect(typeof c.foo).toEqual('undefined');
|
||||
});
|
||||
|
||||
it('should deepMerge properties', function () {
|
||||
var a = {foo: 123};
|
||||
var b = {bar: 456};
|
||||
var c = {foo: 789};
|
||||
var d = deepMerge(a, b, c);
|
||||
|
||||
expect(d.foo).toEqual(789);
|
||||
expect(d.bar).toEqual(456);
|
||||
});
|
||||
|
||||
it('should deepMerge recursively', function () {
|
||||
var a = {foo: {bar: 123}};
|
||||
var b = {foo: {baz: 456}, bar: {qux: 789}};
|
||||
|
||||
expect(deepMerge(a, b)).toEqual({
|
||||
foo: {
|
||||
bar: 123,
|
||||
baz: 456
|
||||
},
|
||||
bar: {
|
||||
qux: 789
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove all references from nested objects', function () {
|
||||
var a = {foo: {bar: 123}};
|
||||
var b = {};
|
||||
var d = deepMerge(a, b);
|
||||
|
||||
expect(d).toEqual({
|
||||
foo: {
|
||||
bar: 123
|
||||
}
|
||||
});
|
||||
|
||||
expect(d.foo).not.toBe(a.foo);
|
||||
});
|
||||
|
||||
it('handles null and undefined arguments', function () {
|
||||
expect(deepMerge(undefined, undefined)).toEqual({});
|
||||
expect(deepMerge(undefined, {foo: 123})).toEqual({foo: 123});
|
||||
expect(deepMerge({foo: 123}, undefined)).toEqual({foo: 123});
|
||||
|
||||
expect(deepMerge(null, null)).toEqual({});
|
||||
expect(deepMerge(null, {foo: 123})).toEqual({foo: 123});
|
||||
expect(deepMerge({foo: 123}, null)).toEqual({foo: 123});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user