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