]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/test/specs/data/proxy/Ajax.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / data / proxy / Ajax.js
CommitLineData
6527f429
DM
1describe("Ext.data.proxy.Ajax", function() {\r
2 var proxy;\r
3 \r
4 function makeProxy(cfg) {\r
5 cfg = cfg || {};\r
6 cfg = Ext.applyIf(cfg, {\r
7 url: '/foo'\r
8 });\r
9 proxy = new Ext.data.proxy.Ajax(cfg);\r
10 }\r
11\r
12 afterEach(function() {\r
13 Ext.data.Model.schema.clear();\r
14 Ext.undefine('spec.Company');\r
15 proxy = null;\r
16 });\r
17\r
18 describe("instantiation", function() {\r
19 beforeEach(function() {\r
20 makeProxy();\r
21 });\r
22 \r
23 it("should extend Ext.data.proxy.Server", function() {\r
24 expect(proxy.superclass).toBe(Ext.data.proxy.Server.prototype);\r
25 });\r
26\r
27 it("should have correct actionMethods", function() {\r
28 expect(proxy.getActionMethods()).toEqual({\r
29 create : "POST",\r
30 read : "GET",\r
31 update : "POST",\r
32 destroy: "POST"\r
33 });\r
34 });\r
35 });\r
36 \r
37 describe("parameters", function() {\r
38 var ajax, operation;\r
39 beforeEach(function() {\r
40 spyOn(Ext.Ajax, 'request').andCallFake(function(o) {\r
41 ajax = o;\r
42 });\r
43 operation = new Ext.data.operation.Read();\r
44 });\r
45 \r
46 afterEach(function(){\r
47 ajax = operation = null;\r
48 });\r
49 \r
50 describe("binary", function() {\r
51 it("should default to binary false", function() {\r
52 makeProxy();\r
53 proxy.read(operation);\r
54 expect(ajax.binary).toBe(false);\r
55 });\r
56 \r
57 it("should pass binary when set on the proxy", function() {\r
58 makeProxy({\r
59 binary: true\r
60 });\r
61 proxy.read(operation);\r
62 expect(ajax.binary).toBe(true);\r
63 });\r
64 });\r
65 \r
66 describe("headers", function() {\r
67 it("should default to no headers", function() {\r
68 makeProxy();\r
69 proxy.read(operation);\r
70 expect(ajax.headers).toBeUndefined();\r
71 });\r
72 \r
73 it("should pass headers", function() {\r
74 makeProxy({\r
75 headers: {\r
76 'Content-Type': 'text/plain'\r
77 }\r
78 });\r
79 proxy.read(operation);\r
80 expect(ajax.headers).toEqual({\r
81 'Content-Type': 'text/plain'\r
82 });\r
83 });\r
84 });\r
85 \r
86 describe("timeout", function() {\r
87 it("should use the default timeout", function() {\r
88 makeProxy();\r
89 proxy.read(operation);\r
90 expect(ajax.timeout).toBe(proxy.getTimeout());\r
91 });\r
92 \r
93 it("should use a passed timeout", function() {\r
94 makeProxy({\r
95 timeout: 1000\r
96 });\r
97 proxy.read(operation);\r
98 expect(ajax.timeout).toBe(1000);\r
99 });\r
100 });\r
101 \r
102 describe("useDefaultXhrHeader", function() {\r
103 it("should default to true", function() {\r
104 makeProxy();\r
105 proxy.read(operation);\r
106 expect(ajax.useDefaultXhrHeader).toBe(true);\r
107 });\r
108 \r
109 it("should pass along useDefaultXhrHeader", function() {\r
110 makeProxy({\r
111 useDefaultXhrHeader: true\r
112 });\r
113 proxy.read(operation);\r
114 expect(ajax.useDefaultXhrHeader).toBe(true);\r
115 });\r
116 });\r
117 \r
118 describe("withCredentials", function() {\r
119 it("should default to false", function() {\r
120 makeProxy();\r
121 proxy.read(operation);\r
122 expect(ajax.withCredentials).toBe(false);\r
123 });\r
124 \r
125 it("should should pass the username/password", function() {\r
126 makeProxy({\r
127 withCredentials: true,\r
128 username: 'foo',\r
129 password: 'bar'\r
130 });\r
131 proxy.read(operation);\r
132 expect(ajax.withCredentials).toBe(true);\r
133 expect(ajax.username).toBe('foo');\r
134 expect(ajax.password).toBe('bar');\r
135 });\r
136 });\r
137 \r
138 describe("paramsAsJson", function(){\r
139 it("should always send as params when using get", function(){\r
140 proxy = new Ext.data.proxy.Ajax({\r
141 url: 'fake',\r
142 paramsAsJson: true\r
143 });\r
144 operation.setParams({\r
145 id: 1\r
146 });\r
147 proxy.read(operation);\r
148 expect(ajax.params.id).toBe(1);\r
149 expect(ajax.jsonData).toBeUndefined();\r
150 });\r
151\r
152 it("should send as params when paramsAsJson is false", function(){\r
153 proxy = new Ext.data.proxy.Ajax({\r
154 url: 'fake',\r
155 paramsAsJson: false\r
156 });\r
157 operation = new Ext.data.operation.Create();\r
158 operation.setParams({\r
159 id: 1\r
160 });\r
161 proxy.create(operation);\r
162 expect(ajax.params.id).toBe(1);\r
163 expect(ajax.jsonData).toBeUndefined();\r
164 });\r
165\r
166 it("should send as jsonData with non-get action and paramsAsJson: true", function(){\r
167 proxy = new Ext.data.proxy.Ajax({\r
168 url: 'fake',\r
169 paramsAsJson: true\r
170 });\r
171 operation = new Ext.data.operation.Create();\r
172 operation.setParams({\r
173 id: 1\r
174 });\r
175 proxy.create(operation);\r
176 expect(ajax.jsonData).toEqual({\r
177 id: 1\r
178 });\r
179 expect(ajax.params).toBeUndefined();\r
180 });\r
181\r
182 it("should not overwrite existing jsonData, but merge them", function() {\r
183 proxy = new Ext.data.proxy.Ajax({\r
184 url: 'fake',\r
185 paramsAsJson: true,\r
186 writer: {\r
187 type: 'json',\r
188 writeRecordId: false\r
189 }\r
190 });\r
191 var Model = Ext.define(null, {\r
192 extend: 'Ext.data.Model',\r
193 fields: ['name']\r
194 });\r
195\r
196 operation = new Ext.data.operation.Create({\r
197 records: [new Model({\r
198 name: 'X'\r
199 })]\r
200 });\r
201 operation.setParams({\r
202 foo: 1\r
203 });\r
204 proxy.create(operation);\r
205 expect(ajax.jsonData).toEqual({\r
206 name: 'X',\r
207 foo: 1\r
208 });\r
209 });\r
210 });\r
211 });\r
212 \r
213 describe("request result", function() {\r
214 var operation, request;\r
215 function complete(status, statusText, responseText) {\r
216 Ext.Ajax.mockComplete({\r
217 status: status || 200,\r
218 statusText: statusText || '',\r
219 responseText: Ext.isDefined(responseText) ? responseText : '{"success": true, "data": []}'\r
220 });\r
221 }\r
222 \r
223 beforeEach(function() {\r
224 Ext.define('spec.AjaxModel', {\r
225 extend: 'Ext.data.Model',\r
226 fields: ['id']\r
227 });\r
228 \r
229 MockAjaxManager.addMethods();\r
230 operation = new Ext.data.operation.Read();\r
231 makeProxy({\r
232 model: spec.AjaxModel,\r
233 reader: {\r
234 type: 'json',\r
235 rootProperty: 'data',\r
236 successProperty: 'success'\r
237 }\r
238 });\r
239 });\r
240 \r
241 afterEach(function() {\r
242 request = operation = null;\r
243 MockAjaxManager.removeMethods();\r
244 Ext.undefine('spec.AjaxModel');\r
245 });\r
246\r
247 it("should return the null result set if status 204 is returned", function() {\r
248 request = proxy.read(operation);\r
249 spyOn(proxy, 'afterRequest');\r
250 complete(204, 'No Content', '');\r
251 expect(operation.getResultSet()).toBe(Ext.data.reader.Reader.prototype.nullResultSet);\r
252 });\r
253 \r
254 describe("successful request", function() {\r
255 it("should call afterRequest with the request & the success status", function() {\r
256 request = proxy.read(operation);\r
257 spyOn(proxy, 'afterRequest');\r
258 complete(200);\r
259 expect(proxy.afterRequest).toHaveBeenCalledWith(request, true);\r
260 });\r
261 \r
262 describe("reader success", function() {\r
263 it("should process the operation", function() {\r
264 proxy.read(operation);\r
265 spyOn(operation, 'process');\r
266 complete(200);\r
267 expect(operation.process).toHaveBeenCalled();\r
268 });\r
269 \r
270 it("should not fire the exception event", function() {\r
271 var spy = jasmine.createSpy();\r
272 proxy.on('exception', spy);\r
273 proxy.read(operation);\r
274 complete(200);\r
275 expect(spy).not.toHaveBeenCalled();\r
276 });\r
277 });\r
278 \r
279 describe("reader failure", function() {\r
280 it("should process the operation", function() {\r
281 spyOn(operation, 'process');\r
282 proxy.read(operation);\r
283 complete(200, '', '{"success": false}');\r
284 expect(operation.process).toHaveBeenCalled();\r
285 });\r
286 \r
287 it("should fire the exception event", function() {\r
288 var spy = jasmine.createSpy();\r
289 proxy.on('exception', spy);\r
290 proxy.read(operation);\r
291 complete(200, '', '{"success": false}');\r
292 var args = spy.mostRecentCall.args;\r
293 expect(args[0]).toBe(proxy);\r
294 expect(args[1].responseText).toBe('{"success": false}');\r
295 expect(args[2]).toBe(operation);\r
296 });\r
297 });\r
298 });\r
299 \r
300 describe("failed request", function() {\r
301 it("should call afterRequest with the request & the success status", function() {\r
302 request = proxy.read(operation);\r
303 spyOn(proxy, 'afterRequest');\r
304 complete(500);\r
305 expect(proxy.afterRequest).toHaveBeenCalledWith(request, false);\r
306 });\r
307 \r
308 describe("server error", function() {\r
309 beforeEach(function() {\r
310 proxy.read(operation);\r
311 });\r
312 \r
313 it("should set an exception on the operation", function() {\r
314 complete(500, 'failStatus');\r
315 expect(operation.wasSuccessful()).toBe(false);\r
316 expect(operation.getError()).toEqual({\r
317 status: 500,\r
318 statusText: 'failStatus',\r
319 response: jasmine.any(Object)\r
320 });\r
321 });\r
322 \r
323 it("should fire the exception event and pass the proxy, response & operation", function() {\r
324 var spy = jasmine.createSpy();\r
325 proxy.on('exception', spy);\r
326 complete(500, '', 'someResponse');\r
327 var args = spy.mostRecentCall.args;\r
328 expect(args[0]).toBe(proxy);\r
329 expect(args[1].responseText).toBe('someResponse');\r
330 expect(args[2]).toBe(operation);\r
331 });\r
332 });\r
333 \r
334 describe("timeout", function() {\r
335 it("should set an exception on the operation", function() {\r
336 proxy.setTimeout(1);\r
337 request = proxy.read(operation);\r
338 waitsFor(function() {\r
339 return operation.isComplete();\r
340 }, "Operation never completed");\r
341 \r
342 runs(function() {\r
343 expect(operation.wasSuccessful()).toBe(false);\r
344 expect(operation.getError()).toEqual({\r
345 status: 0,\r
346 statusText: 'communication failure',\r
347 response: jasmine.any(Object)\r
348 });\r
349 });\r
350 });\r
351 \r
352 it("should fire the exception event and pass the proxy, response & operation", function() {\r
353 var spy = jasmine.createSpy();\r
354 proxy.on('exception', spy);\r
355 \r
356 proxy.setTimeout(1);\r
357 request = proxy.read(operation);\r
358 \r
359 waitsFor(function() {\r
360 return operation.isComplete();\r
361 }, "Operation never completed");\r
362 \r
363 runs(function() {\r
364 var args = spy.mostRecentCall.args;\r
365 expect(args[0]).toBe(proxy);\r
366 expect(args[1].statusText).toBe('communication failure');\r
367 expect(args[2]).toBe(operation);\r
368 });\r
369 });\r
370 });\r
371 });\r
372 });\r
373\r
374 describe("getMethod", function(){\r
375 var request;\r
376 beforeEach(function() {\r
377 makeProxy();\r
378 request = new Ext.data.Request({\r
379 url: "/",\r
380 action: "read"\r
381 });\r
382 });\r
383\r
384 it("should return the HTTP method name for a given request", function() {\r
385 expect(proxy.getMethod(request)).toBe('GET');\r
386 });\r
387 \r
388 it("should return a the default action method if the actionMethods property is overridden", function() {\r
389 proxy.setActionMethods({\r
390 update: 'PUT' \r
391 });\r
392 expect(proxy.getMethod(request)).toBe('GET');\r
393 });\r
394 \r
395 it("should return a value when actionMethods is undefined/null", function() {\r
396 proxy.setActionMethods(undefined);\r
397 expect(proxy.getMethod(request)).toBe('GET');\r
398 });\r
399 });\r
400});\r