| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Mock of XhrIo for unit testing. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.setTestOnly('goog.testing.net.XhrIo'); |
| 20 |
goog.provide('goog.testing.net.XhrIo'); |
| 21 |
|
| 22 |
goog.require('goog.array'); |
| 23 |
goog.require('goog.dom.xml'); |
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.events.EventTarget'); |
| 26 |
goog.require('goog.net.ErrorCode'); |
| 27 |
goog.require('goog.net.EventType'); |
| 28 |
goog.require('goog.net.HttpStatus'); |
| 29 |
goog.require('goog.net.XhrIo'); |
| 30 |
goog.require('goog.net.XmlHttp'); |
| 31 |
goog.require('goog.object'); |
| 32 |
goog.require('goog.structs'); |
| 33 |
goog.require('goog.structs.Map'); |
| 34 |
goog.require('goog.testing.TestQueue'); |
| 35 |
goog.require('goog.uri.utils'); |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Mock implementation of goog.net.XhrIo. This doesn't provide a mock |
| 40 |
* implementation for all cases, but it's not too hard to add them as needed. |
| 41 |
* @param {goog.testing.TestQueue=} opt_testQueue Test queue for inserting test |
| 42 |
* events. |
| 43 |
* @constructor |
| 44 |
* @extends {goog.events.EventTarget} |
| 45 |
*/ |
| 46 |
goog.testing.net.XhrIo = function(opt_testQueue) { |
| 47 |
goog.events.EventTarget.call(this); |
| 48 |
|
| 49 |
/** |
| 50 |
* Map of default headers to add to every request, use: |
| 51 |
* XhrIo.headers.set(name, value) |
| 52 |
* @type {!goog.structs.Map} |
| 53 |
*/ |
| 54 |
this.headers = new goog.structs.Map(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Queue of events write to. |
| 58 |
* @type {goog.testing.TestQueue?} |
| 59 |
* @private |
| 60 |
*/ |
| 61 |
this.testQueue_ = opt_testQueue || null; |
| 62 |
}; |
| 63 |
goog.inherits(goog.testing.net.XhrIo, goog.events.EventTarget); |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* To emulate the behavior of the actual XhrIo, we do not allow access to the |
| 68 |
* XhrIo's properties outside the event callbacks. For backwards compatibility, |
| 69 |
* we allow tests to allow access by setting this value to true. |
| 70 |
* @type {boolean} |
| 71 |
*/ |
| 72 |
goog.testing.net.XhrIo.allowUnsafeAccessToXhrIoOutsideCallbacks = false; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Alias this enum here to make mocking of goog.net.XhrIo easier. |
| 77 |
* @enum {string} |
| 78 |
*/ |
| 79 |
goog.testing.net.XhrIo.ResponseType = goog.net.XhrIo.ResponseType; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* The pattern matching the 'http' and 'https' URI schemes. |
| 84 |
* @private {!RegExp} |
| 85 |
*/ |
| 86 |
goog.testing.net.XhrIo.HTTP_SCHEME_PATTERN_ = /^https?$/i; |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* All non-disposed instances of goog.testing.net.XhrIo created |
| 91 |
* by {@link goog.testing.net.XhrIo.send} are in this Array. |
| 92 |
* @see goog.testing.net.XhrIo.cleanup |
| 93 |
* @type {!Array<!goog.testing.net.XhrIo>} |
| 94 |
* @private |
| 95 |
*/ |
| 96 |
goog.testing.net.XhrIo.sendInstances_ = []; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Returns an Array containing all non-disposed instances of |
| 101 |
* goog.testing.net.XhrIo created by {@link goog.testing.net.XhrIo.send}. |
| 102 |
* @return {!Array<!goog.testing.net.XhrIo>} Array of goog.testing.net.XhrIo |
| 103 |
* instances. |
| 104 |
*/ |
| 105 |
goog.testing.net.XhrIo.getSendInstances = function() { |
| 106 |
return goog.testing.net.XhrIo.sendInstances_; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Disposes all non-disposed instances of goog.testing.net.XhrIo created by |
| 112 |
* {@link goog.testing.net.XhrIo.send}. |
| 113 |
* @see goog.net.XhrIo.cleanup |
| 114 |
*/ |
| 115 |
goog.testing.net.XhrIo.cleanup = function() { |
| 116 |
var instances = goog.testing.net.XhrIo.sendInstances_; |
| 117 |
while (instances.length) { |
| 118 |
instances.pop().dispose(); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Simulates the static XhrIo send method. |
| 125 |
* @param {string} url Uri to make request to. |
| 126 |
* @param {Function=} opt_callback Callback function for when request is |
| 127 |
* complete. |
| 128 |
* @param {string=} opt_method Send method, default: GET. |
| 129 |
* @param {string=} opt_content Post data. |
| 130 |
* @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the |
| 131 |
* request. |
| 132 |
* @param {number=} opt_timeoutInterval Number of milliseconds after which an |
| 133 |
* incomplete request will be aborted; 0 means no timeout is set. |
| 134 |
* @param {boolean=} opt_withCredentials Whether to send credentials with the |
| 135 |
* request. Default to false. See {@link goog.net.XhrIo#setWithCredentials}. |
| 136 |
* @return {!goog.testing.net.XhrIo} The mocked sent XhrIo. |
| 137 |
*/ |
| 138 |
goog.testing.net.XhrIo.send = function( |
| 139 |
url, opt_callback, opt_method, opt_content, opt_headers, |
| 140 |
opt_timeoutInterval, opt_withCredentials) { |
| 141 |
var x = new goog.testing.net.XhrIo(); |
| 142 |
goog.testing.net.XhrIo.sendInstances_.push(x); |
| 143 |
if (opt_callback) { |
| 144 |
goog.events.listen(x, goog.net.EventType.COMPLETE, opt_callback); |
| 145 |
} |
| 146 |
goog.events.listen( |
| 147 |
x, goog.net.EventType.READY, |
| 148 |
goog.partial(goog.testing.net.XhrIo.cleanupSend_, x)); |
| 149 |
if (opt_timeoutInterval) { |
| 150 |
x.setTimeoutInterval(opt_timeoutInterval); |
| 151 |
} |
| 152 |
x.setWithCredentials(Boolean(opt_withCredentials)); |
| 153 |
x.send(url, opt_method, opt_content, opt_headers); |
| 154 |
|
| 155 |
return x; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Disposes of the specified goog.testing.net.XhrIo created by |
| 161 |
* {@link goog.testing.net.XhrIo.send} and removes it from |
| 162 |
* {@link goog.testing.net.XhrIo.pendingStaticSendInstances_}. |
| 163 |
* @param {!goog.testing.net.XhrIo} XhrIo An XhrIo created by |
| 164 |
* {@link goog.testing.net.XhrIo.send}. |
| 165 |
* @private |
| 166 |
*/ |
| 167 |
goog.testing.net.XhrIo.cleanupSend_ = function(XhrIo) { |
| 168 |
XhrIo.dispose(); |
| 169 |
goog.array.remove(goog.testing.net.XhrIo.sendInstances_, XhrIo); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Stores the simulated response headers for the requests which are sent through |
| 175 |
* this XhrIo. |
| 176 |
* @type {Object} |
| 177 |
* @private |
| 178 |
*/ |
| 179 |
goog.testing.net.XhrIo.prototype.responseHeaders_; |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Whether MockXhrIo is active. |
| 184 |
* @type {boolean} |
| 185 |
* @private |
| 186 |
*/ |
| 187 |
goog.testing.net.XhrIo.prototype.active_ = false; |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Last URI that was requested. |
| 192 |
* @type {string} |
| 193 |
* @private |
| 194 |
*/ |
| 195 |
goog.testing.net.XhrIo.prototype.lastUri_ = ''; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Last HTTP method that was requested. |
| 200 |
* @type {string|undefined} |
| 201 |
* @private |
| 202 |
*/ |
| 203 |
goog.testing.net.XhrIo.prototype.lastMethod_; |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Last POST content that was requested. |
| 208 |
* @type {string|undefined} |
| 209 |
* @private |
| 210 |
*/ |
| 211 |
goog.testing.net.XhrIo.prototype.lastContent_; |
| 212 |
|
| 213 |
|
| 214 |
/** |
| 215 |
* Additional headers that were requested in the last query. |
| 216 |
* @type {Object|goog.structs.Map|undefined} |
| 217 |
* @private |
| 218 |
*/ |
| 219 |
goog.testing.net.XhrIo.prototype.lastHeaders_; |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Last error code. |
| 224 |
* @type {goog.net.ErrorCode} |
| 225 |
* @private |
| 226 |
*/ |
| 227 |
goog.testing.net.XhrIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR; |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Last error message. |
| 232 |
* @type {string} |
| 233 |
* @private |
| 234 |
*/ |
| 235 |
goog.testing.net.XhrIo.prototype.lastError_ = ''; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* The response object. |
| 240 |
* @type {string|Document|ArrayBuffer} |
| 241 |
* @private |
| 242 |
*/ |
| 243 |
goog.testing.net.XhrIo.prototype.response_ = ''; |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* The status code. |
| 248 |
* @type {number} |
| 249 |
* @private |
| 250 |
*/ |
| 251 |
goog.testing.net.XhrIo.prototype.statusCode_ = 0; |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Mock ready state. |
| 256 |
* @type {number} |
| 257 |
* @private |
| 258 |
*/ |
| 259 |
goog.testing.net.XhrIo.prototype.readyState_ = |
| 260 |
goog.net.XmlHttp.ReadyState.UNINITIALIZED; |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Number of milliseconds after which an incomplete request will be aborted and |
| 265 |
* a {@link goog.net.EventType.TIMEOUT} event raised; 0 means no timeout is set. |
| 266 |
* @type {number} |
| 267 |
* @private |
| 268 |
*/ |
| 269 |
goog.testing.net.XhrIo.prototype.timeoutInterval_ = 0; |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* The requested type for the response. The empty string means use the default |
| 274 |
* XHR behavior. |
| 275 |
* @type {goog.net.XhrIo.ResponseType} |
| 276 |
* @private |
| 277 |
*/ |
| 278 |
goog.testing.net.XhrIo.prototype.responseType_ = |
| 279 |
goog.net.XhrIo.ResponseType.DEFAULT; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Whether a "credentialed" request is to be sent (one that is aware of cookies |
| 284 |
* and authentication) . This is applicable only for cross-domain requests and |
| 285 |
* more recent browsers that support this part of the HTTP Access Control |
| 286 |
* standard. |
| 287 |
* |
| 288 |
* @see http://dev.w3.org/2006/webapi/XMLHttpRequest-2/#withcredentials |
| 289 |
* |
| 290 |
* @type {boolean} |
| 291 |
* @private |
| 292 |
*/ |
| 293 |
goog.testing.net.XhrIo.prototype.withCredentials_ = false; |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Whether progress events shall be sent for this request. |
| 298 |
* |
| 299 |
* @type {boolean} |
| 300 |
* @private |
| 301 |
*/ |
| 302 |
goog.testing.net.XhrIo.prototype.progressEventsEnabled_ = false; |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Whether there's currently an underlying XHR object. |
| 307 |
* @type {boolean} |
| 308 |
* @private |
| 309 |
*/ |
| 310 |
goog.testing.net.XhrIo.prototype.xhr_ = false; |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Returns the number of milliseconds after which an incomplete request will be |
| 315 |
* aborted, or 0 if no timeout is set. |
| 316 |
* @return {number} Timeout interval in milliseconds. |
| 317 |
*/ |
| 318 |
goog.testing.net.XhrIo.prototype.getTimeoutInterval = function() { |
| 319 |
return this.timeoutInterval_; |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* Sets the number of milliseconds after which an incomplete request will be |
| 325 |
* aborted and a {@link goog.net.EventType.TIMEOUT} event raised; 0 means no |
| 326 |
* timeout is set. |
| 327 |
* @param {number} ms Timeout interval in milliseconds; 0 means none. |
| 328 |
*/ |
| 329 |
goog.testing.net.XhrIo.prototype.setTimeoutInterval = function(ms) { |
| 330 |
this.timeoutInterval_ = Math.max(0, ms); |
| 331 |
}; |
| 332 |
|
| 333 |
|
| 334 |
/** |
| 335 |
* Causes timeout events to be fired. |
| 336 |
*/ |
| 337 |
goog.testing.net.XhrIo.prototype.simulateTimeout = function() { |
| 338 |
this.lastErrorCode_ = goog.net.ErrorCode.TIMEOUT; |
| 339 |
this.dispatchEvent(goog.net.EventType.TIMEOUT); |
| 340 |
this.abort(goog.net.ErrorCode.TIMEOUT); |
| 341 |
}; |
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* Sets the desired type for the response. At time of writing, this is only |
| 346 |
* supported in very recent versions of WebKit (10.0.612.1 dev and later). |
| 347 |
* |
| 348 |
* If this is used, the response may only be accessed via {@link #getResponse}. |
| 349 |
* |
| 350 |
* @param {goog.net.XhrIo.ResponseType} type The desired type for the response. |
| 351 |
*/ |
| 352 |
goog.testing.net.XhrIo.prototype.setResponseType = function(type) { |
| 353 |
this.responseType_ = type; |
| 354 |
}; |
| 355 |
|
| 356 |
|
| 357 |
/** |
| 358 |
* Gets the desired type for the response. |
| 359 |
* @return {goog.net.XhrIo.ResponseType} The desired type for the response. |
| 360 |
*/ |
| 361 |
goog.testing.net.XhrIo.prototype.getResponseType = function() { |
| 362 |
return this.responseType_; |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* Sets whether a "credentialed" request that is aware of cookie and |
| 368 |
* authentication information should be made. This option is only supported by |
| 369 |
* browsers that support HTTP Access Control. As of this writing, this option |
| 370 |
* is not supported in IE. |
| 371 |
* |
| 372 |
* @param {boolean} withCredentials Whether this should be a "credentialed" |
| 373 |
* request. |
| 374 |
*/ |
| 375 |
goog.testing.net.XhrIo.prototype.setWithCredentials = function( |
| 376 |
withCredentials) { |
| 377 |
this.withCredentials_ = withCredentials; |
| 378 |
}; |
| 379 |
|
| 380 |
|
| 381 |
/** |
| 382 |
* Gets whether a "credentialed" request is to be sent. |
| 383 |
* @return {boolean} The desired type for the response. |
| 384 |
*/ |
| 385 |
goog.testing.net.XhrIo.prototype.getWithCredentials = function() { |
| 386 |
return this.withCredentials_; |
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* Sets whether progress events are enabled for this request. Note |
| 392 |
* that progress events require pre-flight OPTIONS request handling |
| 393 |
* for CORS requests, and may cause trouble with older browsers. See |
| 394 |
* goog.net.XhrIo.progressEventsEnabled_ for details. |
| 395 |
* @param {boolean} enabled Whether progress events should be enabled. |
| 396 |
*/ |
| 397 |
goog.testing.net.XhrIo.prototype.setProgressEventsEnabled = function(enabled) { |
| 398 |
this.progressEventsEnabled_ = enabled; |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
/** |
| 403 |
* Gets whether progress events are enabled. |
| 404 |
* @return {boolean} Whether progress events are enabled for this request. |
| 405 |
*/ |
| 406 |
goog.testing.net.XhrIo.prototype.getProgressEventsEnabled = function() { |
| 407 |
return this.progressEventsEnabled_; |
| 408 |
}; |
| 409 |
|
| 410 |
|
| 411 |
/** |
| 412 |
* Abort the current XMLHttpRequest |
| 413 |
* @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use - |
| 414 |
* defaults to ABORT. |
| 415 |
*/ |
| 416 |
goog.testing.net.XhrIo.prototype.abort = function(opt_failureCode) { |
| 417 |
if (this.active_) { |
| 418 |
try { |
| 419 |
this.active_ = false; |
| 420 |
this.readyState_ = goog.net.XmlHttp.ReadyState.UNINITIALIZED; |
| 421 |
this.statusCode_ = -1; |
| 422 |
this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT; |
| 423 |
this.dispatchEvent(goog.net.EventType.COMPLETE); |
| 424 |
this.dispatchEvent(goog.net.EventType.ABORT); |
| 425 |
} finally { |
| 426 |
this.simulateReady(); |
| 427 |
} |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
|
| 432 |
/** |
| 433 |
* Simulates the XhrIo send. |
| 434 |
* @param {string} url Uri to make request too. |
| 435 |
* @param {string=} opt_method Send method, default: GET. |
| 436 |
* @param {string=} opt_content Post data. |
| 437 |
* @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the |
| 438 |
* request. |
| 439 |
*/ |
| 440 |
goog.testing.net.XhrIo.prototype.send = function( |
| 441 |
url, opt_method, opt_content, opt_headers) { |
| 442 |
if (this.xhr_) { |
| 443 |
throw new Error('[goog.net.XhrIo] Object is active with another request'); |
| 444 |
} |
| 445 |
|
| 446 |
this.lastUri_ = url; |
| 447 |
this.lastMethod_ = opt_method || 'GET'; |
| 448 |
this.lastContent_ = opt_content; |
| 449 |
if (!this.headers.isEmpty()) { |
| 450 |
this.lastHeaders_ = this.headers.toObject(); |
| 451 |
// Add headers specific to this request |
| 452 |
if (opt_headers) { |
| 453 |
goog.structs.forEach(opt_headers, goog.bind(function(value, key) { |
| 454 |
this.lastHeaders_[key] = value; |
| 455 |
}, this)); |
| 456 |
} |
| 457 |
} else { |
| 458 |
this.lastHeaders_ = opt_headers; |
| 459 |
} |
| 460 |
|
| 461 |
if (this.testQueue_) { |
| 462 |
this.testQueue_.enqueue(['s', url, opt_method, opt_content, opt_headers]); |
| 463 |
} |
| 464 |
this.xhr_ = true; |
| 465 |
this.active_ = true; |
| 466 |
this.readyState_ = goog.net.XmlHttp.ReadyState.UNINITIALIZED; |
| 467 |
this.simulateReadyStateChange(goog.net.XmlHttp.ReadyState.LOADING); |
| 468 |
}; |
| 469 |
|
| 470 |
|
| 471 |
/** |
| 472 |
* Creates a new XHR object. |
| 473 |
* @return {goog.net.XhrLike.OrNative} The newly created XHR |
| 474 |
* object. |
| 475 |
* @protected |
| 476 |
*/ |
| 477 |
goog.testing.net.XhrIo.prototype.createXhr = function() { |
| 478 |
return goog.net.XmlHttp(); |
| 479 |
}; |
| 480 |
|
| 481 |
|
| 482 |
/** |
| 483 |
* Simulates changing to the new ready state. |
| 484 |
* @param {number} readyState Ready state to change to. |
| 485 |
*/ |
| 486 |
goog.testing.net.XhrIo.prototype.simulateReadyStateChange = function( |
| 487 |
readyState) { |
| 488 |
if (readyState < this.readyState_) { |
| 489 |
throw new Error('Readystate cannot go backwards'); |
| 490 |
} |
| 491 |
|
| 492 |
// INTERACTIVE can be dispatched repeatedly as more data is reported. |
| 493 |
if (readyState == goog.net.XmlHttp.ReadyState.INTERACTIVE && |
| 494 |
readyState == this.readyState_) { |
| 495 |
this.dispatchEvent(goog.net.EventType.READY_STATE_CHANGE); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
while (this.readyState_ < readyState) { |
| 500 |
this.readyState_++; |
| 501 |
this.dispatchEvent(goog.net.EventType.READY_STATE_CHANGE); |
| 502 |
|
| 503 |
if (this.readyState_ == goog.net.XmlHttp.ReadyState.COMPLETE) { |
| 504 |
this.active_ = false; |
| 505 |
this.dispatchEvent(goog.net.EventType.COMPLETE); |
| 506 |
} |
| 507 |
} |
| 508 |
}; |
| 509 |
|
| 510 |
|
| 511 |
/** |
| 512 |
* Simulate receiving some bytes but the request not fully completing, and |
| 513 |
* the XHR entering the 'INTERACTIVE' state. |
| 514 |
* @param {string} partialResponse A string to append to the response text. |
| 515 |
* @param {Object=} opt_headers Simulated response headers. |
| 516 |
*/ |
| 517 |
goog.testing.net.XhrIo.prototype.simulatePartialResponse = function( |
| 518 |
partialResponse, opt_headers) { |
| 519 |
this.response_ += partialResponse; |
| 520 |
this.responseHeaders_ = opt_headers || {}; |
| 521 |
this.statusCode_ = 200; |
| 522 |
this.simulateReadyStateChange(goog.net.XmlHttp.ReadyState.INTERACTIVE); |
| 523 |
}; |
| 524 |
|
| 525 |
|
| 526 |
/** |
| 527 |
* Simulates receiving a response. |
| 528 |
* @param {number} statusCode Simulated status code. |
| 529 |
* @param {string|Document|ArrayBuffer|null} response Simulated response. |
| 530 |
* @param {Object=} opt_headers Simulated response headers. |
| 531 |
*/ |
| 532 |
goog.testing.net.XhrIo.prototype.simulateResponse = function( |
| 533 |
statusCode, response, opt_headers) { |
| 534 |
// This library allows a response to be simulated without send ever being |
| 535 |
// called. If there are no send instances, then just pretend that xhr_ and |
| 536 |
// active_ have been set to true. |
| 537 |
if (!goog.testing.net.XhrIo.allowUnsafeAccessToXhrIoOutsideCallbacks && |
| 538 |
!goog.testing.net.XhrIo.sendInstances_.length) { |
| 539 |
this.xhr_ = true; |
| 540 |
this.active_ = true; |
| 541 |
} |
| 542 |
this.statusCode_ = statusCode; |
| 543 |
this.response_ = response || ''; |
| 544 |
this.responseHeaders_ = opt_headers || {}; |
| 545 |
|
| 546 |
try { |
| 547 |
if (this.isSuccess()) { |
| 548 |
this.simulateReadyStateChange(goog.net.XmlHttp.ReadyState.COMPLETE); |
| 549 |
this.dispatchEvent(goog.net.EventType.SUCCESS); |
| 550 |
} else { |
| 551 |
this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR; |
| 552 |
this.lastError_ = this.getStatusText() + ' [' + this.getStatus() + ']'; |
| 553 |
this.simulateReadyStateChange(goog.net.XmlHttp.ReadyState.COMPLETE); |
| 554 |
this.dispatchEvent(goog.net.EventType.ERROR); |
| 555 |
} |
| 556 |
} finally { |
| 557 |
this.simulateReady(); |
| 558 |
} |
| 559 |
}; |
| 560 |
|
| 561 |
|
| 562 |
/** |
| 563 |
* Simulates the Xhr is ready for the next request. |
| 564 |
*/ |
| 565 |
goog.testing.net.XhrIo.prototype.simulateReady = function() { |
| 566 |
this.active_ = false; |
| 567 |
this.xhr_ = false; |
| 568 |
this.dispatchEvent(goog.net.EventType.READY); |
| 569 |
}; |
| 570 |
|
| 571 |
|
| 572 |
/** |
| 573 |
* Simulates the Xhr progress event. |
| 574 |
* @param {boolean} lengthComputable Whether progress is measurable. |
| 575 |
* @param {number} loaded Amount of work already performed. |
| 576 |
* @param {number} total Total amount of work to perform. |
| 577 |
* @param {boolean=} opt_isDownload Whether the progress is from a download or |
| 578 |
* upload. |
| 579 |
*/ |
| 580 |
goog.testing.net.XhrIo.prototype.simulateProgress = function( |
| 581 |
lengthComputable, loaded, total, opt_isDownload) { |
| 582 |
var progressEvent = { |
| 583 |
type: goog.net.EventType.PROGRESS, |
| 584 |
lengthComputable: lengthComputable, |
| 585 |
loaded: loaded, |
| 586 |
total: total |
| 587 |
}; |
| 588 |
this.dispatchEvent(progressEvent); |
| 589 |
var specificProgress = goog.object.clone(progressEvent); |
| 590 |
specificProgress.type = opt_isDownload ? |
| 591 |
goog.net.EventType.DOWNLOAD_PROGRESS : |
| 592 |
goog.net.EventType.UPLOAD_PROGRESS; |
| 593 |
this.dispatchEvent(specificProgress); |
| 594 |
}; |
| 595 |
|
| 596 |
|
| 597 |
/** |
| 598 |
* @return {boolean} Whether there is an active request. |
| 599 |
*/ |
| 600 |
goog.testing.net.XhrIo.prototype.isActive = function() { |
| 601 |
return !!this.xhr_; |
| 602 |
}; |
| 603 |
|
| 604 |
|
| 605 |
/** |
| 606 |
* Has the request completed. |
| 607 |
* @return {boolean} Whether the request has completed. |
| 608 |
*/ |
| 609 |
goog.testing.net.XhrIo.prototype.isComplete = function() { |
| 610 |
return this.readyState_ == goog.net.XmlHttp.ReadyState.COMPLETE; |
| 611 |
}; |
| 612 |
|
| 613 |
|
| 614 |
/** |
| 615 |
* Has the request compeleted with a success. |
| 616 |
* @return {boolean} Whether the request compeleted successfully. |
| 617 |
*/ |
| 618 |
goog.testing.net.XhrIo.prototype.isSuccess = function() { |
| 619 |
var status = this.getStatus(); |
| 620 |
// A zero status code is considered successful for local files. |
| 621 |
return goog.net.HttpStatus.isSuccess(status) || |
| 622 |
status === 0 && !this.isLastUriEffectiveSchemeHttp_(); |
| 623 |
}; |
| 624 |
|
| 625 |
|
| 626 |
/** |
| 627 |
* @return {boolean} whether the effective scheme of the last URI that was |
| 628 |
* fetched was 'http' or 'https'. |
| 629 |
* @private |
| 630 |
*/ |
| 631 |
goog.testing.net.XhrIo.prototype.isLastUriEffectiveSchemeHttp_ = function() { |
| 632 |
var scheme = goog.uri.utils.getEffectiveScheme(String(this.lastUri_)); |
| 633 |
return goog.testing.net.XhrIo.HTTP_SCHEME_PATTERN_.test(scheme); |
| 634 |
}; |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* Returns the readystate. |
| 639 |
* @return {number} goog.net.XmlHttp.ReadyState.*. |
| 640 |
*/ |
| 641 |
goog.testing.net.XhrIo.prototype.getReadyState = function() { |
| 642 |
return this.readyState_; |
| 643 |
}; |
| 644 |
|
| 645 |
|
| 646 |
/** |
| 647 |
* Get the status from the Xhr object. Will only return correct result when |
| 648 |
* called from the context of a callback. |
| 649 |
* @return {number} Http status. |
| 650 |
*/ |
| 651 |
goog.testing.net.XhrIo.prototype.getStatus = function() { |
| 652 |
return this.statusCode_; |
| 653 |
}; |
| 654 |
|
| 655 |
|
| 656 |
/** |
| 657 |
* Get the status text from the Xhr object. Will only return correct result |
| 658 |
* when called from the context of a callback. |
| 659 |
* @return {string} Status text. |
| 660 |
*/ |
| 661 |
goog.testing.net.XhrIo.prototype.getStatusText = function() { |
| 662 |
return ''; |
| 663 |
}; |
| 664 |
|
| 665 |
|
| 666 |
/** |
| 667 |
* Gets the last error message. |
| 668 |
* @return {goog.net.ErrorCode} Last error code. |
| 669 |
*/ |
| 670 |
goog.testing.net.XhrIo.prototype.getLastErrorCode = function() { |
| 671 |
return this.lastErrorCode_; |
| 672 |
}; |
| 673 |
|
| 674 |
|
| 675 |
/** |
| 676 |
* Gets the last error message. |
| 677 |
* @return {string} Last error message. |
| 678 |
*/ |
| 679 |
goog.testing.net.XhrIo.prototype.getLastError = function() { |
| 680 |
return this.lastError_; |
| 681 |
}; |
| 682 |
|
| 683 |
|
| 684 |
/** |
| 685 |
* Gets the last URI that was requested. |
| 686 |
* @return {string} Last URI. |
| 687 |
*/ |
| 688 |
goog.testing.net.XhrIo.prototype.getLastUri = function() { |
| 689 |
return this.lastUri_; |
| 690 |
}; |
| 691 |
|
| 692 |
|
| 693 |
/** |
| 694 |
* Gets the last HTTP method that was requested. |
| 695 |
* @return {string|undefined} Last HTTP method used by send. |
| 696 |
*/ |
| 697 |
goog.testing.net.XhrIo.prototype.getLastMethod = function() { |
| 698 |
return this.lastMethod_; |
| 699 |
}; |
| 700 |
|
| 701 |
|
| 702 |
/** |
| 703 |
* Gets the last POST content that was requested. |
| 704 |
* @return {string|undefined} Last POST content or undefined if last request was |
| 705 |
* a GET. |
| 706 |
*/ |
| 707 |
goog.testing.net.XhrIo.prototype.getLastContent = function() { |
| 708 |
return this.lastContent_; |
| 709 |
}; |
| 710 |
|
| 711 |
|
| 712 |
/** |
| 713 |
* Gets the headers of the last request. |
| 714 |
* @return {Object|goog.structs.Map|undefined} Last headers manually set in send |
| 715 |
* call or undefined if no additional headers were specified. |
| 716 |
*/ |
| 717 |
goog.testing.net.XhrIo.prototype.getLastRequestHeaders = function() { |
| 718 |
return this.lastHeaders_; |
| 719 |
}; |
| 720 |
|
| 721 |
|
| 722 |
/** |
| 723 |
* Returns true if there is a valid xhr, or if |
| 724 |
* allowUnsafeAccessToXhrIoOutsideCallbacks is false. |
| 725 |
* @return {boolean} |
| 726 |
* @private |
| 727 |
*/ |
| 728 |
goog.testing.net.XhrIo.prototype.checkXhr_ = function() { |
| 729 |
return ( |
| 730 |
goog.testing.net.XhrIo.allowUnsafeAccessToXhrIoOutsideCallbacks || |
| 731 |
!!this.xhr_); |
| 732 |
}; |
| 733 |
|
| 734 |
|
| 735 |
/** |
| 736 |
* Gets the response text from the Xhr object. Will only return correct result |
| 737 |
* when called from the context of a callback. |
| 738 |
* @return {string} Result from the server. |
| 739 |
*/ |
| 740 |
goog.testing.net.XhrIo.prototype.getResponseText = function() { |
| 741 |
if (!this.checkXhr_()) { |
| 742 |
return ''; |
| 743 |
} else if (goog.isString(this.response_)) { |
| 744 |
return this.response_; |
| 745 |
} else if ( |
| 746 |
goog.global['ArrayBuffer'] && this.response_ instanceof ArrayBuffer) { |
| 747 |
return ''; |
| 748 |
} else { |
| 749 |
return goog.dom.xml.serialize(/** @type {Document} */ (this.response_)); |
| 750 |
} |
| 751 |
}; |
| 752 |
|
| 753 |
|
| 754 |
/** |
| 755 |
* Gets the response body from the Xhr object. Will only return correct result |
| 756 |
* when called from the context of a callback. |
| 757 |
* @return {Object} Binary result from the server or null. |
| 758 |
*/ |
| 759 |
goog.testing.net.XhrIo.prototype.getResponseBody = function() { |
| 760 |
return null; |
| 761 |
}; |
| 762 |
|
| 763 |
|
| 764 |
/** |
| 765 |
* Gets the response and evaluates it as JSON from the Xhr object. Will only |
| 766 |
* return correct result when called from the context of a callback. |
| 767 |
* @param {string=} opt_xssiPrefix Optional XSSI prefix string to use for |
| 768 |
* stripping of the response before parsing. This needs to be set only if |
| 769 |
* your backend server prepends the same prefix string to the JSON response. |
| 770 |
* @return {Object|undefined} JavaScript object. |
| 771 |
* @throws Error if s is invalid JSON. |
| 772 |
*/ |
| 773 |
goog.testing.net.XhrIo.prototype.getResponseJson = function(opt_xssiPrefix) { |
| 774 |
if (!this.checkXhr_()) { |
| 775 |
return undefined; |
| 776 |
} |
| 777 |
|
| 778 |
var responseText = this.getResponseText(); |
| 779 |
if (opt_xssiPrefix && responseText.indexOf(opt_xssiPrefix) == 0) { |
| 780 |
responseText = responseText.substring(opt_xssiPrefix.length); |
| 781 |
} |
| 782 |
|
| 783 |
return /** @type {!Object} */ (JSON.parse(responseText)); |
| 784 |
}; |
| 785 |
|
| 786 |
|
| 787 |
/** |
| 788 |
* Gets the response XML from the Xhr object. Will only return correct result |
| 789 |
* when called from the context of a callback. |
| 790 |
* @return {Document} Result from the server if it was XML. |
| 791 |
*/ |
| 792 |
goog.testing.net.XhrIo.prototype.getResponseXml = function() { |
| 793 |
if (!this.checkXhr_()) { |
| 794 |
return null; |
| 795 |
} |
| 796 |
// NOTE(user): I haven't found out how to check in Internet Explorer |
| 797 |
// whether the response is XML document, so I do it the other way around. |
| 798 |
return goog.isString(this.response_) || |
| 799 |
(goog.global['ArrayBuffer'] && |
| 800 |
this.response_ instanceof ArrayBuffer) ? |
| 801 |
null : |
| 802 |
/** @type {Document} */ (this.response_); |
| 803 |
}; |
| 804 |
|
| 805 |
|
| 806 |
/** |
| 807 |
* Get the response as the type specificed by {@link #setResponseType}. At time |
| 808 |
* of writing, this is only supported in very recent versions of WebKit |
| 809 |
* (10.0.612.1 dev and later). |
| 810 |
* |
| 811 |
* @return {*} The response. |
| 812 |
*/ |
| 813 |
goog.testing.net.XhrIo.prototype.getResponse = function() { |
| 814 |
return this.checkXhr_() ? this.response_ : null; |
| 815 |
}; |
| 816 |
|
| 817 |
|
| 818 |
/** |
| 819 |
* Get the value of the response-header with the given name from the Xhr object |
| 820 |
* Will only return correct result when called from the context of a callback |
| 821 |
* and the request has completed |
| 822 |
* @param {string} key The name of the response-header to retrieve. |
| 823 |
* @return {string|undefined} The value of the response-header named key. |
| 824 |
*/ |
| 825 |
goog.testing.net.XhrIo.prototype.getResponseHeader = function(key) { |
| 826 |
if (!this.checkXhr_() || !this.isComplete()) { |
| 827 |
return undefined; |
| 828 |
} |
| 829 |
return this.responseHeaders_[key]; |
| 830 |
}; |
| 831 |
|
| 832 |
|
| 833 |
/** |
| 834 |
* Gets the text of all the headers in the response. |
| 835 |
* Will only return correct result when called from the context of a callback |
| 836 |
* and the request has completed |
| 837 |
* @return {string} The string containing all the response headers. |
| 838 |
*/ |
| 839 |
goog.testing.net.XhrIo.prototype.getAllResponseHeaders = function() { |
| 840 |
if (!this.checkXhr_() || !this.isComplete()) { |
| 841 |
return ''; |
| 842 |
} |
| 843 |
return this.getAllStreamingResponseHeaders(); |
| 844 |
}; |
| 845 |
|
| 846 |
|
| 847 |
/** |
| 848 |
* Returns all response headers as a key-value map. |
| 849 |
* Multiple values for the same header key can be combined into one, |
| 850 |
* separated by a comma and a space. |
| 851 |
* Note that the native getResponseHeader method for retrieving a single header |
| 852 |
* does a case insensitive match on the header name. This method does not |
| 853 |
* include any case normalization logic, it will just return a key-value |
| 854 |
* representation of the headers. |
| 855 |
* See: http://www.w3.org/TR/XMLHttpRequest/#the-getresponseheader()-method |
| 856 |
* @return {!Object<string, string>} An object with the header keys as keys |
| 857 |
* and header values as values. |
| 858 |
*/ |
| 859 |
goog.testing.net.XhrIo.prototype.getResponseHeaders = function() { |
| 860 |
if (!this.checkXhr_() || !this.isComplete()) { |
| 861 |
return {}; |
| 862 |
} |
| 863 |
var headersObject = {}; |
| 864 |
goog.object.forEach(this.responseHeaders_, function(value, key) { |
| 865 |
if (headersObject[key]) { |
| 866 |
headersObject[key] += ', ' + value; |
| 867 |
} else { |
| 868 |
headersObject[key] = value; |
| 869 |
} |
| 870 |
}); |
| 871 |
return headersObject; |
| 872 |
}; |
| 873 |
|
| 874 |
|
| 875 |
/** |
| 876 |
* Get the value of the response-header with the given name from the Xhr object. |
| 877 |
* As opposed to {@link #getResponseHeader}, this method does not require that |
| 878 |
* the request has completed. |
| 879 |
* @param {string} key The name of the response-header to retrieve. |
| 880 |
* @return {?string} The value of the response-header, or null if it is |
| 881 |
* unavailable. |
| 882 |
*/ |
| 883 |
goog.testing.net.XhrIo.prototype.getStreamingResponseHeader = function(key) { |
| 884 |
if (!this.checkXhr_()) { |
| 885 |
return null; |
| 886 |
} |
| 887 |
return key in this.responseHeaders_ ? this.responseHeaders_[key] : null; |
| 888 |
}; |
| 889 |
|
| 890 |
|
| 891 |
/** |
| 892 |
* Gets the text of all the headers in the response. As opposed to |
| 893 |
* {@link #getAllResponseHeaders}, this method does not require that the request |
| 894 |
* has completed. |
| 895 |
* @return {string} The value of the response headers or empty string. |
| 896 |
*/ |
| 897 |
goog.testing.net.XhrIo.prototype.getAllStreamingResponseHeaders = function() { |
| 898 |
if (!this.checkXhr_()) { |
| 899 |
return ''; |
| 900 |
} |
| 901 |
var headers = []; |
| 902 |
goog.object.forEach(this.responseHeaders_, function(value, name) { |
| 903 |
headers.push(name + ': ' + value); |
| 904 |
}); |
| 905 |
return headers.join('\r\n'); |
| 906 |
}; |