73bcce88
luigser
COMPONENTS
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!doctype html>
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<title>iron-ajax</title>
<script src="../../webcomponentsjs/webcomponents.js"></script>
<script src="../../web-component-tester/browser.js"></script>
|
c5169e0e
Renato De Donato
a new hope
|
16
|
<script src="../../test-fixture/test-fixture-mocha.js"></script>
|
73bcce88
luigser
COMPONENTS
|
17
18
|
<link rel="import" href="../../polymer/polymer.html">
|
e619a3b0
Luigi Serra
Controllet cross ...
|
19
|
<link rel="import" href="../../promise-polyfill/promise-polyfill.html">
|
c5169e0e
Renato De Donato
a new hope
|
20
|
<link rel="import" href="../../test-fixture/test-fixture.html">
|
73bcce88
luigser
COMPONENTS
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<link rel="import" href="../iron-ajax.html">
</head>
<body>
<test-fixture id="TrivialGet">
<template>
<iron-ajax url="/responds_to_get_with_json"></iron-ajax>
</template>
</test-fixture>
<test-fixture id="ParamsGet">
<template>
<iron-ajax url="/responds_to_get_with_json"
params='{"a": "a"}'></iron-ajax>
</template>
</test-fixture>
<test-fixture id="AutoGet">
<template>
<iron-ajax auto url="/responds_to_get_with_json"></iron-ajax>
</template>
</test-fixture>
|
e619a3b0
Luigi Serra
Controllet cross ...
|
40
41
42
43
44
|
<test-fixture id="GetEcho">
<template>
<iron-ajax handle-as="json" url="/echoes_request_url"></iron-ajax>
</template>
</test-fixture>
|
73bcce88
luigser
COMPONENTS
|
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<test-fixture id="TrivialPost">
<template>
<iron-ajax method="POST"
url="/responds_to_post_with_json"></iron-ajax>
</template>
</test-fixture>
<test-fixture id="DebouncedGet">
<template>
<iron-ajax auto
url="/responds_to_debounced_get_with_json"
debounce-duration="150"></iron-ajax>
</template>
</test-fixture>
|
f748e9cf
Luigi Serra
new controllet an...
|
58
59
60
61
62
|
<test-fixture id='BlankUrl'>
<template>
<iron-ajax auto handle-as='text'></iron-ajax>
</template>
</test-fixture>
|
73bcce88
luigser
COMPONENTS
|
63
64
65
66
67
68
69
70
71
72
73
|
<!-- note(rictic):
This makes us dependent on a third-party server, but we need to be able
to check what headers the browser actually sends on the wire.
If necessary we can spin up our own httpbin server, as the code is open
source.
-->
<test-fixture id="RealPost">
<template>
<iron-ajax method="POST" url="http://httpbin.org/post"></iron-ajax>
</template>
</test-fixture>
|
eb240478
Luigi Serra
public room cards...
|
74
75
76
77
78
|
<test-fixture id="Delay">
<template>
<iron-ajax url="http://httpbin.org/delay/1"></iron-ajax>
</template>
</test-fixture>
|
73bcce88
luigser
COMPONENTS
|
79
|
<script>
|
eb240478
Luigi Serra
public room cards...
|
80
|
'use strict';
|
73bcce88
luigser
COMPONENTS
|
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
suite('<iron-ajax>', function () {
var responseHeaders = {
json: { 'Content-Type': 'application/json' },
plain: { 'Content-Type': 'text/plain' }
};
var ajax;
var request;
var server;
function timePasses(ms) {
return new Promise(function(resolve) {
window.setTimeout(function() {
resolve();
}, ms);
});
}
setup(function() {
server = sinon.fakeServer.create();
server.respondWith(
'GET',
/\/responds_to_get_with_json.*/,
[
200,
responseHeaders.json,
'{"success":true}'
]
);
server.respondWith(
'POST',
'/responds_to_post_with_json',
[
200,
responseHeaders.json,
'{"post_success":true}'
]
);
server.respondWith(
'GET',
'/responds_to_get_with_text',
[
200,
responseHeaders.plain,
'Hello World'
]
);
server.respondWith(
'GET',
'/responds_to_debounced_get_with_json',
[
200,
responseHeaders.json,
'{"success": "true"}'
]
);
|
eb240478
Luigi Serra
public room cards...
|
140
141
142
143
144
145
146
147
148
149
|
server.respondWith(
'GET',
'/responds_to_get_with_502_error_json',
[
502,
responseHeaders.json,
'{"message": "an error has occurred"}'
]
);
|
73bcce88
luigser
COMPONENTS
|
150
151
152
153
154
155
156
|
ajax = fixture('TrivialGet');
});
teardown(function() {
server.restore();
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// Echo requests are responded to individually and on demand, unlike the
// others in this file which are responded to with server.respond(),
// which responds to all open requests.
// We don't use server.respondWith here because there's no way to use it
// and only respond to a subset of requests.
// This way we can test for delayed and out of order responses and
// distinquish them by their responses.
function respondToEchoRequest(request) {
request.respond(200, responseHeaders.json, JSON.stringify({
url: request.url
}));
}
|
73bcce88
luigser
COMPONENTS
|
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
suite('when making simple GET requests for JSON', function() {
test('has sane defaults that love you', function() {
request = ajax.generateRequest();
server.respond();
expect(request.response).to.be.ok;
expect(request.response).to.be.an('object');
expect(request.response.success).to.be.equal(true);
});
test('will be asynchronous by default', function() {
expect(ajax.toRequestOptions().async).to.be.eql(true);
});
});
suite('when setting custom headers', function() {
test('are present in the request headers', function() {
ajax.headers['custom-header'] = 'valid';
var options = ajax.toRequestOptions();
expect(options.headers).to.be.ok;
expect(options.headers['custom-header']).to.be.an('string');
expect(options.headers.hasOwnProperty('custom-header')).to.be.equal(
true);
});
test('non-objects in headers are not applied', function() {
ajax.headers = 'invalid';
var options = ajax.toRequestOptions();
expect(Object.keys(options.headers).length).to.be.equal(0);
});
});
|
f748e9cf
Luigi Serra
new controllet an...
|
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
suite('when url isn\'t set yet', function() {
test('we don\'t fire any automatic requests', function() {
expect(server.requests.length).to.be.equal(0);
ajax = fixture('BlankUrl');
return timePasses(1).then(function() {
// We don't make any requests.
expect(server.requests.length).to.be.equal(0);
// Explicitly asking for the request to fire works.
ajax.generateRequest();
expect(server.requests.length).to.be.equal(1);
server.requests = [];
// Explicitly setting url to '' works too.
ajax = fixture('BlankUrl');
ajax.url = '';
return timePasses(1);
}).then(function() {
expect(server.requests.length).to.be.equal(1);
});
});
});
|
73bcce88
luigser
COMPONENTS
|
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
suite('when properties are changed', function() {
test('generates simple-request elements that reflect the change', function() {
request = ajax.generateRequest();
expect(request.xhr.method).to.be.equal('GET');
ajax.method = 'POST';
ajax.url = '/responds_to_post_with_json';
request = ajax.generateRequest();
expect(request.xhr.method).to.be.equal('POST');
});
});
suite('when generating a request', function() {
test('yields an iron-request instance', function() {
var IronRequest = document.createElement('iron-request').constructor;
expect(ajax.generateRequest()).to.be.instanceOf(IronRequest);
});
|
eb240478
Luigi Serra
public room cards...
|
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
test('correctly adds params to a URL that already has some', function() {
ajax.url += '?a=b';
ajax.params = {'c': 'd'};
expect(ajax.requestUrl).to.be.equal('/responds_to_get_with_json?a=b&c=d')
})
test('encodes params properly', function() {
ajax.params = {'a b,c': 'd e f'};
expect(ajax.queryString).to.be.equal('a%20b%2Cc=d%20e%20f');
});
test('encodes array params properly', function() {
ajax.params = {'a b': ['c','d e', 'f']};
expect(ajax.queryString).to.be.equal('a%20b=c&a%20b=d%20e&a%20b=f');
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
269
|
test('reflects the loading state in the `loading` property', function() {
|
73bcce88
luigser
COMPONENTS
|
270
271
272
273
274
275
|
var request = ajax.generateRequest();
expect(ajax.loading).to.be.equal(true);
server.respond();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
276
277
278
|
return request.completes.then(function() {
return timePasses(1);
}).then(function() {
|
73bcce88
luigser
COMPONENTS
|
279
|
expect(ajax.loading).to.be.equal(false);
|
73bcce88
luigser
COMPONENTS
|
280
281
282
283
284
285
|
});
});
});
suite('when there are multiple requests', function() {
var requests;
|
e619a3b0
Luigi Serra
Controllet cross ...
|
286
287
|
var echoAjax;
var promiseAllComplete;
|
73bcce88
luigser
COMPONENTS
|
288
289
|
setup(function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
290
|
echoAjax = fixture('GetEcho');
|
73bcce88
luigser
COMPONENTS
|
291
292
293
|
requests = [];
for (var i = 0; i < 3; ++i) {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
294
295
|
echoAjax.params = {'order': i + 1};
requests.push(echoAjax.generateRequest());
|
73bcce88
luigser
COMPONENTS
|
296
|
}
|
e619a3b0
Luigi Serra
Controllet cross ...
|
297
298
|
var allPromises = requests.map(function(r){return r.completes});
promiseAllComplete = Promise.all(allPromises);
|
73bcce88
luigser
COMPONENTS
|
299
300
301
|
});
test('holds all requests in the `activeRequests` Array', function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
302
|
expect(requests).to.deep.eql(echoAjax.activeRequests);
|
73bcce88
luigser
COMPONENTS
|
303
304
|
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
test('empties `activeRequests` when requests are completed', function() {
expect(echoAjax.activeRequests.length).to.be.equal(3);
for (var i = 0; i < 3; i++) {
respondToEchoRequest(server.requests[i]);
}
return promiseAllComplete.then(function() {
return timePasses(1);
}).then(function() {
expect(echoAjax.activeRequests.length).to.be.equal(0);
});
});
test('avoids race conditions with last response', function() {
expect(echoAjax.lastResponse).to.be.equal(undefined);
// Resolving the oldest request doesn't update lastResponse.
respondToEchoRequest(server.requests[0]);
return requests[0].completes.then(function() {
expect(echoAjax.lastResponse).to.be.equal(undefined);
// Resolving the most recent request does!
respondToEchoRequest(server.requests[2]);
return requests[2].completes;
}).then(function() {
expect(echoAjax.lastResponse).to.be.deep.eql(
{url: '/echoes_request_url?order=3'});
// Resolving an out of order stale request after does nothing!
respondToEchoRequest(server.requests[1]);
return requests[1].completes;
}).then(function() {
expect(echoAjax.lastResponse).to.be.deep.eql(
{url: '/echoes_request_url?order=3'});
});
});
test('`loading` is true while the last one is loading', function() {
expect(echoAjax.loading).to.be.equal(true);
respondToEchoRequest(server.requests[0]);
return requests[0].completes.then(function() {
// We're still loading because requests[2] is the most recently
// made request.
expect(echoAjax.loading).to.be.equal(true);
respondToEchoRequest(server.requests[2]);
return requests[2].completes;
}).then(function() {
// Now we're done loading.
expect(echoAjax.loading).to.be.eql(false);
// Resolving an out of order stale request after should have
// no effect.
respondToEchoRequest(server.requests[1]);
return requests[1].completes;
}).then(function() {
expect(echoAjax.loading).to.be.eql(false);
|
73bcce88
luigser
COMPONENTS
|
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
});
});
});
suite('when params are changed', function() {
test('generates a request that reflects the change', function() {
ajax = fixture('ParamsGet');
request = ajax.generateRequest();
expect(request.xhr.url).to.be.equal('/responds_to_get_with_json?a=a');
ajax.params = {b: 'b'};
request = ajax.generateRequest();
expect(request.xhr.url).to.be.equal('/responds_to_get_with_json?b=b');
});
});
suite('when `auto` is enabled', function() {
setup(function() {
ajax = fixture('AutoGet');
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
386
387
388
389
390
|
test('automatically generates new requests', function() {
return new Promise(function(resolve) {
ajax.addEventListener('request', function() {
resolve();
});
|
73bcce88
luigser
COMPONENTS
|
391
392
393
|
});
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
394
395
396
397
398
|
test('does not send requests if url is not a string', function() {
return new Promise(function(resolve, reject) {
ajax.addEventListener('request', function() {
reject('A request was generated but url is null!');
});
|
73bcce88
luigser
COMPONENTS
|
399
|
|
e619a3b0
Luigi Serra
Controllet cross ...
|
400
401
|
ajax.url = null;
ajax.handleAs = 'text';
|
73bcce88
luigser
COMPONENTS
|
402
|
|
e619a3b0
Luigi Serra
Controllet cross ...
|
403
404
405
406
|
Polymer.Base.async(function() {
resolve();
}, 1);
});
|
73bcce88
luigser
COMPONENTS
|
407
408
|
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
test('deduplicates multiple changes to a single request', function() {
return new Promise(function(resolve, reject) {
ajax.addEventListener('request', function() {
server.respond();
});
ajax.addEventListener('response', function() {
try {
expect(ajax.activeRequests.length).to.be.eql(1);
resolve()
} catch (e) {
reject(e);
}
});
ajax.handleas = 'text';
ajax.params = { foo: 'bar' };
ajax.headers = { 'X-Foo': 'Bar' };
});
});
|
eb240478
Luigi Serra
public room cards...
|
429
|
|
e619a3b0
Luigi Serra
Controllet cross ...
|
430
|
test('automatically generates new request when a sub-property of params is changed', function(done) {
|
73bcce88
luigser
COMPONENTS
|
431
432
433
|
ajax.addEventListener('request', function() {
server.respond();
});
|
eb240478
Luigi Serra
public room cards...
|
434
|
|
e619a3b0
Luigi Serra
Controllet cross ...
|
435
|
ajax.params = { foo: 'bar' };
|
73bcce88
luigser
COMPONENTS
|
436
|
ajax.addEventListener('response', function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
437
|
ajax.addEventListener('request', function() {
|
73bcce88
luigser
COMPONENTS
|
438
|
done();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
439
|
});
|
eb240478
Luigi Serra
public room cards...
|
440
|
|
e619a3b0
Luigi Serra
Controllet cross ...
|
441
|
ajax.set('params.foo', 'xyz');
|
73bcce88
luigser
COMPONENTS
|
442
|
});
|
73bcce88
luigser
COMPONENTS
|
443
444
445
446
447
448
449
450
451
|
});
});
suite('the last response', function() {
setup(function() {
request = ajax.generateRequest();
server.respond();
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
452
453
|
test('is accessible as a readonly property', function() {
return request.completes.then(function (request) {
|
73bcce88
luigser
COMPONENTS
|
454
|
expect(ajax.lastResponse).to.be.equal(request.response);
|
e619a3b0
Luigi Serra
Controllet cross ...
|
455
|
});
|
73bcce88
luigser
COMPONENTS
|
456
457
458
|
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
459
460
|
test('updates with each new response', function() {
return request.completes.then(function(request) {
|
73bcce88
luigser
COMPONENTS
|
461
462
463
464
465
466
467
468
469
|
expect(request.response).to.be.an('object');
expect(ajax.lastResponse).to.be.equal(request.response);
ajax.handleAs = 'text';
request = ajax.generateRequest();
server.respond();
return request.completes;
|
73bcce88
luigser
COMPONENTS
|
470
|
}).then(function(request) {
|
73bcce88
luigser
COMPONENTS
|
471
472
|
expect(request.response).to.be.a('string');
expect(ajax.lastResponse).to.be.equal(request.response);
|
e619a3b0
Luigi Serra
Controllet cross ...
|
473
|
});
|
73bcce88
luigser
COMPONENTS
|
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
});
});
suite('when making POST requests', function() {
setup(function() {
ajax = fixture('TrivialPost');
});
test('POSTs the value of the `body` attribute', function() {
var requestBody = JSON.stringify({foo: 'bar'});
ajax.body = requestBody;
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal(requestBody);
});
test('if `contentType` is set to form encode, the body is encoded',function() {
ajax.body = {foo: 'bar\nbip', 'biz bo': 'baz blar'};
ajax.contentType = 'application/x-www-form-urlencoded';
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal(
'foo=bar%0D%0Abip&biz+bo=baz+blar');
});
test('if `contentType` is json, the body is json encoded', function() {
var requestObj = {foo: 'bar', baz: [1,2,3]}
ajax.body = requestObj;
ajax.contentType = 'application/json';
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal(
JSON.stringify(requestObj));
});
suite('the examples in the documentation work', function() {
test('json content, body attribute is an object', function() {
ajax.setAttribute('body', '{"foo": "bar baz", "x": 1}');
ajax.contentType = 'application/json';
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal(
'{"foo":"bar baz","x":1}');
});
test('form content, body attribute is an object', function() {
ajax.setAttribute('body', '{"foo": "bar baz", "x": 1}');
ajax.contentType = 'application/x-www-form-urlencoded';
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal(
'foo=bar+baz&x=1');
});
});
suite('and `contentType` is explicitly set to form encode', function() {
test('we encode a custom object', function() {
function Foo(bar) { this.bar = bar };
var requestObj = new Foo('baz');
ajax.body = requestObj;
ajax.contentType = 'application/x-www-form-urlencoded';
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal('bar=baz');
});
})
suite('and `contentType` isn\'t set', function() {
test('we don\'t try to encode an ArrayBuffer', function() {
var requestObj = new ArrayBuffer()
ajax.body = requestObj;
ajax.generateRequest();
expect(server.requests[0]).to.be.ok;
// We give the browser the ArrayBuffer directly, without trying
// to encode it.
expect(server.requests[0].requestBody).to.be.equal(requestObj);
});
})
});
suite('when debouncing requests', function() {
setup(function() {
ajax = fixture('DebouncedGet');
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
567
|
test('only requests a single resource', function() {
|
73bcce88
luigser
COMPONENTS
|
568
569
570
|
ajax._requestOptionsChanged();
expect(server.requests[0]).to.be.equal(undefined);
ajax._requestOptionsChanged();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
571
|
return timePasses(200).then(function() {
|
73bcce88
luigser
COMPONENTS
|
572
|
expect(server.requests[0]).to.be.ok;
|
e619a3b0
Luigi Serra
Controllet cross ...
|
573
|
});
|
73bcce88
luigser
COMPONENTS
|
574
575
576
577
578
579
580
581
582
583
584
|
});
});
suite('when a response handler is bound', function() {
var responseHandler;
setup(function() {
responseHandler = sinon.spy();
ajax.addEventListener('response', responseHandler);
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
585
|
test('calls the handler after every response', function() {
|
73bcce88
luigser
COMPONENTS
|
586
587
588
589
590
|
ajax.generateRequest();
ajax.generateRequest();
server.respond();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
591
|
return ajax.lastRequest.completes.then(function() {
|
73bcce88
luigser
COMPONENTS
|
592
|
expect(responseHandler.callCount).to.be.equal(2);
|
e619a3b0
Luigi Serra
Controllet cross ...
|
593
|
});
|
73bcce88
luigser
COMPONENTS
|
594
595
596
597
598
599
600
601
|
});
});
suite('when the response type is `json`', function() {
setup(function() {
server.restore();
});
|
e619a3b0
Luigi Serra
Controllet cross ...
|
602
|
test('finds the JSON on any platform', function() {
|
73bcce88
luigser
COMPONENTS
|
603
604
|
ajax.url = '../bower.json';
request = ajax.generateRequest();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
605
|
return request.completes.then(function() {
|
73bcce88
luigser
COMPONENTS
|
606
|
expect(ajax.lastResponse).to.be.instanceOf(Object);
|
73bcce88
luigser
COMPONENTS
|
607
608
609
610
611
612
|
});
});
});
suite('when handleAs parameter is `text`', function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
613
|
test('response type is string', function () {
|
73bcce88
luigser
COMPONENTS
|
614
615
616
617
|
ajax.url = '/responds_to_get_with_json';
ajax.handleAs = 'text';
request = ajax.generateRequest();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
618
|
var promise = request.completes.then(function () {
|
73bcce88
luigser
COMPONENTS
|
619
|
expect(typeof(ajax.lastResponse)).to.be.equal('string');
|
73bcce88
luigser
COMPONENTS
|
620
621
|
});
|
f748e9cf
Luigi Serra
new controllet an...
|
622
623
624
|
expect(server.requests.length).to.be.equal(1);
expect(server.requests[0].requestHeaders['accept']).to.be.equal(
'text/plain');
|
73bcce88
luigser
COMPONENTS
|
625
626
|
server.respond();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
627
|
return promise;
|
73bcce88
luigser
COMPONENTS
|
628
629
630
631
632
|
});
});
suite('when a request fails', function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
633
|
test('we give an error with useful details', function() {
|
eb240478
Luigi Serra
public room cards...
|
634
|
ajax.url = '/responds_to_get_with_502_error_json';
|
73bcce88
luigser
COMPONENTS
|
635
|
ajax.handleAs = 'json';
|
e619a3b0
Luigi Serra
Controllet cross ...
|
636
|
var eventFired = false;
|
73bcce88
luigser
COMPONENTS
|
637
|
ajax.addEventListener('error', function(event) {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
638
639
640
641
|
expect(event.detail.request).to.be.okay;
expect(event.detail.error).to.be.okay;
eventFired = true;
});
|
eb240478
Luigi Serra
public room cards...
|
642
643
|
var request = ajax.generateRequest();
var promise = request.completes.then(function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
644
645
646
|
throw new Error('Expected the request to fail!');
}, function(error) {
expect(error).to.be.instanceof(Error);
|
eb240478
Luigi Serra
public room cards...
|
647
|
expect(request.succeeded).to.be.eq(false);
|
e619a3b0
Luigi Serra
Controllet cross ...
|
648
649
650
|
return timePasses(100);
}).then(function() {
expect(eventFired).to.be.eq(true);
|
eb240478
Luigi Serra
public room cards...
|
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
expect(ajax.lastError).to.not.be.eq(null);
});
server.respond();
return promise;
});
test('we give a useful error even when the domain doesn\'t resolve', function() {
ajax.url = 'http://nonexistant.example.com/';
server.restore();
var eventFired = false;
ajax.addEventListener('error', function(event) {
expect(event.detail.request).to.be.okay;
expect(event.detail.error).to.be.okay;
eventFired = true;
});
var request = ajax.generateRequest();
var promise = request.completes.then(function() {
throw new Error('Expected the request to fail!');
}, function(error) {
expect(request.succeeded).to.be.eq(false);
expect(error).to.not.be.eq(null);
return timePasses(100);
}).then(function() {
expect(eventFired).to.be.eq(true);
expect(ajax.lastError).to.not.be.eq(null);
|
73bcce88
luigser
COMPONENTS
|
678
679
680
|
});
server.respond();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
681
682
|
return promise;
|
73bcce88
luigser
COMPONENTS
|
683
684
685
686
687
|
});
});
suite('when handleAs parameter is `json`', function() {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
688
|
test('response type is string', function () {
|
73bcce88
luigser
COMPONENTS
|
689
690
691
692
|
ajax.url = '/responds_to_get_with_json';
ajax.handleAs = 'json';
request = ajax.generateRequest();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
693
|
var promise = request.completes.then(function () {
|
73bcce88
luigser
COMPONENTS
|
694
|
expect(typeof(ajax.lastResponse)).to.be.equal('object');
|
73bcce88
luigser
COMPONENTS
|
695
696
|
});
|
f748e9cf
Luigi Serra
new controllet an...
|
697
698
699
700
|
expect(server.requests.length).to.be.equal(1);
expect(server.requests[0].requestHeaders['accept']).to.be.equal(
'application/json');
|
73bcce88
luigser
COMPONENTS
|
701
702
|
server.respond();
|
e619a3b0
Luigi Serra
Controllet cross ...
|
703
|
return promise;
|
73bcce88
luigser
COMPONENTS
|
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
});
});
suite('when making a POST over the wire', function() {
test('FormData is handled correctly', function() {
server.restore();
var requestBody = new FormData();
requestBody.append('a', 'foo');
requestBody.append('b', 'bar');
var ajax = fixture('RealPost');
ajax.body = requestBody;
return ajax.generateRequest().completes.then(function() {
expect(ajax.lastResponse.headers['Content-Type']).to.match(
/^multipart\/form-data; boundary=.*$/);
expect(ajax.lastResponse.form.a).to.be.equal('foo');
expect(ajax.lastResponse.form.b).to.be.equal('bar');
});
});
test('json is handled correctly', function() {
server.restore();
var ajax = fixture('RealPost');
ajax.body = JSON.stringify({a: 'foo', b: 'bar'});
ajax.contentType = 'application/json';
return ajax.generateRequest().completes.then(function() {
expect(ajax.lastResponse.headers['Content-Type']).to.match(
/^application\/json(;.*)?$/);
expect(ajax.lastResponse.json.a).to.be.equal('foo');
expect(ajax.lastResponse.json.b).to.be.equal('bar');
});
});
test('urlencoded data is handled correctly', function() {
server.restore();
var ajax = fixture('RealPost');
ajax.body = 'a=foo&b=bar';
return ajax.generateRequest().completes.then(function() {
expect(ajax.lastResponse.headers['Content-Type']).to.match(
/^application\/x-www-form-urlencoded(;.*)?$/);
expect(ajax.lastResponse.form.a).to.be.equal('foo');
expect(ajax.lastResponse.form.b).to.be.equal('bar');
});
});
test('xml is handled correctly', function() {
server.restore();
var ajax = fixture('RealPost');
var xmlDoc = document.implementation.createDocument(
null, "foo", null);
var node = xmlDoc.createElement("bar");
node.setAttribute("name" , "baz");
xmlDoc.documentElement.appendChild(node);
ajax.body = xmlDoc;
return ajax.generateRequest().completes.then(function() {
expect(ajax.lastResponse.headers['Content-Type']).to.match(
/^application\/xml(;.*)?$/);
expect(ajax.lastResponse.data).to.match(
/<foo\s*><bar\s+name="baz"\s*\/><\/foo\s*>/);
});
});
});
|
eb240478
Luigi Serra
public room cards...
|
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
|
suite('when setting timeout', function() {
setup(function() {
server.restore();
});
test('it is present in the request xhr object', function () {
ajax.url = '/responds_to_get_with_json';
ajax.timeout = 5000; // 5 Seconds
request = ajax.generateRequest();
expect(request.xhr.timeout).to.be.equal(5000); // 5 Seconds
});
test('it fails once that timeout is reached', function () {
var ajax = fixture('Delay');
ajax.timeout = 1; // 1 Millisecond
request = ajax.generateRequest();
return request.completes.then(function () {
throw new Error('Expected the request to throw an error.');
}, function() {
expect(request.succeeded).to.be.equal(false);
expect(request.xhr.status).to.be.equal(0);
expect(request.timedOut).to.be.equal(true);
return timePasses(1);
}).then(function() {
expect(ajax.loading).to.be.equal(false);
expect(ajax.lastResponse).to.be.equal(null);
expect(ajax.lastError).to.not.be.equal(null);
});
});
});
|
73bcce88
luigser
COMPONENTS
|
804
805
806
807
808
|
});
</script>
</body>
</html>
|