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