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.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="../iron-request.html"> |
| 19 </head> |
| 20 <body> |
| 21 <test-fixture id="TrivialRequest"> |
| 22 <template> |
| 23 <iron-request></iron-request> |
| 24 </template> |
| 25 </test-fixture> |
| 26 <script> |
| 27 suite('<iron-request>', function () { |
| 28 var jsonResponseHeaders; |
| 29 var successfulRequestOptions; |
| 30 var request; |
| 31 var server; |
| 32 |
| 33 setup(function () { |
| 34 jsonResponseHeaders = { |
| 35 'Content-Type': 'application/json' |
| 36 }; |
| 37 server = sinon.fakeServer.create(); |
| 38 server.respondWith('GET', '/responds_to_get_with_json', [ |
| 39 200, |
| 40 jsonResponseHeaders, |
| 41 '{"success":true}' |
| 42 ]); |
| 43 |
| 44 server.respondWith('GET', '/responds_to_get_with_prefixed_json', [ |
| 45 200, |
| 46 jsonResponseHeaders, |
| 47 '])}while(1);</x>{"success":true}' |
| 48 ]); |
| 49 |
| 50 server.respondWith('GET', '/responds_to_get_with_500', [ |
| 51 500, |
| 52 {}, |
| 53 '' |
| 54 ]); |
| 55 |
| 56 server.respondWith('GET', '/responds_to_get_with_100', [ |
| 57 100, |
| 58 {}, |
| 59 '' |
| 60 ]); |
| 61 |
| 62 server.respondWith('GET', '/responds_to_get_with_0', [ |
| 63 0, |
| 64 jsonResponseHeaders, |
| 65 '{"success":true}' |
| 66 ]); |
| 67 |
| 68 |
| 69 request = fixture('TrivialRequest'); |
| 70 successfulRequestOptions = { |
| 71 url: '/responds_to_get_with_json' |
| 72 }; |
| 73 }); |
| 74 |
| 75 teardown(function () { |
| 76 server.restore(); |
| 77 }); |
| 78 |
| 79 suite('basic usage', function () { |
| 80 test('creates network requests, requiring only `url`', function () { |
| 81 request.send(successfulRequestOptions); |
| 82 |
| 83 server.respond(); |
| 84 |
| 85 expect(request.response).to.be.ok; |
| 86 }); |
| 87 |
| 88 test('sets async to true by default', function () { |
| 89 request.send(successfulRequestOptions); |
| 90 expect(request.xhr.async).to.be.eql(true); |
| 91 }); |
| 92 |
| 93 test('can be aborted', function () { |
| 94 request.send(successfulRequestOptions); |
| 95 |
| 96 request.abort(); |
| 97 |
| 98 server.respond(); |
| 99 |
| 100 return request.completes.then(function () { |
| 101 throw new Error('Request did not abort appropriately!'); |
| 102 }).catch(function (e) { |
| 103 expect(request.response).to.not.be.ok; |
| 104 }); |
| 105 }); |
| 106 |
| 107 test('default responseType is text', function () { |
| 108 request.send(successfulRequestOptions); |
| 109 server.respond(); |
| 110 |
| 111 return request.completes.then(function() { |
| 112 expect(request.response).to.be.an('string') |
| 113 }); |
| 114 }); |
| 115 |
| 116 test('default responseType of text is not applied, when async is false',
function () { |
| 117 var options = Object.create(successfulRequestOptions); |
| 118 options.async = false; |
| 119 |
| 120 request.send(options); |
| 121 server.respond(); |
| 122 |
| 123 return request.completes.then(function() { |
| 124 expect(request.xhr.responseType).to.be.empty; |
| 125 }); |
| 126 }); |
| 127 |
| 128 test('responseType can be configured via handleAs option', function () { |
| 129 var options = Object.create(successfulRequestOptions); |
| 130 options.handleAs = 'json'; |
| 131 |
| 132 request.send(options); |
| 133 expect(server.requests.length).to.be.equal(1); |
| 134 expect(server.requests[0].requestHeaders['accept']).to.be.equal( |
| 135 'application/json'); |
| 136 server.respond(); |
| 137 |
| 138 return request.completes.then(function() { |
| 139 expect(request.response).to.be.an('object'); |
| 140 }); |
| 141 }); |
| 142 |
| 143 test('setting jsonPrefix correctly strips it from the response', functio
n () { |
| 144 var options = { |
| 145 url: '/responds_to_get_with_prefixed_json', |
| 146 handleAs: 'json', |
| 147 jsonPrefix: '])}while(1);</x>' |
| 148 }; |
| 149 |
| 150 request.send(options); |
| 151 expect(server.requests.length).to.be.equal(1); |
| 152 expect(server.requests[0].requestHeaders['accept']).to.be.equal( |
| 153 'application/json'); |
| 154 server.respond(); |
| 155 |
| 156 return request.completes.then(function() { |
| 157 expect(request.response).to.deep.eq({success: true}); |
| 158 }); |
| 159 }); |
| 160 |
| 161 test('responseType cannot be configured via handleAs option, when async
is false', function () { |
| 162 var options = Object.create(successfulRequestOptions); |
| 163 options.handleAs = 'json'; |
| 164 options.async = false; |
| 165 |
| 166 request.send(options); |
| 167 expect(server.requests.length).to.be.equal(1); |
| 168 expect(server.requests[0].requestHeaders['accept']).to.be.equal( |
| 169 'application/json'); |
| 170 server.respond(); |
| 171 |
| 172 return request.completes.then(function() { |
| 173 expect(request.response).to.be.a('string'); |
| 174 }); |
| 175 }); |
| 176 |
| 177 test('headers are sent up', function() { |
| 178 var options = Object.create(successfulRequestOptions); |
| 179 options.headers = { |
| 180 'foo': 'bar', |
| 181 'accept': 'this should override the default' |
| 182 }; |
| 183 request.send(options); |
| 184 expect(server.requests.length).to.be.equal(1); |
| 185 var fakeXhr = server.requests[0] |
| 186 expect(fakeXhr.requestHeaders['foo']).to.be.equal( |
| 187 'bar'); |
| 188 expect(fakeXhr.requestHeaders['accept']).to.be.equal( |
| 189 'this should override the default'); |
| 190 }); |
| 191 |
| 192 test('headers are deduped by lowercasing', function() { |
| 193 var options = Object.create(successfulRequestOptions); |
| 194 options.headers = { |
| 195 'foo': 'bar', |
| 196 'Foo': 'bar', |
| 197 'fOo': 'bar', |
| 198 'Accept': 'this should also override the default' |
| 199 }; |
| 200 request.send(options); |
| 201 expect(server.requests.length).to.be.equal(1); |
| 202 var fakeXhr = server.requests[0] |
| 203 expect(Object.keys(fakeXhr.requestHeaders).length).to.be.equal(2); |
| 204 expect(fakeXhr.requestHeaders['foo']).to.be.equal( |
| 205 'bar'); |
| 206 expect(fakeXhr.requestHeaders['accept']).to.be.equal( |
| 207 'this should also override the default'); |
| 208 }); |
| 209 }); |
| 210 |
| 211 suite('special cases', function() { |
| 212 test('treats status code 0 as success, though the outcome is ambiguous',
function() { |
| 213 // Note: file:// status code will probably be 0 no matter what happene
d. |
| 214 request.send({ |
| 215 url: '/responds_to_get_with_0' |
| 216 }); |
| 217 |
| 218 server.respond(); |
| 219 |
| 220 expect(request.succeeded).to.be.equal(true); |
| 221 }); |
| 222 }); |
| 223 |
| 224 suite('errors', function() { |
| 225 test('treats status codes between 1 and 199 as errors', function() { |
| 226 request.send({ |
| 227 url: '/responds_to_get_with_100' |
| 228 }); |
| 229 |
| 230 server.respond(); |
| 231 |
| 232 expect(request.succeeded).to.be.equal(false); |
| 233 }); |
| 234 |
| 235 test('treats status codes between 300 and ∞ as errors', function() { |
| 236 request.send({ |
| 237 url: '/responds_to_get_with_500' |
| 238 }); |
| 239 |
| 240 server.respond(); |
| 241 |
| 242 expect(request.succeeded).to.be.equal(false); |
| 243 }); |
| 244 }); |
| 245 |
| 246 suite('status codes', function() { |
| 247 test('status and statusText is set after a ambiguous request', function(
) { |
| 248 request.send({ |
| 249 url: '/responds_to_get_with_0' |
| 250 }); |
| 251 |
| 252 server.respond(); |
| 253 |
| 254 expect(request.status).to.be.equal(0); |
| 255 expect(request.statusText).to.be.equal(''); |
| 256 }); |
| 257 |
| 258 test('status and statusText is set after a request that succeeded', func
tion() { |
| 259 request.send({ |
| 260 url: '/responds_to_get_with_json' |
| 261 }); |
| 262 |
| 263 server.respond(); |
| 264 |
| 265 expect(request.status).to.be.equal(200); |
| 266 expect(request.statusText).to.be.equal('OK'); |
| 267 }); |
| 268 |
| 269 test('status and statusText is set after a request that failed', functio
n() { |
| 270 request.send({ |
| 271 url: '/responds_to_get_with_500' |
| 272 }); |
| 273 |
| 274 server.respond(); |
| 275 |
| 276 expect(request.status).to.be.equal(500); |
| 277 expect(request.statusText).to.be.equal('Internal Server Error'); |
| 278 }); |
| 279 }); |
| 280 }); |
| 281 </script> |
| 282 |
| 283 </body> |
| 284 </html> |
OLD | NEW |