From ede39aaea9a57c61063678ab6463e385aae301ee Mon Sep 17 00:00:00 2001 From: mzabriskie Date: Sun, 5 Oct 2014 17:27:25 -0600 Subject: [PATCH] Adding tests for transformers --- test/specs/transform.spec.js | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/specs/transform.spec.js diff --git a/test/specs/transform.spec.js b/test/specs/transform.spec.js new file mode 100644 index 00000000..0d1ada9d --- /dev/null +++ b/test/specs/transform.spec.js @@ -0,0 +1,72 @@ +describe('transform', function () { + beforeEach(function () { + jasmine.Ajax.install(); + }); + + it('should transform JSON to string', function () { + var data = { + foo: 'bar' + }; + + axios({ + url: '/foo', + method: 'post', + data: data + }); + + var request = jasmine.Ajax.requests.mostRecent(); + expect(request.params).toEqual('{"foo":"bar"}'); + }); + + it('should override default transform', function () { + var data = { + foo: 'bar' + }; + + axios({ + url: '/foo', + method: 'post', + data: data, + transformRequest: function (data) { + return data; + } + }); + + var request = jasmine.Ajax.requests.mostRecent(); + expect(typeof request.params).toEqual('object'); + }); + + it('should allow an Array of transformers', function () { + var data = { + foo: 'bar' + }; + + axios({ + url: '/foo', + method: 'post', + data: data, + transformRequest: axios.defaults.transformRequest.concat( + function (data) { + return data.replace('bar', 'baz'); + } + ) + }); + + var request = jasmine.Ajax.requests.mostRecent(); + expect(request.params).toEqual('{"foo":"baz"}'); + }); + + it('should allowing mutating headers', function () { + var token = Math.floor(Math.random() * Math.pow(2, 64)).toString(36); + + axios({ + url: '/foo', + transformRequest: function (data, headers) { + headers['X-Authorization'] = token; + } + }); + + var request = jasmine.Ajax.requests.mostRecent(); + expect(request.requestHeaders['X-Authorization']).toEqual(token); + }); +}); \ No newline at end of file