Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 (function() { | 9 (function() { |
| 10 | 10 |
| 11 'use strict'; | 11 'use strict'; |
| 12 | 12 |
| 13 /** @type {sinon.FakeXhrCtrl} */ | |
| 14 var fakeXhrCtrl; | |
| 15 | |
| 13 /** @type {sinon.FakeXhr} */ | 16 /** @type {sinon.FakeXhr} */ |
| 14 var fakeXhr; | 17 var fakeXhr; |
| 15 | 18 |
| 16 QUnit.module('xhr', { | 19 QUnit.module('xhr', { |
| 17 beforeEach: function() { | 20 beforeEach: function() { |
| 18 fakeXhr = null; | 21 fakeXhr = null; |
| 19 sinon.useFakeXMLHttpRequest().onCreate = | 22 fakeXhrCtrl = sinon.useFakeXMLHttpRequest(); |
| 23 fakeXhrCtrl.onCreate = | |
| 20 function(/** sinon.FakeXhr */ xhr) { | 24 function(/** sinon.FakeXhr */ xhr) { |
| 21 fakeXhr = xhr; | 25 fakeXhr = xhr; |
| 22 }; | 26 }; |
| 27 remoting.identity = new remoting.Identity(); | |
| 28 sinon.stub(remoting.identity, 'getToken'). | |
| 29 returns(Promise.resolve('my_token')); | |
|
Jamie
2015/03/23 23:11:34
You could alternatively mock chrome.identity (this
John Williams
2015/03/24 00:52:12
This way seems a lot easier, since it doesn't invo
John Williams
2015/03/24 17:27:36
After taking a second look, I see I was misunderst
Jamie
2015/03/24 19:44:11
The problem is that this only mocks one method of
John Williams
2015/03/24 23:34:40
Done.
| |
| 30 }, | |
| 31 afterEach: function() { | |
| 32 remoting.identity = null; | |
| 23 } | 33 } |
| 24 }); | 34 }); |
| 25 | 35 |
| 26 QUnit.test('urlencodeParamHash', function(assert) { | 36 QUnit.test('urlencodeParamHash', function(assert) { |
| 27 assert.equal( | 37 assert.equal( |
| 28 remoting.Xhr.urlencodeParamHash({}), | 38 remoting.Xhr.urlencodeParamHash({}), |
| 29 ''); | 39 ''); |
| 30 assert.equal( | 40 assert.equal( |
| 31 remoting.Xhr.urlencodeParamHash({'key': 'value'}), | 41 remoting.Xhr.urlencodeParamHash({'key': 'value'}), |
| 32 'key=value'); | 42 'key=value'); |
| 33 assert.equal( | 43 assert.equal( |
| 34 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), | 44 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), |
| 35 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); | 45 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); |
| 36 assert.equal( | 46 assert.equal( |
| 37 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), | 47 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), |
| 38 'k1=v1&k2=v2'); | 48 'k1=v1&k2=v2'); |
| 39 }); | 49 }); |
| 40 | 50 |
| 41 QUnit.test('basic GET', function(assert) { | 51 // |
| 52 // Test for what happens when the parameters are specified | |
| 53 // incorrectly. | |
| 54 // | |
| 55 | |
| 56 QUnit.test('invalid *content parameters', function(assert) { | |
| 57 assert.throws(function() { | |
| 58 new remoting.Xhr({ | |
| 59 method: 'POST', | |
| 60 url: 'http://foo.com', | |
| 61 jsonContent: {}, | |
| 62 formContent: {} | |
| 63 }); | |
| 64 }); | |
| 65 | |
| 66 assert.throws(function() { | |
| 67 new remoting.Xhr({ | |
| 68 method: 'POST', | |
| 69 url: 'http://foo.com', | |
| 70 textContent: '', | |
| 71 formContent: {} | |
| 72 }); | |
| 73 }); | |
| 74 | |
| 75 assert.throws(function() { | |
| 76 new remoting.Xhr({ | |
| 77 method: 'POST', | |
| 78 url: 'http://foo.com', | |
| 79 textContent: '', | |
| 80 jsonContent: {} | |
| 81 }); | |
| 82 }); | |
| 83 }); | |
| 84 | |
| 85 | |
| 86 QUnit.test('invalid URL parameters', function(assert) { | |
| 87 assert.throws(function() { | |
| 88 new remoting.Xhr({ | |
| 89 method: 'POST', | |
| 90 url: 'http://foo.com?', | |
| 91 urlParams: {} | |
| 92 }); | |
| 93 }); | |
| 94 | |
| 95 assert.throws(function() { | |
| 96 new remoting.Xhr({ | |
| 97 method: 'POST', | |
| 98 url: 'http://foo.com#', | |
| 99 urlParams: {} | |
| 100 }); | |
| 101 }); | |
| 102 }); | |
| 103 | |
| 104 QUnit.test('invalid auth parameters', function(assert) { | |
| 105 assert.throws(function() { | |
| 106 new remoting.Xhr({ | |
| 107 method: 'POST', | |
| 108 url: 'http://foo.com', | |
| 109 useIdentity: false, | |
| 110 oauthToken: '', | |
| 111 headers: { | |
| 112 'Authorization': '' | |
| 113 } | |
| 114 }); | |
| 115 }); | |
| 116 | |
| 117 assert.throws(function() { | |
| 118 new remoting.Xhr({ | |
| 119 method: 'POST', | |
| 120 url: 'http://foo.com', | |
| 121 useIdentity: true, | |
| 122 headers: { | |
| 123 'Authorization': '' | |
| 124 } | |
| 125 }); | |
| 126 }); | |
| 127 | |
| 128 assert.throws(function() { | |
| 129 new remoting.Xhr({ | |
| 130 method: 'POST', | |
| 131 url: 'http://foo.com', | |
| 132 useIdentity: true, | |
| 133 oauthToken: '', | |
| 134 headers: {} | |
| 135 }); | |
| 136 }); | |
| 137 }); | |
| 138 | |
| 139 QUnit.test('invalid auth parameters', function(assert) { | |
| 140 assert.throws(function() { | |
| 141 new remoting.Xhr({ | |
| 142 method: 'POST', | |
| 143 url: 'http://foo.com', | |
| 144 useIdentity: false, | |
| 145 oauthToken: '', | |
| 146 headers: { | |
| 147 'Authorization': '' | |
| 148 } | |
| 149 }); | |
| 150 }); | |
| 151 | |
| 152 assert.throws(function() { | |
| 153 new remoting.Xhr({ | |
| 154 method: 'POST', | |
| 155 url: 'http://foo.com', | |
| 156 useIdentity: true, | |
| 157 headers: { | |
| 158 'Authorization': '' | |
| 159 } | |
| 160 }); | |
| 161 }); | |
| 162 | |
| 163 assert.throws(function() { | |
| 164 new remoting.Xhr({ | |
| 165 method: 'POST', | |
| 166 url: 'http://foo.com', | |
| 167 useIdentity: true, | |
| 168 oauthToken: '', | |
| 169 headers: {} | |
| 170 }); | |
| 171 }); | |
| 172 }); | |
| 173 | |
| 174 // | |
| 175 // The typical case. | |
| 176 // | |
| 177 | |
| 178 QUnit.test('successful GET', function(assert) { | |
| 42 var promise = new remoting.Xhr({ | 179 var promise = new remoting.Xhr({ |
| 43 method: 'GET', | 180 method: 'GET', |
| 44 url: 'http://foo.com', | 181 url: 'http://foo.com' |
| 45 responseType: remoting.Xhr.ResponseType.TEXT | |
| 46 }).start().then(function(response) { | 182 }).start().then(function(response) { |
| 47 assert.equal(response.status, 200); | 183 assert.equal(response.status, 200); |
| 48 assert.equal(response.getText(), 'body'); | 184 assert.equal(response.getText(), 'body'); |
| 49 }); | 185 }); |
| 50 assert.equal(fakeXhr.method, 'GET'); | 186 assert.equal(fakeXhr.method, 'GET'); |
| 51 assert.equal(fakeXhr.url, 'http://foo.com'); | 187 assert.equal(fakeXhr.url, 'http://foo.com'); |
| 52 assert.equal(fakeXhr.withCredentials, false); | 188 assert.equal(fakeXhr.withCredentials, false); |
| 53 assert.equal(fakeXhr.requestBody, null); | 189 assert.equal(fakeXhr.requestBody, null); |
| 54 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | 190 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
| 55 fakeXhr.respond(200, {}, 'body'); | 191 fakeXhr.respond(200, {}, 'body'); |
| 56 return promise; | 192 return promise; |
| 57 }); | 193 }); |
| 58 | 194 |
| 59 QUnit.test('GET with param string', function(assert) { | 195 // |
| 196 // Handling of errors with different values of ignoreErrors. | |
| 197 // | |
| 198 | |
| 199 QUnit.test('failed GET', function(assert) { | |
| 200 var promise = new remoting.Xhr({ | |
| 201 method: 'GET', | |
| 202 url: 'http://foo.com' | |
| 203 }).start().then(function(response) { | |
| 204 assert.equal(response.status, 500); | |
| 205 assert.equal(response.getText(), 'body'); | |
| 206 }); | |
| 207 fakeXhr.respond(500, {}, 'body'); | |
| 208 return promise; | |
| 209 }); | |
| 210 | |
| 211 QUnit.test('failed GET with non-ignored error', function(assert) { | |
| 212 var expectedError = remoting.Error.fromHttpStatus(500); | |
| 213 assert.ok(!expectedError.isNone()); | |
| 214 | |
| 60 var promise = new remoting.Xhr({ | 215 var promise = new remoting.Xhr({ |
| 61 method: 'GET', | 216 method: 'GET', |
| 62 url: 'http://foo.com', | 217 url: 'http://foo.com', |
| 63 responseType: remoting.Xhr.ResponseType.TEXT, | 218 ignoreErrors: [] |
| 64 urlParams: 'the_param_string' | 219 }).start().then(function() { |
| 65 }).start().then(function(response) { | 220 assert.ok(false); |
| 66 assert.equal(response.status, 200); | 221 }, function(/** remoting.Error */ error) { |
| 67 assert.equal(response.getText(), 'body'); | 222 assert.equal(error.getTag(), expectedError.getTag()); |
| 68 }); | 223 }); |
| 69 assert.equal(fakeXhr.method, 'GET'); | 224 fakeXhr.respond(500, {}, 'body'); |
| 70 assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); | |
| 71 assert.equal(fakeXhr.withCredentials, false); | |
| 72 assert.equal(fakeXhr.requestBody, null); | |
| 73 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
| 74 fakeXhr.respond(200, {}, 'body'); | |
| 75 return promise; | 225 return promise; |
| 76 }); | 226 }); |
| 77 | 227 |
| 78 QUnit.test('GET with param object', function(assert) { | 228 QUnit.test('failed GET with ignored error', function(assert) { |
| 229 var expectedError = remoting.Error.fromHttpStatus(500); | |
| 230 assert.ok(!expectedError.isNone()); | |
| 231 | |
| 79 var promise = new remoting.Xhr({ | 232 var promise = new remoting.Xhr({ |
| 80 method: 'GET', | 233 method: 'GET', |
| 81 url: 'http://foo.com', | 234 url: 'http://foo.com', |
| 82 responseType: remoting.Xhr.ResponseType.TEXT, | 235 ignoreErrors: [expectedError.getTag()] |
| 83 urlParams: {'a': 'b', 'c': 'd'} | 236 }).start(); |
| 84 }).start().then(function(response) { | 237 fakeXhr.respond(500, {}, 'body'); |
| 85 assert.equal(response.status, 200); | |
| 86 assert.equal(response.getText(), 'body'); | |
| 87 }); | |
| 88 assert.equal(fakeXhr.method, 'GET'); | |
| 89 assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); | |
| 90 assert.equal(fakeXhr.withCredentials, false); | |
| 91 assert.equal(fakeXhr.requestBody, null); | |
| 92 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
| 93 fakeXhr.respond(200, {}, 'body'); | |
| 94 return promise; | 238 return promise; |
| 95 }); | 239 }); |
| 96 | 240 |
| 97 QUnit.test('GET with headers', function(assert) { | 241 QUnit.test('204 is not a failure', function(assert) { |
| 242 assert.expect(0); | |
| 243 var promise = new remoting.Xhr({ | |
| 244 method: 'DELETE', | |
| 245 url: 'http://foo.com', | |
| 246 ignoreErrors: [] | |
| 247 }).start(); | |
| 248 fakeXhr.respond(204, {}, 'body'); | |
| 249 return promise; | |
| 250 }); | |
| 251 | |
| 252 // | |
| 253 // Tests for the effect of acceptJson. | |
| 254 // | |
| 255 | |
| 256 QUnit.test('acceptJson required', function(assert) { | |
| 257 var promise = new remoting.Xhr({ | |
| 258 method: 'GET', | |
| 259 url: 'http://foo.com' | |
| 260 }).start().then(function(response) { | |
| 261 assert.throws(response.getJson); | |
| 262 }); | |
| 263 fakeXhr.respond(200, {}, '{}'); | |
| 264 return promise; | |
| 265 }); | |
| 266 | |
| 267 QUnit.test('JSON response', function(assert) { | |
| 268 var responseJson = { | |
| 269 'myJsonData': [true] | |
| 270 }; | |
| 271 var responseText = JSON.stringify(responseJson); | |
| 98 var promise = new remoting.Xhr({ | 272 var promise = new remoting.Xhr({ |
| 99 method: 'GET', | 273 method: 'GET', |
| 100 url: 'http://foo.com', | 274 url: 'http://foo.com', |
| 101 responseType: remoting.Xhr.ResponseType.TEXT, | 275 acceptJson: true |
| 276 }).start().then(function(response) { | |
| 277 // Calling getText is still OK even when a JSON response is | |
| 278 // requested. | |
| 279 assert.equal( | |
| 280 response.getText(), | |
| 281 responseText); | |
| 282 // Check that getJson works as advertized. | |
| 283 assert.deepEqual( | |
| 284 response.getJson(), | |
| 285 responseJson); | |
| 286 // Calling getJson multiple times doesn't re-parse the response. | |
| 287 assert.strictEqual( | |
| 288 response.getJson(), | |
| 289 response.getJson()); | |
| 290 }); | |
| 291 fakeXhr.respond(200, {}, responseText); | |
| 292 return promise; | |
| 293 }); | |
| 294 | |
| 295 // | |
| 296 // Tests for various parameters that modify the HTTP request. | |
| 297 // | |
| 298 | |
| 299 QUnit.test('GET with param string', function(assert) { | |
| 300 new remoting.Xhr({ | |
| 301 method: 'GET', | |
| 302 url: 'http://foo.com', | |
| 303 urlParams: 'the_param_string' | |
| 304 }).start(); | |
| 305 assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); | |
| 306 }); | |
| 307 | |
| 308 QUnit.test('GET with param object', function(assert) { | |
| 309 new remoting.Xhr({ | |
| 310 method: 'GET', | |
| 311 url: 'http://foo.com', | |
| 312 urlParams: {'a': 'b', 'c': 'd'} | |
| 313 }).start(); | |
| 314 assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); | |
| 315 }); | |
| 316 | |
| 317 QUnit.test('GET with headers', function(assert) { | |
| 318 new remoting.Xhr({ | |
| 319 method: 'GET', | |
| 320 url: 'http://foo.com', | |
| 102 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} | 321 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} |
| 103 }).start().then(function(response) { | 322 }).start(); |
| 104 assert.equal(response.status, 200); | |
| 105 assert.equal(response.getText(), 'body'); | |
| 106 }); | |
| 107 assert.equal(fakeXhr.method, 'GET'); | |
| 108 assert.equal(fakeXhr.url, 'http://foo.com'); | |
| 109 assert.equal(fakeXhr.withCredentials, false); | |
| 110 assert.equal(fakeXhr.requestBody, null); | |
| 111 assert.equal( | 323 assert.equal( |
| 112 fakeXhr.requestHeaders['Header1'], | 324 fakeXhr.requestHeaders['Header1'], |
| 113 'headerValue1'); | 325 'headerValue1'); |
| 114 assert.equal( | 326 assert.equal( |
| 115 fakeXhr.requestHeaders['Header2'], | 327 fakeXhr.requestHeaders['Header2'], |
| 116 'headerValue2'); | 328 'headerValue2'); |
| 117 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
| 118 fakeXhr.respond(200, {}, 'body'); | |
| 119 return promise; | |
| 120 }); | 329 }); |
| 121 | 330 |
| 122 | 331 |
| 123 QUnit.test('GET with credentials', function(assert) { | 332 QUnit.test('GET with credentials', function(assert) { |
| 124 var promise = new remoting.Xhr({ | 333 new remoting.Xhr({ |
| 125 method: 'GET', | 334 method: 'GET', |
| 126 url: 'http://foo.com', | 335 url: 'http://foo.com', |
| 127 responseType: remoting.Xhr.ResponseType.TEXT, | |
| 128 withCredentials: true | 336 withCredentials: true |
| 129 }).start().then(function(response) { | 337 }).start(); |
| 130 assert.equal(response.status, 200); | |
| 131 assert.equal(response.getText(), 'body'); | |
| 132 }); | |
| 133 assert.equal(fakeXhr.method, 'GET'); | |
| 134 assert.equal(fakeXhr.url, 'http://foo.com'); | |
| 135 assert.equal(fakeXhr.withCredentials, true); | 338 assert.equal(fakeXhr.withCredentials, true); |
| 136 assert.equal(fakeXhr.requestBody, null); | |
| 137 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
| 138 fakeXhr.respond(200, {}, 'body'); | |
| 139 return promise; | |
| 140 }); | 339 }); |
| 141 | 340 |
| 341 // | |
| 342 // Checking that typical POST requests work. | |
| 343 // | |
| 344 | |
| 142 QUnit.test('POST with text content', function(assert) { | 345 QUnit.test('POST with text content', function(assert) { |
| 143 var done = assert.async(); | 346 var done = assert.async(); |
| 144 | 347 |
| 145 var promise = new remoting.Xhr({ | 348 var promise = new remoting.Xhr({ |
| 146 method: 'POST', | 349 method: 'POST', |
| 147 url: 'http://foo.com', | 350 url: 'http://foo.com', |
| 148 responseType: remoting.Xhr.ResponseType.TEXT, | |
| 149 textContent: 'the_content_string' | 351 textContent: 'the_content_string' |
| 150 }).start().then(function(response) { | 352 }).start().then(function(response) { |
| 151 assert.equal(response.status, 200); | 353 assert.equal(response.status, 200); |
| 152 assert.equal(response.getText(), 'body'); | 354 assert.equal(response.getText(), 'body'); |
| 153 done(); | 355 done(); |
| 154 }); | 356 }); |
| 155 assert.equal(fakeXhr.method, 'POST'); | 357 assert.equal(fakeXhr.method, 'POST'); |
| 156 assert.equal(fakeXhr.url, 'http://foo.com'); | 358 assert.equal(fakeXhr.url, 'http://foo.com'); |
| 157 assert.equal(fakeXhr.withCredentials, false); | 359 assert.equal(fakeXhr.withCredentials, false); |
| 158 assert.equal(fakeXhr.requestBody, 'the_content_string'); | 360 assert.equal(fakeXhr.requestBody, 'the_content_string'); |
| 159 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | 361 assert.equal(fakeXhr.requestHeaders['Content-type'], |
| 362 'text/plain; charset=UTF-8'); | |
| 160 fakeXhr.respond(200, {}, 'body'); | 363 fakeXhr.respond(200, {}, 'body'); |
| 161 return promise; | 364 return promise; |
| 162 }); | 365 }); |
| 163 | 366 |
| 164 QUnit.test('POST with form content', function(assert) { | 367 QUnit.test('POST with form content', function(assert) { |
| 165 var promise = new remoting.Xhr({ | 368 new remoting.Xhr({ |
| 166 method: 'POST', | 369 method: 'POST', |
| 167 url: 'http://foo.com', | 370 url: 'http://foo.com', |
| 168 responseType: remoting.Xhr.ResponseType.TEXT, | |
| 169 formContent: {'a': 'b', 'c': 'd'} | 371 formContent: {'a': 'b', 'c': 'd'} |
| 170 }).start().then(function(response) { | 372 }).start(); |
| 171 assert.equal(response.status, 200); | |
| 172 assert.equal(response.getText(), 'body'); | |
| 173 }); | |
| 174 assert.equal(fakeXhr.method, 'POST'); | |
| 175 assert.equal(fakeXhr.url, 'http://foo.com'); | |
| 176 assert.equal(fakeXhr.withCredentials, false); | |
| 177 assert.equal(fakeXhr.requestBody, 'a=b&c=d'); | 373 assert.equal(fakeXhr.requestBody, 'a=b&c=d'); |
| 178 assert.equal( | 374 assert.equal( |
| 179 fakeXhr.requestHeaders['Content-type'], | 375 fakeXhr.requestHeaders['Content-type'], |
| 180 'application/x-www-form-urlencoded'); | 376 'application/x-www-form-urlencoded; charset=UTF-8'); |
| 181 fakeXhr.respond(200, {}, 'body'); | 377 }); |
| 378 | |
| 379 // | |
| 380 // Tests for authentication-related options. | |
| 381 // | |
| 382 | |
| 383 QUnit.test('GET with auth token', function(assert) { | |
| 384 new remoting.Xhr({ | |
| 385 method: 'GET', | |
| 386 url: 'http://foo.com', | |
| 387 oauthToken: 'my_token' | |
| 388 }).start(); | |
| 389 assert.equal(fakeXhr.requestHeaders['Authorization'], | |
| 390 'Bearer my_token'); | |
| 391 }); | |
| 392 | |
| 393 QUnit.test('GET with useIdentity', function(assert) { | |
| 394 var xhr = new remoting.Xhr({ | |
| 395 method: 'GET', | |
| 396 url: 'http://foo.com', | |
| 397 useIdentity: true | |
| 398 }); | |
| 399 | |
| 400 xhr.start(); | |
| 401 | |
| 402 var done = assert.async(); | |
| 403 fakeXhr.addEventListener('loadstart', function() { | |
| 404 assert.equal(fakeXhr.requestHeaders['Authorization'], | |
| 405 'Bearer my_token'); | |
| 406 done(); | |
| 407 }); | |
| 408 }); | |
| 409 | |
| 410 | |
| 411 // | |
| 412 // Tests for the abort method. | |
| 413 // | |
| 414 | |
| 415 QUnit.test('abort before start', function(assert) { | |
| 416 assert.expect(0); | |
| 417 var xhr = new remoting.Xhr({ | |
| 418 method: 'GET', | |
| 419 url: 'http://foo.com' | |
| 420 }); | |
| 421 fakeXhr.addEventListener('abort', assert.async()); | |
| 422 xhr.abort(); | |
| 423 xhr.start().then(function() { | |
| 424 assert.ok(false, 'promise resolved after abort'); | |
| 425 }, function() { | |
| 426 assert.ok(false, 'promise rejected after abort'); | |
| 427 }); | |
| 428 }); | |
| 429 | |
| 430 QUnit.test('abort before send', function(assert) { | |
| 431 assert.expect(0); | |
| 432 var xhr = new remoting.Xhr({ | |
| 433 method: 'GET', | |
| 434 url: 'http://foo.com', | |
| 435 useIdentity: true | |
| 436 }); | |
| 437 xhr.start().then(function() { | |
| 438 assert.ok(false, 'promise resolved after abort'); | |
| 439 }, function() { | |
| 440 assert.ok(false, 'promise rejected after abort'); | |
| 441 }); | |
| 442 fakeXhr.addEventListener('abort', assert.async()); | |
| 443 xhr.abort(); | |
| 444 }); | |
| 445 | |
| 446 QUnit.test('abort after resolved', function(assert) { | |
| 447 var xhr = new remoting.Xhr({ | |
| 448 method: 'GET', | |
| 449 url: 'http://foo.com', | |
| 450 useIdentity: true | |
| 451 }); | |
| 452 var promise = xhr.start().then(function() { | |
| 453 assert.ok(true); | |
| 454 fakeXhr.addEventListener('abort', assert.async()); | |
| 455 xhr.abort(); | |
| 456 }); | |
| 457 fakeXhr.addEventListener('loadstart', function() { | |
| 458 fakeXhr.respond(200, {}, 'body'); | |
| 459 }); | |
| 182 return promise; | 460 return promise; |
| 183 }); | 461 }); |
| 184 | 462 |
| 185 QUnit.test('defaultResponse 200', function(assert) { | |
| 186 var done = assert.async(); | |
| 187 | |
| 188 var onDone = function() { | |
| 189 assert.ok(true); | |
| 190 done(); | |
| 191 }; | |
| 192 | |
| 193 var onError = function(error) { | |
| 194 assert.ok(false); | |
| 195 done(); | |
| 196 }; | |
| 197 | |
| 198 new remoting.Xhr({ | |
| 199 method: 'POST', | |
| 200 url: 'http://foo.com' | |
| 201 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); | |
| 202 fakeXhr.respond(200, {}, ''); | |
| 203 }); | |
| 204 | |
| 205 | |
| 206 QUnit.test('defaultResponse 404', function(assert) { | |
| 207 var done = assert.async(); | |
| 208 | |
| 209 var onDone = function() { | |
| 210 assert.ok(false); | |
| 211 done(); | |
| 212 }; | |
| 213 | |
| 214 var onError = function(error) { | |
| 215 assert.ok(true); | |
| 216 done(); | |
| 217 }; | |
| 218 | |
| 219 new remoting.Xhr({ | |
| 220 method: 'POST', | |
| 221 url: 'http://foo.com' | |
| 222 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); | |
| 223 fakeXhr.respond(404, {}, ''); | |
| 224 }); | |
| 225 | |
| 226 })(); | 463 })(); |
| OLD | NEW |