From: Stas Vilchik Date: Mon, 6 Jul 2015 14:54:34 +0000 (+0200) Subject: transition coding rules web tests X-Git-Tag: 5.2-RC1~1178 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cc9e9f5e58d6efa4a434f58eee543db3f840a609;p=sonarqube.git transition coding rules web tests --- diff --git a/server/sonar-web/src/main/js/libs/third-party/jquery.mockjax.js b/server/sonar-web/src/main/js/libs/third-party/jquery.mockjax.js old mode 100644 new mode 100755 index fc6327fd1c1..b935290ff58 --- a/server/sonar-web/src/main/js/libs/third-party/jquery.mockjax.js +++ b/server/sonar-web/src/main/js/libs/third-party/jquery.mockjax.js @@ -1,15 +1,51 @@ -(function($) { +/*! jQuery Mockjax + * A Plugin providing simple and flexible mocking of ajax requests and responses + * + * Version: 2.0.1 + * Home: https://github.com/jakerella/jquery-mockjax + * Copyright (c) 2015 Jordan Kasper, formerly appendTo; + * NOTE: This repository was taken over by Jordan Kasper (@jakerella) October, 2014 + * + * Dual licensed under the MIT or GPL licenses. + * http://opensource.org/licenses/MIT OR http://www.gnu.org/licenses/gpl-2.0.html + */ +(function(root, factory) { + 'use strict'; + + // AMDJS module definition + if ( typeof define === 'function' && define.amd && define.amd.jQuery ) { + define(['jquery'], function($) { + return factory($, root); + }); + + // CommonJS module definition + } else if ( typeof exports === 'object') { + + // NOTE: To use Mockjax as a Node module you MUST provide the factory with + // a valid version of jQuery and a window object (the global scope): + // var mockjax = require('jquery.mockjax')(jQuery, window); + + module.exports = factory; + + // Global jQuery in web browsers + } else { + return factory(root.jQuery || root.$, root); + } +}(this, function($, window) { + 'use strict'; + var _ajax = $.ajax, mockHandlers = [], mockedAjaxCalls = [], + unmockedAjaxCalls = [], CALLBACK_REGEX = /=\?(&|$)/, jsc = (new Date()).getTime(); // Parse the given XML string. function parseXML(xml) { - if ( window.DOMParser == undefined && window.ActiveXObject ) { - DOMParser = function() { }; + if ( window.DOMParser === undefined && window.ActiveXObject ) { + window.DOMParser = function() { }; DOMParser.prototype.parseFromString = function( xmlString ) { var doc = new ActiveXObject('Microsoft.XMLDOM'); doc.async = 'false'; @@ -22,25 +58,20 @@ var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' ); if ( $.isXMLDoc( xmlDoc ) ) { var err = $('parsererror', xmlDoc); - if ( err.length == 1 ) { - throw('Error: ' + $(xmlDoc).text() ); + if ( err.length === 1 ) { + throw new Error('Error: ' + $(xmlDoc).text() ); } } else { - throw('Unable to parse XML'); + throw new Error('Unable to parse XML'); } return xmlDoc; } catch( e ) { - var msg = ( e.name == undefined ? e : e.name + ': ' + e.message ); + var msg = ( e.name === undefined ? e : e.name + ': ' + e.message ); $(document).trigger('xmlParseError', [ msg ]); return undefined; } } - // Trigger a jQuery event - function trigger(s, type, args) { - (s.context ? $(s.context) : $.event).trigger(type, args); - } - // Check if the data field on the mock handler and the request match. This // can be used to restrict a mock handler to being used only when a certain // set of data is passed to it. @@ -49,21 +80,23 @@ // Test for situations where the data is a querystring (not an object) if (typeof live === 'string') { // Querystring may be a regex - return $.isFunction( mock.test ) ? mock.test(live) : mock == live; + return $.isFunction( mock.test ) ? mock.test(live) : mock === live; } $.each(mock, function(k) { if ( live[k] === undefined ) { identical = false; return identical; } else { - // This will allow to compare Arrays if ( typeof live[k] === 'object' && live[k] !== null ) { + if ( identical && $.isArray( live[k] ) ) { + identical = $.isArray( mock[k] ) && live[k].length === mock[k].length; + } identical = identical && isMockDataEqual(mock[k], live[k]); } else { if ( mock[k] && $.isFunction( mock[k].test ) ) { identical = identical && mock[k].test(live[k]); } else { - identical = identical && ( mock[k] == live[k] ); + identical = identical && ( mock[k] === live[k] ); } } } @@ -72,10 +105,10 @@ return identical; } - // See if a mock handler property matches the default settings - function isDefaultSetting(handler, property) { - return handler[property] === $.mockjaxSettings[property]; - } + // See if a mock handler property matches the default settings + function isDefaultSetting(handler, property) { + return handler[property] === $.mockjaxSettings[property]; + } // Check the given handler should mock the given request function getMockForRequest( handler, requestSettings ) { @@ -96,21 +129,42 @@ // Look for a simple wildcard '*' or a direct URL match var star = handler.url.indexOf('*'); if (handler.url !== requestSettings.url && star === -1 || - !new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\$&").replace(/\*/g, '.+')).test(requestSettings.url)) { + !new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, '\\$&').replace(/\*/g, '.+')).test(requestSettings.url)) { return null; } } + // Inspect the request headers submitted + if ( handler.requestHeaders ) { + //No expectation for headers, do not mock this request + if (requestSettings.headers === undefined) { + return null; + } else { + var headersMismatch = false; + $.each(handler.requestHeaders, function(key, value) { + var v = requestSettings.headers[key]; + if(v !== value) { + headersMismatch = true; + return false; + } + }); + //Headers do not match, do not mock this request + if (headersMismatch) { + return null; + } + } + } + // Inspect the data submitted in the request (either POST body or GET query string) - if ( handler.data && requestSettings.data ) { - if ( !isMockDataEqual(handler.data, requestSettings.data) ) { + if ( handler.data ) { + if ( ! requestSettings.data || !isMockDataEqual(handler.data, requestSettings.data) ) { // They're not identical, do not mock this request return null; } } // Inspect the request type if ( handler && handler.type && - handler.type.toLowerCase() != requestSettings.type.toLowerCase() ) { + handler.type.toLowerCase() !== requestSettings.type.toLowerCase() ) { // The request type doesn't match (GET vs. POST) return null; } @@ -118,6 +172,23 @@ return handler; } + function isPosNum(value) { + return typeof value === 'number' && value >= 0; + } + + function parseResponseTimeOpt(responseTime) { + if ($.isArray(responseTime) && responseTime.length === 2) { + var min = responseTime[0]; + var max = responseTime[1]; + if(isPosNum(min) && isPosNum(max)) { + return Math.floor(Math.random() * (max - min)) + min; + } + } else if(isPosNum(responseTime)) { + return responseTime; + } + return DEFAULT_RESPONSE_TIME; + } + // Process the xhr objects send operation function _xhrSend(mockHandler, requestSettings, origSettings) { @@ -125,52 +196,70 @@ var process = (function(that) { return function() { return (function() { - var onReady; - // The request has returned - this.status = mockHandler.status; + this.status = mockHandler.status; this.statusText = mockHandler.statusText; - this.readyState = 4; + this.readyState = 1; + + var finishRequest = function () { + this.readyState = 4; + + var onReady; + // Copy over our mock to our xhr object before passing control back to + // jQuery's onreadystatechange callback + if ( requestSettings.dataType === 'json' && ( typeof mockHandler.responseText === 'object' ) ) { + this.responseText = JSON.stringify(mockHandler.responseText); + } else if ( requestSettings.dataType === 'xml' ) { + if ( typeof mockHandler.responseXML === 'string' ) { + this.responseXML = parseXML(mockHandler.responseXML); + //in jQuery 1.9.1+, responseXML is processed differently and relies on responseText + this.responseText = mockHandler.responseXML; + } else { + this.responseXML = mockHandler.responseXML; + } + } else if (typeof mockHandler.responseText === 'object' && mockHandler.responseText !== null) { + // since jQuery 1.9 responseText type has to match contentType + mockHandler.contentType = 'application/json'; + this.responseText = JSON.stringify(mockHandler.responseText); + } else { + this.responseText = mockHandler.responseText; + } + if( typeof mockHandler.status === 'number' || typeof mockHandler.status === 'string' ) { + this.status = mockHandler.status; + } + if( typeof mockHandler.statusText === 'string') { + this.statusText = mockHandler.statusText; + } + // jQuery 2.0 renamed onreadystatechange to onload + onReady = this.onreadystatechange || this.onload; + + // jQuery < 1.4 doesn't have onreadystate change for xhr + if ( $.isFunction( onReady ) ) { + if( mockHandler.isTimeout) { + this.status = -1; + } + onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined ); + } else if ( mockHandler.isTimeout ) { + // Fix for 1.3.2 timeout to keep success from firing. + this.status = -1; + } + }; // We have an executable function, call it to give // the mock handler a chance to update it's data if ( $.isFunction(mockHandler.response) ) { - mockHandler.response(origSettings); - } - // Copy over our mock to our xhr object before passing control back to - // jQuery's onreadystatechange callback - if ( requestSettings.dataType == 'json' && ( typeof mockHandler.responseText == 'object' ) ) { - this.responseText = JSON.stringify(mockHandler.responseText); - } else if ( requestSettings.dataType == 'xml' ) { - if ( typeof mockHandler.responseXML == 'string' ) { - this.responseXML = parseXML(mockHandler.responseXML); - //in jQuery 1.9.1+, responseXML is processed differently and relies on responseText - this.responseText = mockHandler.responseXML; + // Wait for it to finish + if ( mockHandler.response.length === 2 ) { + mockHandler.response(origSettings, function () { + finishRequest.call(that); + }); + return; } else { - this.responseXML = mockHandler.responseXML; + mockHandler.response(origSettings); } - } else { - this.responseText = mockHandler.responseText; - } - if( typeof mockHandler.status == 'number' || typeof mockHandler.status == 'string' ) { - this.status = mockHandler.status; - } - if( typeof mockHandler.statusText === "string") { - this.statusText = mockHandler.statusText; } - // jQuery 2.0 renamed onreadystatechange to onload - onReady = this.onreadystatechange || this.onload; - // jQuery < 1.4 doesn't have onreadystate change for xhr - if ( $.isFunction( onReady ) ) { - if( mockHandler.isTimeout) { - this.status = -1; - } - onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined ); - } else if ( mockHandler.isTimeout ) { - // Fix for 1.3.2 timeout to keep success from firing. - this.status = -1; - } + finishRequest.call(that); }).apply(that); }; })(this); @@ -182,28 +271,27 @@ url: mockHandler.proxy, type: mockHandler.proxyType, data: mockHandler.data, - dataType: requestSettings.dataType === "script" ? "text/plain" : requestSettings.dataType, + dataType: requestSettings.dataType === 'script' ? 'text/plain' : requestSettings.dataType, complete: function(xhr) { mockHandler.responseXML = xhr.responseXML; mockHandler.responseText = xhr.responseText; - // Don't override the handler status/statusText if it's specified by the config - if (isDefaultSetting(mockHandler, 'status')) { - mockHandler.status = xhr.status; - } - if (isDefaultSetting(mockHandler, 'statusText')) { - mockHandler.statusText = xhr.statusText; - } - - this.responseTimer = setTimeout(process, mockHandler.responseTime || 0); + // Don't override the handler status/statusText if it's specified by the config + if (isDefaultSetting(mockHandler, 'status')) { + mockHandler.status = xhr.status; + } + if (isDefaultSetting(mockHandler, 'statusText')) { + mockHandler.statusText = xhr.statusText; + } + this.responseTimer = setTimeout(process, parseResponseTimeOpt(mockHandler.responseTime)); } }); } else { - // type == 'POST' || 'GET' || 'DELETE' + // type === 'POST' || 'GET' || 'DELETE' if ( requestSettings.async === false ) { // TODO: Blocking delay process(); } else { - this.responseTimer = setTimeout(process, mockHandler.responseTime || 50); + this.responseTimer = setTimeout(process, parseResponseTimeOpt(mockHandler.responseTime)); } } } @@ -216,6 +304,9 @@ if (typeof mockHandler.headers === 'undefined') { mockHandler.headers = {}; } + if (typeof requestSettings.headers === 'undefined') { + requestSettings.headers = {}; + } if ( mockHandler.contentType ) { mockHandler.headers['content-type'] = mockHandler.contentType; } @@ -233,25 +324,29 @@ clearTimeout(this.responseTimer); }, setRequestHeader: function(header, value) { - mockHandler.headers[header] = value; + requestSettings.headers[header] = value; }, getResponseHeader: function(header) { // 'Last-modified', 'Etag', 'content-type' are all checked by jQuery if ( mockHandler.headers && mockHandler.headers[header] ) { // Return arbitrary headers return mockHandler.headers[header]; - } else if ( header.toLowerCase() == 'last-modified' ) { + } else if ( header.toLowerCase() === 'last-modified' ) { return mockHandler.lastModified || (new Date()).toString(); - } else if ( header.toLowerCase() == 'etag' ) { + } else if ( header.toLowerCase() === 'etag' ) { return mockHandler.etag || ''; - } else if ( header.toLowerCase() == 'content-type' ) { + } else if ( header.toLowerCase() === 'content-type' ) { return mockHandler.contentType || 'text/plain'; } }, getAllResponseHeaders: function() { var headers = ''; + // since jQuery 1.9 responseText type has to match contentType + if (mockHandler.contentType) { + mockHandler.headers['Content-Type'] = mockHandler.contentType; + } $.each(mockHandler.headers, function(k, v) { - headers += k + ': ' + v + "\n"; + headers += k + ': ' + v + '\n'; }); return headers; } @@ -265,7 +360,7 @@ processJsonpUrl( requestSettings ); - requestSettings.dataType = "json"; + requestSettings.dataType = 'json'; if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) { createJsonpCallback(requestSettings, mockHandler, origSettings); @@ -276,8 +371,8 @@ parts = rurl.exec( requestSettings.url ), remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host); - requestSettings.dataType = "script"; - if(requestSettings.type.toUpperCase() === "GET" && remote ) { + requestSettings.dataType = 'script'; + if(requestSettings.type.toUpperCase() === 'GET' && remote ) { var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings ); // Check if we are supposed to return a Deferred back to the mock call, or just @@ -294,13 +389,13 @@ // Append the required callback parameter to the end of the request URL, for a JSONP request function processJsonpUrl( requestSettings ) { - if ( requestSettings.type.toUpperCase() === "GET" ) { + if ( requestSettings.type.toUpperCase() === 'GET' ) { if ( !CALLBACK_REGEX.test( requestSettings.url ) ) { - requestSettings.url += (/\?/.test( requestSettings.url ) ? "&" : "?") + - (requestSettings.jsonp || "callback") + "=?"; + requestSettings.url += (/\?/.test( requestSettings.url ) ? '&' : '?') + + (requestSettings.jsonp || 'callback') + '=?'; } } else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) { - requestSettings.data = (requestSettings.data ? requestSettings.data + "&" : "") + (requestSettings.jsonp || "callback") + "=?"; + requestSettings.data = (requestSettings.data ? requestSettings.data + '&' : '') + (requestSettings.jsonp || 'callback') + '=?'; } } @@ -308,58 +403,82 @@ function processJsonpRequest( requestSettings, mockHandler, origSettings ) { // Synthesize the mock request for adding a script tag var callbackContext = origSettings && origSettings.context || requestSettings, - newMock = null; - + // If we are running under jQuery 1.5+, return a deferred object + newMock = ($.Deferred) ? (new $.Deferred()) : null; // If the response handler on the moock is a function, call it if ( mockHandler.response && $.isFunction(mockHandler.response) ) { + mockHandler.response(origSettings); - } else { - + + + } else if ( typeof mockHandler.responseText === 'object' ) { // Evaluate the responseText javascript in a global context - if( typeof mockHandler.responseText === 'object' ) { - $.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')'); - } else { - $.globalEval( '(' + mockHandler.responseText + ')'); - } + $.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')'); + + } else if (mockHandler.proxy) { + + // This handles the unique case where we have a remote URL, but want to proxy the JSONP + // response to another file (not the same URL as the mock matching) + _ajax({ + global: false, + url: mockHandler.proxy, + type: mockHandler.proxyType, + data: mockHandler.data, + dataType: requestSettings.dataType === 'script' ? 'text/plain' : requestSettings.dataType, + complete: function(xhr) { + $.globalEval( '(' + xhr.responseText + ')'); + completeJsonpCall( requestSettings, mockHandler, callbackContext, newMock ); + } + }); + + return newMock; + + } else { + $.globalEval( '(' + mockHandler.responseText + ')'); } + completeJsonpCall( requestSettings, mockHandler, callbackContext, newMock ); + + return newMock; + } + + function completeJsonpCall( requestSettings, mockHandler, callbackContext, newMock ) { + var json; + // Successful response - jsonpSuccess( requestSettings, callbackContext, mockHandler ); - jsonpComplete( requestSettings, callbackContext, mockHandler ); - - // If we are running under jQuery 1.5+, return a deferred object - if($.Deferred){ - newMock = new $.Deferred(); - if(typeof mockHandler.responseText == "object"){ - newMock.resolveWith( callbackContext, [mockHandler.responseText] ); - } - else{ - newMock.resolveWith( callbackContext, [$.parseJSON( mockHandler.responseText )] ); - } + setTimeout(function() { + jsonpSuccess( requestSettings, callbackContext, mockHandler ); + jsonpComplete( requestSettings, callbackContext ); + }, parseResponseTimeOpt( mockHandler.responseTime )); + + if ( newMock ) { + try { + json = $.parseJSON( mockHandler.responseText ); + } catch (err) { /* just checking... */ } + + newMock.resolveWith( callbackContext, [json || mockHandler.responseText] ); } - return newMock; } // Create the required JSONP callback function for the request function createJsonpCallback( requestSettings, mockHandler, origSettings ) { var callbackContext = origSettings && origSettings.context || requestSettings; - var jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++); + var jsonp = requestSettings.jsonpCallback || ('jsonp' + jsc++); // Replace the =? sequence both in the query string and the data if ( requestSettings.data ) { - requestSettings.data = (requestSettings.data + "").replace(CALLBACK_REGEX, "=" + jsonp + "$1"); + requestSettings.data = (requestSettings.data + '').replace(CALLBACK_REGEX, '=' + jsonp + '$1'); } - requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, "=" + jsonp + "$1"); + requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, '=' + jsonp + '$1'); // Handle JSONP-style loading - window[ jsonp ] = window[ jsonp ] || function( tmp ) { - data = tmp; + window[ jsonp ] = window[ jsonp ] || function() { jsonpSuccess( requestSettings, callbackContext, mockHandler ); - jsonpComplete( requestSettings, callbackContext, mockHandler ); + jsonpComplete( requestSettings, callbackContext ); // Garbage collect window[ jsonp ] = undefined; @@ -367,9 +486,6 @@ delete window[ jsonp ]; } catch(e) {} - if ( head ) { - head.removeChild( script ); - } }; } @@ -377,49 +493,65 @@ function jsonpSuccess(requestSettings, callbackContext, mockHandler) { // If a local callback was specified, fire it and pass it the data if ( requestSettings.success ) { - requestSettings.success.call( callbackContext, mockHandler.responseText || "", status, {} ); + requestSettings.success.call( callbackContext, mockHandler.responseText || '', 'success', {} ); } // Fire the global callback if ( requestSettings.global ) { - trigger(requestSettings, "ajaxSuccess", [{}, requestSettings] ); + (requestSettings.context ? $(requestSettings.context) : $.event).trigger('ajaxSuccess', [{}, requestSettings]); } } // The JSONP request was completed function jsonpComplete(requestSettings, callbackContext) { - // Process result if ( requestSettings.complete ) { - requestSettings.complete.call( callbackContext, {} , status ); + requestSettings.complete.call( callbackContext, { + statusText: 'success', + status: 200 + } , 'success' ); } // The request was completed if ( requestSettings.global ) { - trigger( "ajaxComplete", [{}, requestSettings] ); + (requestSettings.context ? $(requestSettings.context) : $.event).trigger('ajaxComplete', [{}, requestSettings]); } // Handle the global AJAX counter if ( requestSettings.global && ! --$.active ) { - $.event.trigger( "ajaxStop" ); + $.event.trigger( 'ajaxStop' ); } } // The core $.ajax replacement. function handleAjax( url, origSettings ) { - var mockRequest, requestSettings, mockHandler; + var mockRequest, requestSettings, mockHandler, overrideCallback; // If url is an object, simulate pre-1.5 signature - if ( typeof url === "object" ) { + if ( typeof url === 'object' ) { origSettings = url; url = undefined; } else { // work around to support 1.5 signature + origSettings = origSettings || {}; origSettings.url = url; } // Extend the original settings for the request - requestSettings = $.extend(true, {}, $.ajaxSettings, origSettings); + requestSettings = $.ajaxSetup({}, origSettings); + requestSettings.type = requestSettings.method = requestSettings.method || requestSettings.type; + + // Generic function to override callback methods for use with + // callback options (onAfterSuccess, onAfterError, onAfterComplete) + overrideCallback = function(action, mockHandler) { + var origHandler = origSettings[action.toLowerCase()]; + return function() { + if ( $.isFunction(origHandler) ) { + origHandler.apply(this, [].slice.call(arguments)); + } + mockHandler['onAfter' + action](); + }; + }; // Iterate over our mock handlers (in registration order) until we find // one that is willing to intercept the request @@ -440,7 +572,7 @@ $.mockjaxSettings.log( mockHandler, requestSettings ); - if ( requestSettings.dataType === "jsonp" ) { + if ( requestSettings.dataType && requestSettings.dataType.toUpperCase() === 'JSONP' ) { if ((mockRequest = processJsonpMock( requestSettings, mockHandler, origSettings ))) { // This mock will handle the JSONP request return mockRequest; @@ -455,21 +587,48 @@ mockHandler.timeout = requestSettings.timeout; mockHandler.global = requestSettings.global; + // In the case of a timeout, we just need to ensure + // an actual jQuery timeout (That is, our reponse won't) + // return faster than the timeout setting. + if ( mockHandler.isTimeout ) { + if ( mockHandler.responseTime > 1 ) { + origSettings.timeout = mockHandler.responseTime - 1; + } else { + mockHandler.responseTime = 2; + origSettings.timeout = 1; + } + } + + // Set up onAfter[X] callback functions + if ( $.isFunction( mockHandler.onAfterSuccess ) ) { + origSettings.success = overrideCallback('Success', mockHandler); + } + if ( $.isFunction( mockHandler.onAfterError ) ) { + origSettings.error = overrideCallback('Error', mockHandler); + } + if ( $.isFunction( mockHandler.onAfterComplete ) ) { + origSettings.complete = overrideCallback('Complete', mockHandler); + } + copyUrlParameters(mockHandler, origSettings); + /* jshint loopfunc:true */ (function(mockHandler, requestSettings, origSettings, origHandler) { + mockRequest = _ajax.call($, $.extend(true, {}, origSettings, { // Mock the XHR object xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); } })); })(mockHandler, requestSettings, origSettings, mockHandlers[k]); + /* jshint loopfunc:true */ return mockRequest; } // We don't have a mock request + unmockedAjaxCalls.push(origSettings); if($.mockjaxSettings.throwUnmocked === true) { - throw('AJAX not mocked: ' + origSettings.url); + throw new Error('AJAX not mocked: ' + origSettings.url); } else { // trigger a normal request return _ajax.apply($, [origSettings]); @@ -517,20 +676,23 @@ ajax: handleAjax }); + var DEFAULT_RESPONSE_TIME = 500; + + $.mockjaxSettings = { - //url: null, - //type: 'GET', - log: function( mockHandler, requestSettings ) { + //url: null, + //type: 'GET', + log: function( mockHandler, requestSettings ) { if ( mockHandler.logging === false || ( typeof mockHandler.logging === 'undefined' && $.mockjaxSettings.logging === false ) ) { return; } if ( window.console && console.log ) { var message = 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url; - var request = $.extend({}, requestSettings); + var request = $.ajaxSetup({}, requestSettings); if (typeof console.log === 'function') { - console.log(message, JSON.stringify(request.data)); + console.log(message, request); } else { try { console.log( message + ' ' + JSON.stringify(request) ); @@ -542,8 +704,8 @@ }, logging: true, status: 200, - statusText: "OK", - responseTime: 500, + statusText: 'OK', + responseTime: DEFAULT_RESPONSE_TIME, isTimeout: false, throwUnmocked: false, contentType: 'text/plain', @@ -566,20 +728,37 @@ mockHandlers[i] = settings; return i; }; - $.mockjaxClear = function(i) { - if ( arguments.length == 1 ) { + $.mockjax.clear = function(i) { + if ( i || i === 0 ) { mockHandlers[i] = null; } else { mockHandlers = []; } mockedAjaxCalls = []; + unmockedAjaxCalls = []; }; $.mockjax.handler = function(i) { - if ( arguments.length == 1 ) { + if ( arguments.length === 1 ) { return mockHandlers[i]; } }; $.mockjax.mockedAjaxCalls = function() { return mockedAjaxCalls; }; -})(jQuery); + $.mockjax.unfiredHandlers = function() { + var results = []; + for (var i=0, len=mockHandlers.length; i - +