diff --git a/lib/axios.js b/lib/axios.js index 7e63e14..72ebb04 100644 --- a/lib/axios.js +++ b/lib/axios.js @@ -6,6 +6,7 @@ var spread = require('./spread'); var axios = module.exports = function axios(config) { config = utils.merge({ method: 'get', + headers: {}, transformRequest: defaults.transformRequest, transformResponse: defaults.transformResponse }, config); diff --git a/lib/defaults.js b/lib/defaults.js index 3baa22f..88c6b7b 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -19,7 +19,7 @@ module.exports = { } if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) { // Set application/json if no Content-Type has been specified - if (utils.isUndefined(headers['Content-Type'])) { + if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) { headers['Content-Type'] = 'application/json;charset=utf-8'; } return JSON.stringify(data); diff --git a/lib/utils.js b/lib/utils.js index 72d696b..516ff6f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -56,6 +56,16 @@ function isNumber(val) { return typeof val === 'number'; } +/** + * Determine if a value is undefined + * + * @param {Object} val The value to test + * @returns {boolean} True if the value is undefined, otherwise false + */ +function isUndefined(val) { + return typeof val === 'undefined'; +} + /** * Determine if a value is an Object * @@ -182,6 +192,7 @@ module.exports = { isString: isString, isNumber: isNumber, isObject: isObject, + isUndefined: isUndefined, isDate: isDate, isFile: isFile, isBlob: isBlob, diff --git a/test/specs/wrapper.spec.js b/test/specs/wrapper.spec.js index 452fcf4..7ae2cfa 100644 --- a/test/specs/wrapper.spec.js +++ b/test/specs/wrapper.spec.js @@ -30,10 +30,7 @@ describe('wrapper', function () { axios({ method: 'post', url: '/foo', - data: { - firstName: 'foo', - lastName: 'bar' - } + data: 'fizz=buzz' }); var request = jasmine.Ajax.requests.mostRecent(); @@ -45,6 +42,20 @@ describe('wrapper', function () { } }); + it('should use application/json when posting an object', function () { + axios({ + url: '/foo/bar', + method: 'post', + data: { + firstName: 'foo', + lastName: 'bar' + } + }); + + var request = jasmine.Ajax.requests.mostRecent(); + expect(request.requestHeaders['Content-Type']).toEqual('application/json;charset=utf-8'); + }); + it('should support binary data as array buffer', function () { var input = new Int8Array(2); input[0] = 1; diff --git a/test/unit/utils/isX.js b/test/unit/utils/isX.js index 306ce3b..c50f43a 100644 --- a/test/unit/utils/isX.js +++ b/test/unit/utils/isX.js @@ -19,6 +19,12 @@ module.exports = { test.done(); }, + testIsUndefined: function (test) { + test.equals(utils.isUndefined(), true); + test.equals(utils.isUndefined(null), false); + test.done(); + }, + testIsObject: function (test) { test.equals(utils.isObject({}), true); test.equals(utils.isObject(null), false);