| OLD | NEW |
| (Empty) |
| 1 <!doctype html> | |
| 2 <!-- | |
| 3 Copyright (c) 2014 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>core-ajax-response-and-error</title> | |
| 13 | |
| 14 <script src="../../../platform/platform.js"></script> | |
| 15 <script src="../../../polymer-test-tools/chai/chai.js"></script> | |
| 16 <script src="../../../polymer-test-tools/htmltest.js"></script> | |
| 17 | |
| 18 <link rel="import" href="../../core-ajax.html"> | |
| 19 | |
| 20 </head> | |
| 21 <body> | |
| 22 | |
| 23 <polymer-element name="race-condition"> | |
| 24 <template> | |
| 25 <style>section {margin-top: 20px;}</style> | |
| 26 <core-ajax | |
| 27 id='ajax' | |
| 28 url="http://httpbin.org/delay/{{delay}}" | |
| 29 handleas="json" auto | |
| 30 response="{{response}}" | |
| 31 on-core-response="{{handleResponse}}"></core-ajax> | |
| 32 <div>Response url: {{response.url}}</div> | |
| 33 <div>Result: {{testResult}}</div> | |
| 34 | |
| 35 <section> | |
| 36 <div>Requests</div> | |
| 37 <ul> | |
| 38 <template repeat='{{request in requests}}'> | |
| 39 <li> | |
| 40 {{request.delay}} second delay, Status: {{request.statusText}} | |
| 41 </li> | |
| 42 </template> | |
| 43 </ul> | |
| 44 </section> | |
| 45 </template> | |
| 46 <script> | |
| 47 Polymer({ | |
| 48 delay: 1, | |
| 49 response: null, | |
| 50 testResult: 'pending...', | |
| 51 passed: false, | |
| 52 requests: [], | |
| 53 observe: { | |
| 54 '$.ajax.activeRequest': 'requestChanged' | |
| 55 }, | |
| 56 domReady: function() { | |
| 57 setTimeout(function() { | |
| 58 if (this.response != null) { | |
| 59 console.error('HTTP request returned too quick!') | |
| 60 chai.assert.fail( | |
| 61 '', '', 'Indeterminate, initial request returned too quick'); | |
| 62 this.testResult = 'indeterminate'; | |
| 63 return; | |
| 64 } | |
| 65 this.delay = 2; | |
| 66 }.bind(this), 100); | |
| 67 // This will fail the test if it neither passes nor fails in time. | |
| 68 this.finalTimeout = setTimeout(function() { | |
| 69 chai.assert.fail('', '', 'Test timed out.'); | |
| 70 }, 7000); | |
| 71 }, | |
| 72 responseChanged: function() { | |
| 73 if (this.response.url != this.$.ajax.url) { | |
| 74 this.testResult = 'FAIL'; | |
| 75 chai.assert.fail(this.$.ajax.url, this.response.url, | |
| 76 'Race condition in response attribute'); | |
| 77 return; | |
| 78 } | |
| 79 this.passed = true; | |
| 80 }, | |
| 81 passedChanged: function() { | |
| 82 if (this.passed && this.testResult == 'pending...') { | |
| 83 this.testResult = 'PASS'; | |
| 84 clearTimeout(this.finalTimeout); | |
| 85 done(); | |
| 86 } | |
| 87 }, | |
| 88 requestChanged: function(o, n) { | |
| 89 this.requests.push({ | |
| 90 'statusText': 'pending', | |
| 91 xhr: n, | |
| 92 delay: this.delay | |
| 93 }); | |
| 94 }, | |
| 95 handleResponse: function(resp) { | |
| 96 var xhr = resp.detail.xhr; | |
| 97 for (var i = 0; i < this.requests.length; i++) { | |
| 98 if (this.requests[i].xhr === xhr) { | |
| 99 this.requests[i].statusText = xhr.statusText; | |
| 100 } | |
| 101 } | |
| 102 }, | |
| 103 }); | |
| 104 </script> | |
| 105 </polymer-element> | |
| 106 | |
| 107 <race-condition></race-condition> | |
| 108 </body> | |
| 109 </html> | |
| OLD | NEW |