mockiframeio.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. * @fileoverview Mock of IframeIo for unit testing.
  16. */
  17. goog.provide('goog.net.MockIFrameIo');
  18. goog.require('goog.events.EventTarget');
  19. goog.require('goog.json');
  20. goog.require('goog.net.ErrorCode');
  21. goog.require('goog.net.EventType');
  22. goog.require('goog.net.IframeIo');
  23. goog.forwardDeclare('goog.testing.TestQueue');
  24. /**
  25. * Mock implementation of goog.net.IframeIo. This doesn't provide a mock
  26. * implementation for all cases, but it's not too hard to add them as needed.
  27. * @param {goog.testing.TestQueue} testQueue Test queue for inserting test
  28. * events.
  29. * @constructor
  30. * @extends {goog.events.EventTarget}
  31. * @final
  32. * @deprecated Use goog.testing.net.MockIFrameIo instead.
  33. */
  34. goog.net.MockIFrameIo = function(testQueue) {
  35. goog.events.EventTarget.call(this);
  36. /**
  37. * Queue of events write to
  38. * @type {goog.testing.TestQueue}
  39. * @private
  40. */
  41. this.testQueue_ = testQueue;
  42. };
  43. goog.inherits(goog.net.MockIFrameIo, goog.events.EventTarget);
  44. /**
  45. * Whether MockIFrameIo is active.
  46. * @type {boolean}
  47. * @private
  48. */
  49. goog.net.MockIFrameIo.prototype.active_ = false;
  50. /**
  51. * Last content.
  52. * @type {string}
  53. * @private
  54. */
  55. goog.net.MockIFrameIo.prototype.lastContent_ = '';
  56. /**
  57. * Last error code.
  58. * @type {goog.net.ErrorCode}
  59. * @private
  60. */
  61. goog.net.MockIFrameIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
  62. /**
  63. * Last error message.
  64. * @type {string}
  65. * @private
  66. */
  67. goog.net.MockIFrameIo.prototype.lastError_ = '';
  68. /**
  69. * Last custom error.
  70. * @type {Object}
  71. * @private
  72. */
  73. goog.net.MockIFrameIo.prototype.lastCustomError_ = null;
  74. /**
  75. * Last URI.
  76. * @type {goog.Uri}
  77. * @private
  78. */
  79. goog.net.MockIFrameIo.prototype.lastUri_ = null;
  80. /** @private {Function} */
  81. goog.net.MockIFrameIo.prototype.errorChecker_;
  82. /** @private {boolean} */
  83. goog.net.MockIFrameIo.prototype.success_;
  84. /** @private {boolean} */
  85. goog.net.MockIFrameIo.prototype.complete_;
  86. /**
  87. * Simulates the iframe send.
  88. *
  89. * @param {goog.Uri|string} uri Uri of the request.
  90. * @param {string=} opt_method Default is GET, POST uses a form to submit the
  91. * request.
  92. * @param {boolean=} opt_noCache Append a timestamp to the request to avoid
  93. * caching.
  94. * @param {Object|goog.structs.Map=} opt_data Map of key-value pairs.
  95. */
  96. goog.net.MockIFrameIo.prototype.send = function(
  97. uri, opt_method, opt_noCache, opt_data) {
  98. if (this.active_) {
  99. throw Error('[goog.net.IframeIo] Unable to send, already active.');
  100. }
  101. this.testQueue_.enqueue(['s', uri, opt_method, opt_noCache, opt_data]);
  102. this.complete_ = false;
  103. this.active_ = true;
  104. };
  105. /**
  106. * Simulates the iframe send from a form.
  107. * @param {Element} form Form element used to send the request to the server.
  108. * @param {string=} opt_uri Uri to set for the destination of the request, by
  109. * default the uri will come from the form.
  110. * @param {boolean=} opt_noCache Append a timestamp to the request to avoid
  111. * caching.
  112. */
  113. goog.net.MockIFrameIo.prototype.sendFromForm = function(
  114. form, opt_uri, opt_noCache) {
  115. if (this.active_) {
  116. throw Error('[goog.net.IframeIo] Unable to send, already active.');
  117. }
  118. this.testQueue_.enqueue(['s', form, opt_uri, opt_noCache]);
  119. this.complete_ = false;
  120. this.active_ = true;
  121. };
  122. /**
  123. * Simulates aborting the current Iframe request.
  124. * @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use -
  125. * defaults to ABORT.
  126. */
  127. goog.net.MockIFrameIo.prototype.abort = function(opt_failureCode) {
  128. if (this.active_) {
  129. this.testQueue_.enqueue(['a', opt_failureCode]);
  130. this.complete_ = false;
  131. this.active_ = false;
  132. this.success_ = false;
  133. this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;
  134. this.dispatchEvent(goog.net.EventType.ABORT);
  135. this.simulateReady();
  136. }
  137. };
  138. /**
  139. * Simulates receive of incremental data.
  140. * @param {Object} data Data.
  141. */
  142. goog.net.MockIFrameIo.prototype.simulateIncrementalData = function(data) {
  143. this.dispatchEvent(new goog.net.IframeIo.IncrementalDataEvent(data));
  144. };
  145. /**
  146. * Simulates the iframe is done.
  147. * @param {goog.net.ErrorCode} errorCode The error code for any error that
  148. * should be simulated.
  149. */
  150. goog.net.MockIFrameIo.prototype.simulateDone = function(errorCode) {
  151. if (errorCode) {
  152. this.success_ = false;
  153. this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;
  154. this.lastError_ = this.getLastError();
  155. this.dispatchEvent(goog.net.EventType.ERROR);
  156. } else {
  157. this.success_ = true;
  158. this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
  159. this.dispatchEvent(goog.net.EventType.SUCCESS);
  160. }
  161. this.complete_ = true;
  162. this.dispatchEvent(goog.net.EventType.COMPLETE);
  163. };
  164. /**
  165. * Simulates the IFrame is ready for the next request.
  166. */
  167. goog.net.MockIFrameIo.prototype.simulateReady = function() {
  168. this.dispatchEvent(goog.net.EventType.READY);
  169. };
  170. /**
  171. * @return {boolean} True if transfer is complete.
  172. */
  173. goog.net.MockIFrameIo.prototype.isComplete = function() {
  174. return this.complete_;
  175. };
  176. /**
  177. * @return {boolean} True if transfer was successful.
  178. */
  179. goog.net.MockIFrameIo.prototype.isSuccess = function() {
  180. return this.success_;
  181. };
  182. /**
  183. * @return {boolean} True if a transfer is in progress.
  184. */
  185. goog.net.MockIFrameIo.prototype.isActive = function() {
  186. return this.active_;
  187. };
  188. /**
  189. * Returns the last response text (i.e. the text content of the iframe).
  190. * Assumes plain text!
  191. * @return {string} Result from the server.
  192. */
  193. goog.net.MockIFrameIo.prototype.getResponseText = function() {
  194. return this.lastContent_;
  195. };
  196. /**
  197. * Parses the content as JSON. This is a safe parse and may throw an error
  198. * if the response is malformed.
  199. * @return {Object} The parsed content.
  200. */
  201. goog.net.MockIFrameIo.prototype.getResponseJson = function() {
  202. return goog.json.parse(this.lastContent_);
  203. };
  204. /**
  205. * Get the uri of the last request.
  206. * @return {goog.Uri} Uri of last request.
  207. */
  208. goog.net.MockIFrameIo.prototype.getLastUri = function() {
  209. return this.lastUri_;
  210. };
  211. /**
  212. * Gets the last error code.
  213. * @return {goog.net.ErrorCode} Last error code.
  214. */
  215. goog.net.MockIFrameIo.prototype.getLastErrorCode = function() {
  216. return this.lastErrorCode_;
  217. };
  218. /**
  219. * Gets the last error message.
  220. * @return {string} Last error message.
  221. */
  222. goog.net.MockIFrameIo.prototype.getLastError = function() {
  223. return goog.net.ErrorCode.getDebugMessage(this.lastErrorCode_);
  224. };
  225. /**
  226. * Gets the last custom error.
  227. * @return {Object} Last custom error.
  228. */
  229. goog.net.MockIFrameIo.prototype.getLastCustomError = function() {
  230. return this.lastCustomError_;
  231. };
  232. /**
  233. * Sets the callback function used to check if a loaded IFrame is in an error
  234. * state.
  235. * @param {Function} fn Callback that expects a document object as it's single
  236. * argument.
  237. */
  238. goog.net.MockIFrameIo.prototype.setErrorChecker = function(fn) {
  239. this.errorChecker_ = fn;
  240. };
  241. /**
  242. * Gets the callback function used to check if a loaded IFrame is in an error
  243. * state.
  244. * @return {Function} A callback that expects a document object as it's single
  245. * argument.
  246. */
  247. goog.net.MockIFrameIo.prototype.getErrorChecker = function() {
  248. return this.errorChecker_;
  249. };