| 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 <script src="../../test-fixture/test-fixture-mocha.js"></script> |
| 17 |
| 18 <link rel="import" href="../../polymer/polymer.html"> |
| 19 <link rel="import" href="../../test-fixture/test-fixture.html"> |
| 20 <link rel="import" href="../iron-ajax.html"> |
| 21 </head> |
| 22 <body> |
| 23 <test-fixture id="TrivialGet"> |
| 24 <template> |
| 25 <iron-ajax url="/responds_to_get_with_json"></iron-ajax> |
| 26 </template> |
| 27 </test-fixture> |
| 28 <test-fixture id="ParamsGet"> |
| 29 <template> |
| 30 <iron-ajax url="/responds_to_get_with_json" |
| 31 params='{"a": "a"}'></iron-ajax> |
| 32 </template> |
| 33 </test-fixture> |
| 34 <test-fixture id="AutoGet"> |
| 35 <template> |
| 36 <iron-ajax auto url="/responds_to_get_with_json"></iron-ajax> |
| 37 </template> |
| 38 </test-fixture> |
| 39 <test-fixture id="TrivialPost"> |
| 40 <template> |
| 41 <iron-ajax method="POST" |
| 42 url="/responds_to_post_with_json"></iron-ajax> |
| 43 </template> |
| 44 </test-fixture> |
| 45 <test-fixture id="DebouncedGet"> |
| 46 <template> |
| 47 <iron-ajax auto |
| 48 url="/responds_to_debounced_get_with_json" |
| 49 debounce-duration="150"></iron-ajax> |
| 50 </template> |
| 51 </test-fixture> |
| 52 <script> |
| 53 suite('<iron-ajax>', function () { |
| 54 var responseHeaders = { |
| 55 json: { 'Content-Type': 'application/json' }, |
| 56 plain: { 'Content-Type': 'text/plain' } |
| 57 }; |
| 58 var ajax; |
| 59 var request; |
| 60 var server; |
| 61 |
| 62 setup(function() { |
| 63 server = sinon.fakeServer.create(); |
| 64 server.respondWith( |
| 65 'GET', |
| 66 /\/responds_to_get_with_json.*/, |
| 67 [ |
| 68 200, |
| 69 responseHeaders.json, |
| 70 '{"success":true}' |
| 71 ] |
| 72 ); |
| 73 |
| 74 server.respondWith( |
| 75 'POST', |
| 76 '/responds_to_post_with_json', |
| 77 [ |
| 78 200, |
| 79 responseHeaders.json, |
| 80 '{"post_success":true}' |
| 81 ] |
| 82 ); |
| 83 |
| 84 server.respondWith( |
| 85 'GET', |
| 86 '/responds_to_get_with_text', |
| 87 [ |
| 88 200, |
| 89 responseHeaders.plain, |
| 90 'Hello World' |
| 91 ] |
| 92 ); |
| 93 |
| 94 server.respondWith( |
| 95 'GET', |
| 96 '/responds_to_debounced_get_with_json', |
| 97 [ |
| 98 200, |
| 99 responseHeaders.json, |
| 100 '{"success": "true"}' |
| 101 ] |
| 102 ); |
| 103 |
| 104 ajax = fixture('TrivialGet'); |
| 105 }); |
| 106 |
| 107 teardown(function() { |
| 108 server.restore(); |
| 109 }); |
| 110 |
| 111 suite('when making simple GET requests for JSON', function() { |
| 112 test('has sane defaults that love you', function() { |
| 113 request = ajax.generateRequest(); |
| 114 |
| 115 server.respond(); |
| 116 |
| 117 expect(request.response).to.be.ok; |
| 118 expect(request.response).to.be.an('object'); |
| 119 expect(request.response.success).to.be.equal(true); |
| 120 }); |
| 121 |
| 122 test('will be asynchronous by default', function() { |
| 123 expect(ajax.toRequestOptions().async).to.be.eql(true); |
| 124 }); |
| 125 }); |
| 126 |
| 127 suite('when setting custom headers', function() { |
| 128 test('are present in the request headers', function() { |
| 129 ajax.headers['custom-header'] = 'valid'; |
| 130 var options = ajax.toRequestOptions(); |
| 131 |
| 132 expect(options.headers).to.be.ok; |
| 133 expect(options.headers['custom-header']).to.be.an('string'); |
| 134 expect(options.headers.hasOwnProperty('custom-header')).to.be.equal(tr
ue); |
| 135 }); |
| 136 |
| 137 test('non-objects in headers are not applied', function() { |
| 138 ajax.headers = 'invalid'; |
| 139 var options = ajax.toRequestOptions(); |
| 140 |
| 141 expect(Object.keys(options.headers).length).to.be.equal(1); |
| 142 }); |
| 143 }); |
| 144 |
| 145 suite('when properties are changed', function() { |
| 146 test('generates simple-request elements that reflect the change', functi
on() { |
| 147 request = ajax.generateRequest(); |
| 148 |
| 149 expect(request.xhr.method).to.be.equal('GET'); |
| 150 |
| 151 ajax.method = 'POST'; |
| 152 ajax.url = '/responds_to_post_with_json'; |
| 153 |
| 154 request = ajax.generateRequest(); |
| 155 |
| 156 expect(request.xhr.method).to.be.equal('POST'); |
| 157 }); |
| 158 }); |
| 159 |
| 160 suite('when generating a request', function() { |
| 161 test('yields a iron-request instance', function() { |
| 162 var IronRequest = document.createElement('iron-request').constructor; |
| 163 |
| 164 expect(ajax.generateRequest()).to.be.instanceOf(IronRequest); |
| 165 }); |
| 166 }); |
| 167 |
| 168 suite('when there are multiple requests', function() { |
| 169 var requests; |
| 170 |
| 171 setup(function() { |
| 172 requests = []; |
| 173 |
| 174 for (var i = 0; i < 3; ++i) { |
| 175 requests.push(ajax.generateRequest()); |
| 176 } |
| 177 }); |
| 178 |
| 179 test('holds all requests in the `activeRequests` Array', function() { |
| 180 expect(requests).to.deep.eql(ajax.activeRequests); |
| 181 }); |
| 182 }); |
| 183 |
| 184 suite('when params are changed', function() { |
| 185 test('generates a request that reflects the change', function() { |
| 186 ajax = fixture('ParamsGet'); |
| 187 request = ajax.generateRequest(); |
| 188 |
| 189 expect(request.xhr.url).to.be.equal('/responds_to_get_with_json?a=a'); |
| 190 |
| 191 ajax.params = {b: 'b'}; |
| 192 request = ajax.generateRequest(); |
| 193 |
| 194 expect(request.xhr.url).to.be.equal('/responds_to_get_with_json?b=b'); |
| 195 }); |
| 196 }); |
| 197 |
| 198 suite('when `auto` is enabled', function() { |
| 199 setup(function() { |
| 200 ajax = fixture('AutoGet'); |
| 201 }); |
| 202 |
| 203 test('automatically generates new requests', function(done) { |
| 204 ajax.addEventListener('request', function() { |
| 205 done(); |
| 206 }); |
| 207 }); |
| 208 |
| 209 test('does not send requests if url is not a string', function(done) { |
| 210 ajax.addEventListener('request', function() { |
| 211 done(new Error('A request was generated but url is null!')); |
| 212 }); |
| 213 |
| 214 ajax.url = null; |
| 215 ajax.handleAs = 'text'; |
| 216 |
| 217 Polymer.Base.async(function() { |
| 218 done(); |
| 219 }, 1); |
| 220 }); |
| 221 |
| 222 test('deduplicates multiple changes to a single request', function(done)
{ |
| 223 ajax.addEventListener('request', function() { |
| 224 server.respond(); |
| 225 }); |
| 226 |
| 227 ajax.addEventListener('response', function() { |
| 228 try { |
| 229 expect(ajax.activeRequests.length).to.be.eql(1); |
| 230 done(); |
| 231 } catch (e) { |
| 232 done(e); |
| 233 } |
| 234 }); |
| 235 |
| 236 ajax.handleas = 'text'; |
| 237 ajax.params = { foo: 'bar' }; |
| 238 ajax.headers = { 'X-Foo': 'Bar' }; |
| 239 }); |
| 240 }); |
| 241 |
| 242 suite('the last response', function() { |
| 243 setup(function() { |
| 244 request = ajax.generateRequest(); |
| 245 server.respond(); |
| 246 }); |
| 247 |
| 248 test('is accessible as a readonly property', function(done) { |
| 249 request.completes.then(function (request) { |
| 250 expect(ajax.lastResponse).to.be.equal(request.response); |
| 251 done(); |
| 252 }).catch(done); |
| 253 }); |
| 254 |
| 255 |
| 256 test('updates with each new response', function(done) { |
| 257 request.completes.then(function(request) { |
| 258 |
| 259 expect(request.response).to.be.an('object'); |
| 260 expect(ajax.lastResponse).to.be.equal(request.response); |
| 261 |
| 262 ajax.handleAs = 'text'; |
| 263 request = ajax.generateRequest(); |
| 264 server.respond(); |
| 265 |
| 266 return request.completes; |
| 267 |
| 268 }).then(function(request) { |
| 269 |
| 270 expect(request.response).to.be.a('string'); |
| 271 expect(ajax.lastResponse).to.be.equal(request.response); |
| 272 |
| 273 done(); |
| 274 |
| 275 }).catch(done); |
| 276 }); |
| 277 }); |
| 278 |
| 279 suite('when making POST requests', function() { |
| 280 setup(function() { |
| 281 ajax = fixture('TrivialPost'); |
| 282 }); |
| 283 |
| 284 test('POSTs the value of the `body` attribute', function() { |
| 285 var requestBody = JSON.stringify({foo: 'bar'}); |
| 286 |
| 287 ajax.body = requestBody; |
| 288 ajax.generateRequest(); |
| 289 |
| 290 expect(server.requests[0]).to.be.ok; |
| 291 expect(server.requests[0].requestBody).to.be.equal(requestBody); |
| 292 }); |
| 293 }); |
| 294 |
| 295 suite('when debouncing requests', function() { |
| 296 setup(function() { |
| 297 ajax = fixture('DebouncedGet'); |
| 298 }); |
| 299 |
| 300 test('only requests a single resource', function(done) { |
| 301 ajax.requestOptionsChanged(); |
| 302 expect(server.requests[0]).to.be.equal(undefined); |
| 303 ajax.requestOptionsChanged(); |
| 304 window.setTimeout(function() { |
| 305 expect(server.requests[0]).to.be.ok; |
| 306 done(); |
| 307 }, 200); |
| 308 }); |
| 309 }); |
| 310 |
| 311 suite('when a response handler is bound', function() { |
| 312 var responseHandler; |
| 313 |
| 314 setup(function() { |
| 315 responseHandler = sinon.spy(); |
| 316 ajax.addEventListener('response', responseHandler); |
| 317 }); |
| 318 |
| 319 test('calls the handler after every response', function(done) { |
| 320 ajax.generateRequest(); |
| 321 ajax.generateRequest(); |
| 322 |
| 323 server.respond(); |
| 324 |
| 325 ajax.lastRequest.completes.then(function() { |
| 326 expect(responseHandler.callCount).to.be.equal(2); |
| 327 done(); |
| 328 }).catch(done); |
| 329 }); |
| 330 }); |
| 331 |
| 332 suite('when the response type is `json`', function() { |
| 333 setup(function() { |
| 334 server.restore(); |
| 335 }); |
| 336 |
| 337 test('finds the JSON on any platform', function(done) { |
| 338 ajax.url = '../bower.json'; |
| 339 request = ajax.generateRequest(); |
| 340 request.completes.then(function() { |
| 341 expect(ajax.lastResponse).to.be.instanceOf(Object); |
| 342 done(); |
| 343 }).catch(function(e) { |
| 344 done(e); |
| 345 }); |
| 346 }); |
| 347 }); |
| 348 |
| 349 suite('when handleAs parameter is `text`', function() { |
| 350 |
| 351 test('response type is string', function (done) { |
| 352 |
| 353 ajax.url = '/responds_to_get_with_json'; |
| 354 ajax.handleAs = 'text'; |
| 355 |
| 356 request = ajax.generateRequest(); |
| 357 request.completes.then(function () { |
| 358 expect(typeof(ajax.lastResponse)).to.be.equal('string'); |
| 359 done(); |
| 360 }).catch(function (e) { |
| 361 done(e); |
| 362 }); |
| 363 |
| 364 server.respond(); |
| 365 |
| 366 }); |
| 367 |
| 368 }); |
| 369 |
| 370 suite('when a request fails', function() { |
| 371 test('the error event has useful details', function(done) { |
| 372 |
| 373 ajax.url = '/responds_to_get_with_text'; |
| 374 ajax.handleAs = 'json'; |
| 375 ajax.generateRequest(); |
| 376 |
| 377 ajax.addEventListener('error', function(event) { |
| 378 try { |
| 379 expect(event.detail.request).to.be.okay; |
| 380 expect(event.detail.error).to.be.okay; |
| 381 done(); |
| 382 } catch (e) { |
| 383 done(e); |
| 384 } |
| 385 }); |
| 386 |
| 387 server.respond(); |
| 388 }); |
| 389 }); |
| 390 |
| 391 suite('when handleAs parameter is `json`', function() { |
| 392 |
| 393 test('response type is string', function (done) { |
| 394 |
| 395 ajax.url = '/responds_to_get_with_json'; |
| 396 ajax.handleAs = 'json'; |
| 397 |
| 398 request = ajax.generateRequest(); |
| 399 request.completes.then(function () { |
| 400 expect(typeof(ajax.lastResponse)).to.be.equal('object'); |
| 401 done(); |
| 402 }).catch(function (e) { |
| 403 done(e); |
| 404 }); |
| 405 |
| 406 server.respond(); |
| 407 |
| 408 }); |
| 409 |
| 410 test('fails when getting invalid json data', function (done) { |
| 411 |
| 412 ajax.url = '/responds_to_get_with_text'; |
| 413 ajax.handleAs = 'json'; |
| 414 |
| 415 request = ajax.generateRequest(); |
| 416 request.completes.catch(function (e) { |
| 417 expect(e).to.be.instanceOf(Error); |
| 418 done(); |
| 419 }).catch(function (e) { |
| 420 done(e); |
| 421 }); |
| 422 |
| 423 server.respond(); |
| 424 |
| 425 }); |
| 426 |
| 427 }); |
| 428 |
| 429 }); |
| 430 </script> |
| 431 |
| 432 </body> |
| 433 </html> |
| OLD | NEW |