jquery.mockjax.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*!
  2. * MockJax - jQuery Plugin to Mock Ajax requests
  3. *
  4. * Version: 1.5.3
  5. * Released:
  6. * Home: http://github.com/appendto/jquery-mockjax
  7. * Author: Jonathan Sharp (http://jdsharp.com)
  8. * License: MIT,GPL
  9. *
  10. * Copyright (c) 2011 appendTo LLC.
  11. * Dual licensed under the MIT or GPL licenses.
  12. * http://appendto.com/open-source-licenses
  13. */
  14. (function($) {
  15. var _ajax = $.ajax,
  16. mockHandlers = [],
  17. mockedAjaxCalls = [],
  18. CALLBACK_REGEX = /=\?(&|$)/,
  19. jsc = (new Date()).getTime();
  20. // Parse the given XML string.
  21. function parseXML(xml) {
  22. if ( window.DOMParser == undefined && window.ActiveXObject ) {
  23. DOMParser = function() { };
  24. DOMParser.prototype.parseFromString = function( xmlString ) {
  25. var doc = new ActiveXObject('Microsoft.XMLDOM');
  26. doc.async = 'false';
  27. doc.loadXML( xmlString );
  28. return doc;
  29. };
  30. }
  31. try {
  32. var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
  33. if ( $.isXMLDoc( xmlDoc ) ) {
  34. var err = $('parsererror', xmlDoc);
  35. if ( err.length == 1 ) {
  36. throw('Error: ' + $(xmlDoc).text() );
  37. }
  38. } else {
  39. throw('Unable to parse XML');
  40. }
  41. return xmlDoc;
  42. } catch( e ) {
  43. var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
  44. $(document).trigger('xmlParseError', [ msg ]);
  45. return undefined;
  46. }
  47. }
  48. // Trigger a jQuery event
  49. function trigger(s, type, args) {
  50. (s.context ? $(s.context) : $.event).trigger(type, args);
  51. }
  52. // Check if the data field on the mock handler and the request match. This
  53. // can be used to restrict a mock handler to being used only when a certain
  54. // set of data is passed to it.
  55. function isMockDataEqual( mock, live ) {
  56. var identical = true;
  57. // Test for situations where the data is a querystring (not an object)
  58. if (typeof live === 'string') {
  59. // Querystring may be a regex
  60. return $.isFunction( mock.test ) ? mock.test(live) : mock == live;
  61. }
  62. $.each(mock, function(k) {
  63. if ( live[k] === undefined ) {
  64. identical = false;
  65. return identical;
  66. } else {
  67. if ( typeof live[k] === 'object' && live[k] !== null ) {
  68. if ( identical && $.isArray( live[k] ) ) {
  69. identical = $.isArray( mock[k] ) && live[k].length === mock[k].length;
  70. }
  71. identical = identical && isMockDataEqual(mock[k], live[k]);
  72. } else {
  73. if ( mock[k] && $.isFunction( mock[k].test ) ) {
  74. identical = identical && mock[k].test(live[k]);
  75. } else {
  76. identical = identical && ( mock[k] == live[k] );
  77. }
  78. }
  79. }
  80. });
  81. return identical;
  82. }
  83. // See if a mock handler property matches the default settings
  84. function isDefaultSetting(handler, property) {
  85. return handler[property] === $.mockjaxSettings[property];
  86. }
  87. // Check the given handler should mock the given request
  88. function getMockForRequest( handler, requestSettings ) {
  89. // If the mock was registered with a function, let the function decide if we
  90. // want to mock this request
  91. if ( $.isFunction(handler) ) {
  92. return handler( requestSettings );
  93. }
  94. // Inspect the URL of the request and check if the mock handler's url
  95. // matches the url for this ajax request
  96. if ( $.isFunction(handler.url.test) ) {
  97. // The user provided a regex for the url, test it
  98. if ( !handler.url.test( requestSettings.url ) ) {
  99. return null;
  100. }
  101. } else {
  102. // Look for a simple wildcard '*' or a direct URL match
  103. var star = handler.url.indexOf('*');
  104. if (handler.url !== requestSettings.url && star === -1 ||
  105. !new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\$&").replace(/\*/g, '.+')).test(requestSettings.url)) {
  106. return null;
  107. }
  108. }
  109. // Inspect the data submitted in the request (either POST body or GET query string)
  110. if ( handler.data ) {
  111. if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) {
  112. // They're not identical, do not mock this request
  113. return null;
  114. }
  115. }
  116. // Inspect the request type
  117. if ( handler && handler.type &&
  118. handler.type.toLowerCase() != requestSettings.type.toLowerCase() ) {
  119. // The request type doesn't match (GET vs. POST)
  120. return null;
  121. }
  122. return handler;
  123. }
  124. // Process the xhr objects send operation
  125. function _xhrSend(mockHandler, requestSettings, origSettings) {
  126. // This is a substitute for < 1.4 which lacks $.proxy
  127. var process = (function(that) {
  128. return function() {
  129. return (function() {
  130. var onReady;
  131. // The request has returned
  132. this.status = mockHandler.status;
  133. this.statusText = mockHandler.statusText;
  134. this.readyState = 4;
  135. // We have an executable function, call it to give
  136. // the mock handler a chance to update it's data
  137. if ( $.isFunction(mockHandler.response) ) {
  138. mockHandler.response(origSettings);
  139. }
  140. // Copy over our mock to our xhr object before passing control back to
  141. // jQuery's onreadystatechange callback
  142. if ( requestSettings.dataType == 'json' && ( typeof mockHandler.responseText == 'object' ) ) {
  143. this.responseText = JSON.stringify(mockHandler.responseText);
  144. } else if ( requestSettings.dataType == 'xml' ) {
  145. if ( typeof mockHandler.responseXML == 'string' ) {
  146. this.responseXML = parseXML(mockHandler.responseXML);
  147. //in jQuery 1.9.1+, responseXML is processed differently and relies on responseText
  148. this.responseText = mockHandler.responseXML;
  149. } else {
  150. this.responseXML = mockHandler.responseXML;
  151. }
  152. } else {
  153. this.responseText = mockHandler.responseText;
  154. }
  155. if( typeof mockHandler.status == 'number' || typeof mockHandler.status == 'string' ) {
  156. this.status = mockHandler.status;
  157. }
  158. if( typeof mockHandler.statusText === "string") {
  159. this.statusText = mockHandler.statusText;
  160. }
  161. // jQuery 2.0 renamed onreadystatechange to onload
  162. onReady = this.onreadystatechange || this.onload;
  163. // jQuery < 1.4 doesn't have onreadystate change for xhr
  164. if ( $.isFunction( onReady ) ) {
  165. if( mockHandler.isTimeout) {
  166. this.status = -1;
  167. }
  168. onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined );
  169. } else if ( mockHandler.isTimeout ) {
  170. // Fix for 1.3.2 timeout to keep success from firing.
  171. this.status = -1;
  172. }
  173. }).apply(that);
  174. };
  175. })(this);
  176. if ( mockHandler.proxy ) {
  177. // We're proxying this request and loading in an external file instead
  178. _ajax({
  179. global: false,
  180. url: mockHandler.proxy,
  181. type: mockHandler.proxyType,
  182. data: mockHandler.data,
  183. dataType: requestSettings.dataType === "script" ? "text/plain" : requestSettings.dataType,
  184. complete: function(xhr) {
  185. mockHandler.responseXML = xhr.responseXML;
  186. mockHandler.responseText = xhr.responseText;
  187. // Don't override the handler status/statusText if it's specified by the config
  188. if (isDefaultSetting(mockHandler, 'status')) {
  189. mockHandler.status = xhr.status;
  190. }
  191. if (isDefaultSetting(mockHandler, 'statusText')) {
  192. mockHandler.statusText = xhr.statusText;
  193. }
  194. this.responseTimer = setTimeout(process, mockHandler.responseTime || 0);
  195. }
  196. });
  197. } else {
  198. // type == 'POST' || 'GET' || 'DELETE'
  199. if ( requestSettings.async === false ) {
  200. // TODO: Blocking delay
  201. process();
  202. } else {
  203. this.responseTimer = setTimeout(process, mockHandler.responseTime || 50);
  204. }
  205. }
  206. }
  207. // Construct a mocked XHR Object
  208. function xhr(mockHandler, requestSettings, origSettings, origHandler) {
  209. // Extend with our default mockjax settings
  210. mockHandler = $.extend(true, {}, $.mockjaxSettings, mockHandler);
  211. if (typeof mockHandler.headers === 'undefined') {
  212. mockHandler.headers = {};
  213. }
  214. if ( mockHandler.contentType ) {
  215. mockHandler.headers['content-type'] = mockHandler.contentType;
  216. }
  217. return {
  218. status: mockHandler.status,
  219. statusText: mockHandler.statusText,
  220. readyState: 1,
  221. open: function() { },
  222. send: function() {
  223. origHandler.fired = true;
  224. _xhrSend.call(this, mockHandler, requestSettings, origSettings);
  225. },
  226. abort: function() {
  227. clearTimeout(this.responseTimer);
  228. },
  229. setRequestHeader: function(header, value) {
  230. mockHandler.headers[header] = value;
  231. },
  232. getResponseHeader: function(header) {
  233. // 'Last-modified', 'Etag', 'content-type' are all checked by jQuery
  234. if ( mockHandler.headers && mockHandler.headers[header] ) {
  235. // Return arbitrary headers
  236. return mockHandler.headers[header];
  237. } else if ( header.toLowerCase() == 'last-modified' ) {
  238. return mockHandler.lastModified || (new Date()).toString();
  239. } else if ( header.toLowerCase() == 'etag' ) {
  240. return mockHandler.etag || '';
  241. } else if ( header.toLowerCase() == 'content-type' ) {
  242. return mockHandler.contentType || 'text/plain';
  243. }
  244. },
  245. getAllResponseHeaders: function() {
  246. var headers = '';
  247. $.each(mockHandler.headers, function(k, v) {
  248. headers += k + ': ' + v + "\n";
  249. });
  250. return headers;
  251. }
  252. };
  253. }
  254. // Process a JSONP mock request.
  255. function processJsonpMock( requestSettings, mockHandler, origSettings ) {
  256. // Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
  257. // because there isn't an easy hook for the cross domain script tag of jsonp
  258. processJsonpUrl( requestSettings );
  259. requestSettings.dataType = "json";
  260. if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) {
  261. createJsonpCallback(requestSettings, mockHandler, origSettings);
  262. // We need to make sure
  263. // that a JSONP style response is executed properly
  264. var rurl = /^(\w+:)?\/\/([^\/?#]+)/,
  265. parts = rurl.exec( requestSettings.url ),
  266. remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
  267. requestSettings.dataType = "script";
  268. if(requestSettings.type.toUpperCase() === "GET" && remote ) {
  269. var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings );
  270. // Check if we are supposed to return a Deferred back to the mock call, or just
  271. // signal success
  272. if(newMockReturn) {
  273. return newMockReturn;
  274. } else {
  275. return true;
  276. }
  277. }
  278. }
  279. return null;
  280. }
  281. // Append the required callback parameter to the end of the request URL, for a JSONP request
  282. function processJsonpUrl( requestSettings ) {
  283. if ( requestSettings.type.toUpperCase() === "GET" ) {
  284. if ( !CALLBACK_REGEX.test( requestSettings.url ) ) {
  285. requestSettings.url += (/\?/.test( requestSettings.url ) ? "&" : "?") +
  286. (requestSettings.jsonp || "callback") + "=?";
  287. }
  288. } else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) {
  289. requestSettings.data = (requestSettings.data ? requestSettings.data + "&" : "") + (requestSettings.jsonp || "callback") + "=?";
  290. }
  291. }
  292. // Process a JSONP request by evaluating the mocked response text
  293. function processJsonpRequest( requestSettings, mockHandler, origSettings ) {
  294. // Synthesize the mock request for adding a script tag
  295. var callbackContext = origSettings && origSettings.context || requestSettings,
  296. newMock = null;
  297. // If the response handler on the moock is a function, call it
  298. if ( mockHandler.response && $.isFunction(mockHandler.response) ) {
  299. mockHandler.response(origSettings);
  300. } else {
  301. // Evaluate the responseText javascript in a global context
  302. if( typeof mockHandler.responseText === 'object' ) {
  303. $.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')');
  304. } else {
  305. $.globalEval( '(' + mockHandler.responseText + ')');
  306. }
  307. }
  308. // Successful response
  309. jsonpSuccess( requestSettings, callbackContext, mockHandler );
  310. jsonpComplete( requestSettings, callbackContext, mockHandler );
  311. // If we are running under jQuery 1.5+, return a deferred object
  312. if($.Deferred){
  313. newMock = new $.Deferred();
  314. if(typeof mockHandler.responseText == "object"){
  315. newMock.resolveWith( callbackContext, [mockHandler.responseText] );
  316. }
  317. else{
  318. newMock.resolveWith( callbackContext, [$.parseJSON( mockHandler.responseText )] );
  319. }
  320. }
  321. return newMock;
  322. }
  323. // Create the required JSONP callback function for the request
  324. function createJsonpCallback( requestSettings, mockHandler, origSettings ) {
  325. var callbackContext = origSettings && origSettings.context || requestSettings;
  326. var jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++);
  327. // Replace the =? sequence both in the query string and the data
  328. if ( requestSettings.data ) {
  329. requestSettings.data = (requestSettings.data + "").replace(CALLBACK_REGEX, "=" + jsonp + "$1");
  330. }
  331. requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, "=" + jsonp + "$1");
  332. // Handle JSONP-style loading
  333. window[ jsonp ] = window[ jsonp ] || function( tmp ) {
  334. data = tmp;
  335. jsonpSuccess( requestSettings, callbackContext, mockHandler );
  336. jsonpComplete( requestSettings, callbackContext, mockHandler );
  337. // Garbage collect
  338. window[ jsonp ] = undefined;
  339. try {
  340. delete window[ jsonp ];
  341. } catch(e) {}
  342. if ( head ) {
  343. head.removeChild( script );
  344. }
  345. };
  346. }
  347. // The JSONP request was successful
  348. function jsonpSuccess(requestSettings, callbackContext, mockHandler) {
  349. // If a local callback was specified, fire it and pass it the data
  350. if ( requestSettings.success ) {
  351. requestSettings.success.call( callbackContext, mockHandler.responseText || "", status, {} );
  352. }
  353. // Fire the global callback
  354. if ( requestSettings.global ) {
  355. trigger(requestSettings, "ajaxSuccess", [{}, requestSettings] );
  356. }
  357. }
  358. // The JSONP request was completed
  359. function jsonpComplete(requestSettings, callbackContext) {
  360. // Process result
  361. if ( requestSettings.complete ) {
  362. requestSettings.complete.call( callbackContext, {} , status );
  363. }
  364. // The request was completed
  365. if ( requestSettings.global ) {
  366. trigger( "ajaxComplete", [{}, requestSettings] );
  367. }
  368. // Handle the global AJAX counter
  369. if ( requestSettings.global && ! --$.active ) {
  370. $.event.trigger( "ajaxStop" );
  371. }
  372. }
  373. // The core $.ajax replacement.
  374. function handleAjax( url, origSettings ) {
  375. var mockRequest, requestSettings, mockHandler;
  376. // If url is an object, simulate pre-1.5 signature
  377. if ( typeof url === "object" ) {
  378. origSettings = url;
  379. url = undefined;
  380. } else {
  381. // work around to support 1.5 signature
  382. origSettings = origSettings || {};
  383. origSettings.url = url;
  384. }
  385. // Extend the original settings for the request
  386. requestSettings = $.extend(true, {}, $.ajaxSettings, origSettings);
  387. // Iterate over our mock handlers (in registration order) until we find
  388. // one that is willing to intercept the request
  389. for(var k = 0; k < mockHandlers.length; k++) {
  390. if ( !mockHandlers[k] ) {
  391. continue;
  392. }
  393. mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
  394. if(!mockHandler) {
  395. // No valid mock found for this request
  396. continue;
  397. }
  398. mockedAjaxCalls.push(requestSettings);
  399. // If logging is enabled, log the mock to the console
  400. $.mockjaxSettings.log( mockHandler, requestSettings );
  401. if ( requestSettings.dataType && requestSettings.dataType.toUpperCase() === 'JSONP' ) {
  402. if ((mockRequest = processJsonpMock( requestSettings, mockHandler, origSettings ))) {
  403. // This mock will handle the JSONP request
  404. return mockRequest;
  405. }
  406. }
  407. // Removed to fix #54 - keep the mocking data object intact
  408. //mockHandler.data = requestSettings.data;
  409. mockHandler.cache = requestSettings.cache;
  410. mockHandler.timeout = requestSettings.timeout;
  411. mockHandler.global = requestSettings.global;
  412. copyUrlParameters(mockHandler, origSettings);
  413. (function(mockHandler, requestSettings, origSettings, origHandler) {
  414. mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
  415. // Mock the XHR object
  416. xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); }
  417. }));
  418. })(mockHandler, requestSettings, origSettings, mockHandlers[k]);
  419. return mockRequest;
  420. }
  421. // We don't have a mock request
  422. if($.mockjaxSettings.throwUnmocked === true) {
  423. throw('AJAX not mocked: ' + origSettings.url);
  424. }
  425. else { // trigger a normal request
  426. return _ajax.apply($, [origSettings]);
  427. }
  428. }
  429. /**
  430. * Copies URL parameter values if they were captured by a regular expression
  431. * @param {Object} mockHandler
  432. * @param {Object} origSettings
  433. */
  434. function copyUrlParameters(mockHandler, origSettings) {
  435. //parameters aren't captured if the URL isn't a RegExp
  436. if (!(mockHandler.url instanceof RegExp)) {
  437. return;
  438. }
  439. //if no URL params were defined on the handler, don't attempt a capture
  440. if (!mockHandler.hasOwnProperty('urlParams')) {
  441. return;
  442. }
  443. var captures = mockHandler.url.exec(origSettings.url);
  444. //the whole RegExp match is always the first value in the capture results
  445. if (captures.length === 1) {
  446. return;
  447. }
  448. captures.shift();
  449. //use handler params as keys and capture resuts as values
  450. var i = 0,
  451. capturesLength = captures.length,
  452. paramsLength = mockHandler.urlParams.length,
  453. //in case the number of params specified is less than actual captures
  454. maxIterations = Math.min(capturesLength, paramsLength),
  455. paramValues = {};
  456. for (i; i < maxIterations; i++) {
  457. var key = mockHandler.urlParams[i];
  458. paramValues[key] = captures[i];
  459. }
  460. origSettings.urlParams = paramValues;
  461. }
  462. // Public
  463. $.extend({
  464. ajax: handleAjax
  465. });
  466. $.mockjaxSettings = {
  467. //url: null,
  468. //type: 'GET',
  469. log: function( mockHandler, requestSettings ) {
  470. if ( mockHandler.logging === false ||
  471. ( typeof mockHandler.logging === 'undefined' && $.mockjaxSettings.logging === false ) ) {
  472. return;
  473. }
  474. if ( window.console && console.log ) {
  475. var message = 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url;
  476. var request = $.extend({}, requestSettings);
  477. if (typeof console.log === 'function') {
  478. console.log(message, request);
  479. } else {
  480. try {
  481. console.log( message + ' ' + JSON.stringify(request) );
  482. } catch (e) {
  483. console.log(message);
  484. }
  485. }
  486. }
  487. },
  488. logging: true,
  489. status: 200,
  490. statusText: "OK",
  491. responseTime: 500,
  492. isTimeout: false,
  493. throwUnmocked: false,
  494. contentType: 'text/plain',
  495. response: '',
  496. responseText: '',
  497. responseXML: '',
  498. proxy: '',
  499. proxyType: 'GET',
  500. lastModified: null,
  501. etag: '',
  502. headers: {
  503. etag: 'IJF@H#@923uf8023hFO@I#H#',
  504. 'content-type' : 'text/plain'
  505. }
  506. };
  507. $.mockjax = function(settings) {
  508. var i = mockHandlers.length;
  509. mockHandlers[i] = settings;
  510. return i;
  511. };
  512. $.mockjaxClear = function(i) {
  513. if ( arguments.length == 1 ) {
  514. mockHandlers[i] = null;
  515. } else {
  516. mockHandlers = [];
  517. }
  518. mockedAjaxCalls = [];
  519. };
  520. $.mockjax.handler = function(i) {
  521. if ( arguments.length == 1 ) {
  522. return mockHandlers[i];
  523. }
  524. };
  525. $.mockjax.mockedAjaxCalls = function() {
  526. return mockedAjaxCalls;
  527. };
  528. })(jQuery);