Merge pull request #2716 from arian/fix-2715-edge-ua-string
[mootools.git] / Specs / Request / Request.js
blobb2e4af56ae74ac7947595c789a3d5ba5cb2c2a65
1 /*
2 ---
3 name: Request
4 requires: ~
5 provides: ~
6 ...
7 */
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){
16                         requests.push(xhr);
17                 };
18         });
20         afterEach(function(){
21                 this.xhr.restore();
22         });
24         it('should create an ajax request', function(){
25                 var onComplete = jasmine.createSpy('Request onComplete');
27                 var request = new Request({
28                         url: '/',
29                         onComplete: onComplete
30                 }).send({data: {
31                         '__response': 'res&ponsé'
32                 }});
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é');
39         });
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',
47                         method: 'get',
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');
57         });
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',
64                         method: 'get',
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);
76         });
78         xit('(async) should create an ajax request and as it\'s an invalid XML, onComplete will receive null as the xml document', function(){
80                 runs(function(){
81                         this.onComplete = jasmine.createSpy();
82                         this.request = new Request({
83                                 url: '../Helpers/request.php',
84                                 onComplete: this.onComplete
85                         }).send({data: {
86                                 '__type': 'xml',
87                                 '__response': 'response'
88                         }});
89                 });
91                 waitsFor(800, function(){
92                         return this.onComplete.wasCalled;
93                 });
95                 runs(function(){
96                         expect(this.onComplete.argsForCall[0][0]).toEqual('response');
97                         expect(this.request.response.text).toEqual('response');
98                 });
100                 runs(function(){
101                         this.chain = jasmine.createSpy();
102                         this.request.chain(this.chain).send({data: {
103                                 '__type': 'xml',
104                                 '__response': '<node>response</node><no></no>'
105                         }});
106                 });
108                 waitsFor(800, function(){
109                         return this.chain.wasCalled;
110                 });
112                 runs(function(){
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>');
115                 });
117         });
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',
124                         data: {
125                                 __response: 'data'
126                         },
127                         onComplete: onComplete
128                 }).post();
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');
137         });
139         it('should not set xhr.withCredentials flag by default', function(){
140                 var request = new Request({
141                         url: '/something/or/other'
142                 }).send();
144                 expect(request.xhr.withCredentials).toBeFalsy();
145         });
147         /*<1.4compat>*/
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',
152                         user: 'someone'
153                 }).send();
155                 expect(request.xhr.withCredentials).toBe(true);
156         });
157         /*</1.4compat>*/
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',
163                         user: 'someone'
164                 }).send();
166                 expect(request.xhr.withCredentials).toBeFalsy();
167         });
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
173                 }).send();
175                 expect(request.xhr.withCredentials).toBe(true);
176         });