| 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-request</title> |
| 13 |
| 14 <script src="../../webcomponentsjs/webcomponents-lite.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-request.html"> |
| 21 </head> |
| 22 <body> |
| 23 <test-fixture id="TrivialRequest"> |
| 24 <template> |
| 25 <iron-request></iron-request> |
| 26 </template> |
| 27 </test-fixture> |
| 28 <script> |
| 29 suite('<iron-request>', function () { |
| 30 var jsonResponseHeaders; |
| 31 var successfulRequestOptions; |
| 32 var request; |
| 33 var server; |
| 34 |
| 35 setup(function () { |
| 36 jsonResponseHeaders = { |
| 37 'Content-Type': 'application/json' |
| 38 }; |
| 39 server = sinon.fakeServer.create(); |
| 40 server.respondWith('GET', '/responds_to_get_with_json', [ |
| 41 200, |
| 42 jsonResponseHeaders, |
| 43 '{"success":true}' |
| 44 ]); |
| 45 |
| 46 server.respondWith('GET', '/responds_to_get_with_500', [ |
| 47 500, |
| 48 {}, |
| 49 '' |
| 50 ]); |
| 51 |
| 52 server.respondWith('GET', '/responds_to_get_with_100', [ |
| 53 100, |
| 54 {}, |
| 55 '' |
| 56 ]); |
| 57 |
| 58 server.respondWith('GET', '/responds_to_get_with_0', [ |
| 59 0, |
| 60 jsonResponseHeaders, |
| 61 '{"success":true}' |
| 62 ]); |
| 63 |
| 64 |
| 65 request = fixture('TrivialRequest'); |
| 66 successfulRequestOptions = { |
| 67 url: '/responds_to_get_with_json' |
| 68 }; |
| 69 }); |
| 70 |
| 71 teardown(function () { |
| 72 server.restore(); |
| 73 }); |
| 74 |
| 75 suite('basic usage', function () { |
| 76 test('creates network requests, requiring only `url`', function () { |
| 77 request.send(successfulRequestOptions); |
| 78 |
| 79 server.respond(); |
| 80 |
| 81 expect(request.response).to.be.ok; |
| 82 }); |
| 83 |
| 84 test('sets async to true by default', function () { |
| 85 request.send(successfulRequestOptions); |
| 86 expect(request.xhr.async).to.be.eql(true); |
| 87 }); |
| 88 |
| 89 test('can be aborted', function (done) { |
| 90 request.send(successfulRequestOptions); |
| 91 |
| 92 request.abort(); |
| 93 |
| 94 server.respond(); |
| 95 |
| 96 request.completes.then(function () { |
| 97 done(new Error('Request did not abort appropriately!')); |
| 98 }).catch(function (e) { |
| 99 expect(request.response).to.not.be.ok; |
| 100 done(); |
| 101 }); |
| 102 }); |
| 103 |
| 104 test('default responseType is text', function (done) { |
| 105 |
| 106 request.send(successfulRequestOptions); |
| 107 server.respond(); |
| 108 |
| 109 request.completes.then(function() { |
| 110 expect(request.response).to.be.an('string') |
| 111 done(); |
| 112 }).catch(function(e) { |
| 113 done(new Error('Response was not a Object')); |
| 114 }); |
| 115 |
| 116 }); |
| 117 |
| 118 test('responseType can be configured via handleAs option', function (don
e) { |
| 119 |
| 120 var options = Object.create(successfulRequestOptions); |
| 121 options.handleAs = 'json'; |
| 122 |
| 123 request.send(options); |
| 124 server.respond(); |
| 125 |
| 126 request.completes.then(function() { |
| 127 expect(request.response).to.be.an('object'); |
| 128 done(); |
| 129 }).catch(function(e) { |
| 130 done(new Error('Response was not type Object')); |
| 131 }); |
| 132 |
| 133 }); |
| 134 |
| 135 }); |
| 136 |
| 137 suite('special cases', function() { |
| 138 test('treats status code 0 as success, though the outcome is ambiguous',
function() { |
| 139 // Note: file:// status code will probably be 0 no matter what happene
d. |
| 140 request.send({ |
| 141 url: '/responds_to_get_with_0' |
| 142 }); |
| 143 |
| 144 server.respond(); |
| 145 |
| 146 expect(request.succeeded).to.be.equal(true); |
| 147 }); |
| 148 }); |
| 149 |
| 150 suite('errors', function() { |
| 151 test('treats status codes between 1 and 199 as errors', function() { |
| 152 request.send({ |
| 153 url: '/responds_to_get_with_100' |
| 154 }); |
| 155 |
| 156 server.respond(); |
| 157 |
| 158 expect(request.succeeded).to.be.equal(false); |
| 159 }); |
| 160 |
| 161 test('treats status codes between 300 and ∞ as errors', function() { |
| 162 request.send({ |
| 163 url: '/responds_to_get_with_500' |
| 164 }); |
| 165 |
| 166 server.respond(); |
| 167 |
| 168 expect(request.succeeded).to.be.equal(false); |
| 169 }); |
| 170 }); |
| 171 }); |
| 172 </script> |
| 173 |
| 174 </body> |
| 175 </html> |
| OLD | NEW |