| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 */ | |
| 8 | |
| 9 (function() { | |
| 10 | |
| 11 'use strict'; | |
| 12 | |
| 13 /** @type {sinon.FakeXhrCtrl} */ | |
| 14 var fakeXhrCtrl; | |
| 15 | |
| 16 /** @type {sinon.FakeXhr} */ | |
| 17 var fakeXhr; | |
| 18 | |
| 19 QUnit.module('xhr', { | |
| 20 beforeEach: function() { | |
| 21 fakeXhr = null; | |
| 22 fakeXhrCtrl = sinon.useFakeXMLHttpRequest(); | |
| 23 fakeXhrCtrl.onCreate = | |
| 24 function(/** sinon.FakeXhr */ xhr) { | |
| 25 fakeXhr = xhr; | |
| 26 }; | |
| 27 remoting.identity = new remoting.Identity(); | |
| 28 chromeMocks.activate(['identity']); | |
| 29 chromeMocks.identity.mock$setToken('my_token'); | |
| 30 }, | |
| 31 afterEach: function() { | |
| 32 chromeMocks.restore(); | |
| 33 remoting.identity = null; | |
| 34 } | |
| 35 }); | |
| 36 | |
| 37 QUnit.test('urlencodeParamHash', function(assert) { | |
| 38 assert.equal( | |
| 39 remoting.Xhr.urlencodeParamHash({}), | |
| 40 ''); | |
| 41 assert.equal( | |
| 42 remoting.Xhr.urlencodeParamHash({'key': 'value'}), | |
| 43 'key=value'); | |
| 44 assert.equal( | |
| 45 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), | |
| 46 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); | |
| 47 assert.equal( | |
| 48 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), | |
| 49 'k1=v1&k2=v2'); | |
| 50 }); | |
| 51 | |
| 52 // | |
| 53 // Test for what happens when the parameters are specified | |
| 54 // incorrectly. | |
| 55 // | |
| 56 | |
| 57 QUnit.test('invalid *content parameters', function(assert) { | |
| 58 assert.throws(function() { | |
| 59 new remoting.Xhr({ | |
| 60 method: 'POST', | |
| 61 url: 'http://foo.com', | |
| 62 jsonContent: {}, | |
| 63 formContent: {} | |
| 64 }); | |
| 65 }); | |
| 66 | |
| 67 assert.throws(function() { | |
| 68 new remoting.Xhr({ | |
| 69 method: 'POST', | |
| 70 url: 'http://foo.com', | |
| 71 textContent: '', | |
| 72 formContent: {} | |
| 73 }); | |
| 74 }); | |
| 75 | |
| 76 assert.throws(function() { | |
| 77 new remoting.Xhr({ | |
| 78 method: 'POST', | |
| 79 url: 'http://foo.com', | |
| 80 textContent: '', | |
| 81 jsonContent: {} | |
| 82 }); | |
| 83 }); | |
| 84 }); | |
| 85 | |
| 86 | |
| 87 QUnit.test('invalid URL parameters', function(assert) { | |
| 88 assert.throws(function() { | |
| 89 new remoting.Xhr({ | |
| 90 method: 'POST', | |
| 91 url: 'http://foo.com?', | |
| 92 urlParams: {} | |
| 93 }); | |
| 94 }); | |
| 95 | |
| 96 assert.throws(function() { | |
| 97 new remoting.Xhr({ | |
| 98 method: 'POST', | |
| 99 url: 'http://foo.com#', | |
| 100 urlParams: {} | |
| 101 }); | |
| 102 }); | |
| 103 }); | |
| 104 | |
| 105 QUnit.test('invalid auth parameters', function(assert) { | |
| 106 assert.throws(function() { | |
| 107 new remoting.Xhr({ | |
| 108 method: 'POST', | |
| 109 url: 'http://foo.com', | |
| 110 useIdentity: false, | |
| 111 oauthToken: '', | |
| 112 headers: { | |
| 113 'Authorization': '' | |
| 114 } | |
| 115 }); | |
| 116 }); | |
| 117 | |
| 118 assert.throws(function() { | |
| 119 new remoting.Xhr({ | |
| 120 method: 'POST', | |
| 121 url: 'http://foo.com', | |
| 122 useIdentity: true, | |
| 123 headers: { | |
| 124 'Authorization': '' | |
| 125 } | |
| 126 }); | |
| 127 }); | |
| 128 | |
| 129 assert.throws(function() { | |
| 130 new remoting.Xhr({ | |
| 131 method: 'POST', | |
| 132 url: 'http://foo.com', | |
| 133 useIdentity: true, | |
| 134 oauthToken: '', | |
| 135 headers: {} | |
| 136 }); | |
| 137 }); | |
| 138 }); | |
| 139 | |
| 140 QUnit.test('invalid auth parameters', function(assert) { | |
| 141 assert.throws(function() { | |
| 142 new remoting.Xhr({ | |
| 143 method: 'POST', | |
| 144 url: 'http://foo.com', | |
| 145 useIdentity: false, | |
| 146 oauthToken: '', | |
| 147 headers: { | |
| 148 'Authorization': '' | |
| 149 } | |
| 150 }); | |
| 151 }); | |
| 152 | |
| 153 assert.throws(function() { | |
| 154 new remoting.Xhr({ | |
| 155 method: 'POST', | |
| 156 url: 'http://foo.com', | |
| 157 useIdentity: true, | |
| 158 headers: { | |
| 159 'Authorization': '' | |
| 160 } | |
| 161 }); | |
| 162 }); | |
| 163 | |
| 164 assert.throws(function() { | |
| 165 new remoting.Xhr({ | |
| 166 method: 'POST', | |
| 167 url: 'http://foo.com', | |
| 168 useIdentity: true, | |
| 169 oauthToken: '', | |
| 170 headers: {} | |
| 171 }); | |
| 172 }); | |
| 173 }); | |
| 174 | |
| 175 | |
| 176 QUnit.test('unexpected parameters', function(assert) { | |
| 177 assert.throws(function() { | |
| 178 new remoting.Xhr({ | |
| 179 method: 'POST', | |
| 180 url: 'http://foo.com', | |
| 181 xyzzy: 'not a real parameter' | |
| 182 }); | |
| 183 }); | |
| 184 }); | |
| 185 | |
| 186 // | |
| 187 // The typical case. | |
| 188 // | |
| 189 | |
| 190 QUnit.test('successful GET', function(assert) { | |
| 191 var promise = new remoting.Xhr({ | |
| 192 method: 'GET', | |
| 193 url: 'http://foo.com' | |
| 194 }).start().then(function(response) { | |
| 195 assert.ok(!response.isError()); | |
| 196 assert.equal(response.status, 200); | |
| 197 assert.equal(response.getText(), 'body'); | |
| 198 }); | |
| 199 assert.equal(fakeXhr.method, 'GET'); | |
| 200 assert.equal(fakeXhr.url, 'http://foo.com'); | |
| 201 assert.equal(fakeXhr.withCredentials, false); | |
| 202 assert.equal(fakeXhr.requestBody, null); | |
| 203 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
| 204 fakeXhr.respond(200, {}, 'body'); | |
| 205 return promise; | |
| 206 }); | |
| 207 | |
| 208 // | |
| 209 // Tests for the effect of acceptJson. | |
| 210 // | |
| 211 | |
| 212 QUnit.test('acceptJson required', function(assert) { | |
| 213 var promise = new remoting.Xhr({ | |
| 214 method: 'GET', | |
| 215 url: 'http://foo.com' | |
| 216 }).start().then(function(response) { | |
| 217 assert.throws(response.getJson); | |
| 218 }); | |
| 219 fakeXhr.respond(200, {}, '{}'); | |
| 220 return promise; | |
| 221 }); | |
| 222 | |
| 223 QUnit.test('JSON response', function(assert) { | |
| 224 var responseJson = { | |
| 225 'myJsonData': [true] | |
| 226 }; | |
| 227 var responseText = JSON.stringify(responseJson); | |
| 228 var promise = new remoting.Xhr({ | |
| 229 method: 'GET', | |
| 230 url: 'http://foo.com', | |
| 231 acceptJson: true | |
| 232 }).start().then(function(response) { | |
| 233 // Calling getText is still OK even when a JSON response is | |
| 234 // requested. | |
| 235 assert.equal( | |
| 236 response.getText(), | |
| 237 responseText); | |
| 238 // Check that getJson works as advertised. | |
| 239 assert.deepEqual( | |
| 240 response.getJson(), | |
| 241 responseJson); | |
| 242 // Calling getJson multiple times doesn't re-parse the response. | |
| 243 assert.strictEqual( | |
| 244 response.getJson(), | |
| 245 response.getJson()); | |
| 246 }); | |
| 247 fakeXhr.respond(200, {}, responseText); | |
| 248 return promise; | |
| 249 }); | |
| 250 | |
| 251 // | |
| 252 // Tests for various parameters that modify the HTTP request. | |
| 253 // | |
| 254 | |
| 255 QUnit.test('GET with param string', function(assert) { | |
| 256 new remoting.Xhr({ | |
| 257 method: 'GET', | |
| 258 url: 'http://foo.com', | |
| 259 urlParams: 'the_param_string' | |
| 260 }).start(); | |
| 261 assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); | |
| 262 }); | |
| 263 | |
| 264 QUnit.test('GET with param object', function(assert) { | |
| 265 new remoting.Xhr({ | |
| 266 method: 'GET', | |
| 267 url: 'http://foo.com', | |
| 268 urlParams: {'a': 'b', 'c': 'd'} | |
| 269 }).start(); | |
| 270 assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); | |
| 271 }); | |
| 272 | |
| 273 QUnit.test('GET with headers', function(assert) { | |
| 274 new remoting.Xhr({ | |
| 275 method: 'GET', | |
| 276 url: 'http://foo.com', | |
| 277 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} | |
| 278 }).start(); | |
| 279 assert.equal( | |
| 280 fakeXhr.requestHeaders['Header1'], | |
| 281 'headerValue1'); | |
| 282 assert.equal( | |
| 283 fakeXhr.requestHeaders['Header2'], | |
| 284 'headerValue2'); | |
| 285 }); | |
| 286 | |
| 287 | |
| 288 QUnit.test('GET with credentials', function(assert) { | |
| 289 new remoting.Xhr({ | |
| 290 method: 'GET', | |
| 291 url: 'http://foo.com', | |
| 292 withCredentials: true | |
| 293 }).start(); | |
| 294 assert.equal(fakeXhr.withCredentials, true); | |
| 295 }); | |
| 296 | |
| 297 // | |
| 298 // Checking that typical POST requests work. | |
| 299 // | |
| 300 | |
| 301 QUnit.test('POST with text content', function(assert) { | |
| 302 var done = assert.async(); | |
| 303 | |
| 304 var promise = new remoting.Xhr({ | |
| 305 method: 'POST', | |
| 306 url: 'http://foo.com', | |
| 307 textContent: 'the_content_string' | |
| 308 }).start().then(function(response) { | |
| 309 assert.equal(response.status, 200); | |
| 310 assert.equal(response.getText(), 'body'); | |
| 311 done(); | |
| 312 }); | |
| 313 assert.equal(fakeXhr.method, 'POST'); | |
| 314 assert.equal(fakeXhr.url, 'http://foo.com'); | |
| 315 assert.equal(fakeXhr.withCredentials, false); | |
| 316 assert.equal(fakeXhr.requestBody, 'the_content_string'); | |
| 317 assert.equal(fakeXhr.requestHeaders['Content-type'], | |
| 318 'text/plain; charset=UTF-8'); | |
| 319 fakeXhr.respond(200, {}, 'body'); | |
| 320 return promise; | |
| 321 }); | |
| 322 | |
| 323 QUnit.test('POST with form content', function(assert) { | |
| 324 new remoting.Xhr({ | |
| 325 method: 'POST', | |
| 326 url: 'http://foo.com', | |
| 327 formContent: {'a': 'b', 'c': 'd'} | |
| 328 }).start(); | |
| 329 assert.equal(fakeXhr.requestBody, 'a=b&c=d'); | |
| 330 assert.equal( | |
| 331 fakeXhr.requestHeaders['Content-type'], | |
| 332 'application/x-www-form-urlencoded; charset=UTF-8'); | |
| 333 }); | |
| 334 | |
| 335 // | |
| 336 // Tests for authentication-related options. | |
| 337 // | |
| 338 | |
| 339 QUnit.test('GET with auth token', function(assert) { | |
| 340 new remoting.Xhr({ | |
| 341 method: 'GET', | |
| 342 url: 'http://foo.com', | |
| 343 oauthToken: 'my_token' | |
| 344 }).start(); | |
| 345 assert.equal(fakeXhr.requestHeaders['Authorization'], | |
| 346 'Bearer my_token'); | |
| 347 }); | |
| 348 | |
| 349 QUnit.test('GET with useIdentity', function(assert) { | |
| 350 var xhr = new remoting.Xhr({ | |
| 351 method: 'GET', | |
| 352 url: 'http://foo.com', | |
| 353 useIdentity: true | |
| 354 }); | |
| 355 | |
| 356 xhr.start(); | |
| 357 var done = assert.async(); | |
| 358 fakeXhr.addEventListener('loadstart', function() { | |
| 359 assert.equal(fakeXhr.requestHeaders['Authorization'], | |
| 360 'Bearer my_token'); | |
| 361 done(); | |
| 362 }); | |
| 363 }); | |
| 364 | |
| 365 // | |
| 366 // Error responses. | |
| 367 // | |
| 368 QUnit.test('GET with error response', function(assert) { | |
| 369 var promise = new remoting.Xhr({ | |
| 370 method: 'GET', | |
| 371 url: 'http://foo.com' | |
| 372 }).start().then(function(response) { | |
| 373 assert.ok(response.isError()); | |
| 374 assert.equal(response.status, 500); | |
| 375 assert.equal(response.getText(), 'body'); | |
| 376 }); | |
| 377 fakeXhr.respond(500, {}, 'body'); | |
| 378 return promise; | |
| 379 }); | |
| 380 | |
| 381 QUnit.test('204 is not an error', function(assert) { | |
| 382 var promise = new remoting.Xhr({ | |
| 383 method: 'GET', | |
| 384 url: 'http://foo.com' | |
| 385 }).start().then(function(response) { | |
| 386 assert.ok(!response.isError()); | |
| 387 assert.equal(response.status, 204); | |
| 388 assert.equal(response.getText(), ''); | |
| 389 }); | |
| 390 fakeXhr.respond(204, {}, null); | |
| 391 return promise; | |
| 392 }); | |
| 393 | |
| 394 QUnit.test('GET with non-HTTP response', function(assert) { | |
| 395 var promise = new remoting.Xhr({ | |
| 396 method: 'GET', | |
| 397 url: 'http://foo.com' | |
| 398 }).start().then(function(response) { | |
| 399 assert.ok(response.isError()); | |
| 400 }); | |
| 401 fakeXhr.respond(0, {}, null); | |
| 402 return promise; | |
| 403 }); | |
| 404 | |
| 405 })(); | |
| OLD | NEW |