9 describe('Request', function(){
10 var hasWithCredentials = 'withCredentials' in new Browser.Request;
12 beforeEach(function(){
13 this.xhr = sinon.useFakeXMLHttpRequest();
14 var requests = this.requests = [];
15 this.xhr.onCreate = function(xhr){
24 it('should create an ajax request', function(){
25 var onComplete = jasmine.createSpy('Request onComplete');
27 var request = new Request({
29 onComplete: onComplete
31 '__response': 'res&ponsé'
34 this.requests[0].respond(200, {'Content-Type': 'text/plain'}, 'res&ponsé');
36 // checks the first argument from the first call
37 expect(onComplete.argsForCall[0][0]).toEqual('res&ponsé');
41 it('should create a Request with method get and sending data', function(){
43 var onComplete = jasmine.createSpy('Request onComplete data');
45 var request = new Request({
46 url: '../Helpers/request.php',
48 onComplete: onComplete
49 }).send({data: {'some': 'data'}});
51 this.requests[0].respond(200, {'Content-Type': 'text/json'}, 'data');
53 expect(onComplete.wasCalled).toBe(true);
55 expect(onComplete.argsForCall[0][0]).toEqual('data');
59 it('the options passed on the send method should rewrite the current ones', function(){
61 var onComplete = jasmine.createSpy('Request onComplete rewrite data');
62 var request = new Request({
63 url: '../Helpers/request.php',
65 data: {'setup': 'data'},
66 onComplete: onComplete
67 }).send({method: 'post', data: {'send': 'senddata'}});
69 var requested = this.requests[0];
71 expect(requested.method.toLowerCase()).toBe('post');
73 requested.respond(200, {'Content-Type': 'text/plain'}, '');
75 expect(onComplete.wasCalled).toBe(true);
78 xit('(async) should create an ajax request and as it\'s an invalid XML, onComplete will receive null as the xml document', function(){
81 this.onComplete = jasmine.createSpy();
82 this.request = new Request({
83 url: '../Helpers/request.php',
84 onComplete: this.onComplete
87 '__response': 'response'
91 waitsFor(800, function(){
92 return this.onComplete.wasCalled;
96 expect(this.onComplete.argsForCall[0][0]).toEqual('response');
97 expect(this.request.response.text).toEqual('response');
101 this.chain = jasmine.createSpy();
102 this.request.chain(this.chain).send({data: {
104 '__response': '<node>response</node><no></no>'
108 waitsFor(800, function(){
109 return this.chain.wasCalled;
113 expect(this.onComplete.argsForCall[0][0]).toEqual('<node>response</node><no></no>');
114 expect(this.request.response.text).toEqual('<node>response</node><no></no>');
119 it('should not overwrite the data object', function(){
121 var onComplete = jasmine.createSpy('Request onComplete overwrite protection');
122 var request = new Request({
123 url: '../Helpers/request.php',
127 onComplete: onComplete
130 var requested = this.requests[0];
131 requested.respond(200, {'Content-Type': 'text/plain'}, requested.requestBody)
133 expect(onComplete.wasCalled).toBe(true);
135 expect(onComplete.argsForCall[0][0]).toEqual('__response=data');
139 it('should not set xhr.withCredentials flag by default', function(){
140 var request = new Request({
141 url: '/something/or/other'
144 expect(request.xhr.withCredentials).toBeFalsy();
148 var dit = hasWithCredentials ? it : xit;
149 dit('should set xhr.withCredentials flag in 1.4 for this.options.user', function(){
150 var request = new Request({
151 url: '/something/or/other',
155 expect(request.xhr.withCredentials).toBe(true);
159 var dit = hasWithCredentials ? /*<1.4compat>*/xit || /*</1.4compat>*/it : xit; // don't run unless no compat
160 dit('should not set xhr.withCredentials flag in 1.5 for this.options.user', function(){
161 var request = new Request({
162 url: '/something/or/other',
166 expect(request.xhr.withCredentials).toBeFalsy();
169 dit('should set xhr.withCredentials flag if options.withCredentials is set', function(){
170 var request = new Request({
171 url: '/something/or/other',
172 withCredentials: true
175 expect(request.xhr.withCredentials).toBe(true);