diff --git a/test/util.http.js b/test/util.http.js new file mode 100644 index 000000000..4990d41e7 --- /dev/null +++ b/test/util.http.js @@ -0,0 +1,89 @@ +'use strict'; + +var _ = require('lodash'); +var chai = chai || require('chai'); +var sinon = sinon || require('sinon'); +var should = chai.should(); +var httpUtils = require('../js/util/HTTP'); +describe('http utils', function() { + + var xhr; + beforeEach(function() { + xhr = { + open: sinon.stub(), + getAllResponseHeaders: sinon.stub().returns('Content-type: xx'), + setRequestHeader: sinon.stub().returns(), + send: function() { + var self = this; + setTimeout(function() { + self.response = 'test'; + self.error ? self.onerror() : self.onload(); + }, 10); + }, + }; + }); + + it('should get success', function(done) { + xhr.error = 0; + httpUtils.request({ + xhr: xhr, + method: 'GET', + url: 'http://test' + }).success(function(data, status) { + done(); + }).error(function(data, status) { + throw new Error(); + }); + }); + it('should get error', function(done) { + xhr.error = 1; + httpUtils.request({ + xhr: xhr, + method: 'GET', + url: 'http://test', + }).success(function(data, status) { + throw new Error(); + }).error(function(data, status) { + done(); + }); + }); + + it('should get with headers', function(done) { + xhr.error = 0; + httpUtils.request({ + xhr: xhr, + method: 'GET', + url: 'http://test', + headers: { + 'Content-Transfer-Encoding': 'a', + 'Content-Length': 1, + 'X-test': 2, + } + }).success(function(data, status) { + done(); + }); + }); + it('should get with body', function(done) { + xhr.error = 0; + httpUtils.request({ + xhr: xhr, + method: 'GET', + url: 'http://test', + body: 'hola', + responseType: 'type', + }).success(function(data, status) { + done(); + }); + }); + it('should get with default error', function() { + xhr.error = 1; + + var ret = httpUtils.request({ + xhr: xhr, + method: 'GET', + url: 'http://test', + }); + ret._error.should.throw() + }); + +});