jquery-mockjax
Advanced tools
Comparing version 2.6.1 to 2.7.0-beta.0
@@ -0,1 +1,7 @@ | ||
## 2024-09-09 v2.7.0-beta.0 | ||
* Support for jQuery 4.0.0-beta | ||
* Removed deprecated calls to $.isArray() and $.isFunction() | ||
* Updated cross-origin tests to use a safe domain | ||
* Thanks to @jayaddison for the nudge and PR! | ||
## 2024-02-17 v2.6.1 | ||
@@ -2,0 +8,0 @@ * Changed license from dual (MIT & GPL) to only be MIT |
@@ -158,3 +158,3 @@ # Contributing to Mockjax # | ||
1. Make sure to generate fresh dist files if necessary and commit those. | ||
1. Submit a PR for the branch, this will initiate the Travis CI checks. | ||
1. Submit a PR for the branch, fill out all of the necessary details. | ||
1. Ask others for input on the PR (mostly testing in their own browsers). | ||
@@ -161,0 +161,0 @@ 1. *If all is well*, merge the branch into `master` |
/*! jQuery Mockjax | ||
* A Plugin providing simple and flexible mocking of ajax requests and responses | ||
* | ||
* Version: 2.6.0 | ||
* Version: 2.7.0-beta.0 | ||
* Home: https://github.com/jakerella/jquery-mockjax | ||
@@ -81,3 +81,3 @@ * Copyright (c) 2024 Jordan Kasper, formerly appendTo; | ||
if ( $.isFunction(mock) ) { | ||
if (typeof mock === 'function') { | ||
return !!mock(live); | ||
@@ -89,3 +89,3 @@ } | ||
// Querystring may be a regex | ||
if ($.isFunction( mock.test )) { | ||
if (typeof mock.test === 'function') { | ||
return mock.test(live); | ||
@@ -105,8 +105,8 @@ } else if (typeof mock === 'object') { | ||
if ( typeof live[k] === 'object' && live[k] !== null ) { | ||
if ( identical && $.isArray( live[k] ) ) { | ||
identical = $.isArray( mock[k] ) && live[k].length === mock[k].length; | ||
if ( identical && Array.isArray( live[k] ) ) { | ||
identical = Array.isArray( mock[k] ) && live[k].length === mock[k].length; | ||
} | ||
identical = identical && isMockDataEqual(mock[k], live[k]); | ||
} else { | ||
if ( mock[k] && $.isFunction( mock[k].test ) ) { | ||
if ( mock[k] && typeof mock[k].test === 'function') { | ||
identical = identical && mock[k].test(live[k]); | ||
@@ -166,3 +166,3 @@ } else { | ||
// want to mock this request | ||
if ( $.isFunction(handler) ) { | ||
if (typeof handler === 'function') { | ||
return handler( requestSettings ); | ||
@@ -176,3 +176,3 @@ } | ||
// matches the url for this ajax request | ||
if ( $.isFunction(handler.url.test) ) { | ||
if (typeof handler.url.test === 'function') { | ||
// namespace exists prepend handler.url with namespace | ||
@@ -251,3 +251,3 @@ if (!!namespace) { | ||
function parseResponseTimeOpt(responseTime) { | ||
if ($.isArray(responseTime) && responseTime.length === 2) { | ||
if (Array.isArray(responseTime) && responseTime.length === 2) { | ||
var min = responseTime[0]; | ||
@@ -301,3 +301,3 @@ var max = responseTime[1]; | ||
if ($.isArray(mockHandler.status)) { | ||
if (Array.isArray(mockHandler.status)) { | ||
var idxStatus = Math.floor(Math.random() * mockHandler.status.length); | ||
@@ -316,3 +316,3 @@ this.status = mockHandler.status[idxStatus]; | ||
// jQuery < 1.4 doesn't have onreadystate change for xhr | ||
if ( $.isFunction( onReady ) ) { | ||
if (typeof onReady === 'function') { | ||
if( mockHandler.isTimeout) { | ||
@@ -330,3 +330,3 @@ this.status = -1; | ||
// the mock handler a chance to update it's data | ||
if ( $.isFunction(mockHandler.response) ) { | ||
if (typeof mockHandler.response === 'function') { | ||
// Wait for it to finish | ||
@@ -507,3 +507,3 @@ if ( mockHandler.response.length === 2 ) { | ||
// If the response handler on the moock is a function, call it | ||
if ( mockHandler.response && $.isFunction(mockHandler.response) ) { | ||
if ( mockHandler.response && typeof mockHandler.response === 'function' ) { | ||
@@ -558,3 +558,3 @@ mockHandler.response(origSettings); | ||
try { | ||
json = $.parseJSON( mockHandler.responseText ); | ||
json = JSON.parse( mockHandler.responseText ); | ||
} catch (err) { /* just checking... */ } | ||
@@ -655,3 +655,3 @@ | ||
return function() { | ||
if ( $.isFunction(origHandler) ) { | ||
if (typeof origHandler === 'function') { | ||
origHandler.apply(this, [].slice.call(arguments)); | ||
@@ -740,9 +740,9 @@ } | ||
// Set up onAfter[X] callback functions | ||
if ( $.isFunction( mockHandler.onAfterSuccess ) ) { | ||
if (typeof mockHandler.onAfterSuccess === 'function') { | ||
origSettings.success = overrideCallback('Success', mockHandler); | ||
} | ||
if ( $.isFunction( mockHandler.onAfterError ) ) { | ||
if (typeof mockHandler.onAfterError === 'function') { | ||
origSettings.error = overrideCallback('Error', mockHandler); | ||
} | ||
if ( $.isFunction( mockHandler.onAfterComplete ) ) { | ||
if (typeof mockHandler.onAfterComplete === 'function') { | ||
origSettings.complete = overrideCallback('Complete', mockHandler); | ||
@@ -951,3 +951,3 @@ } | ||
// Multiple mocks. | ||
if ( $.isArray(settings) ) { | ||
if (Array.isArray(settings)) { | ||
return $.map(settings, function(s) { | ||
@@ -954,0 +954,0 @@ return $.mockjax(s); |
@@ -1,1 +0,1 @@ | ||
!function(t,r){"use strict";"function"==typeof define&&define.amd&&define.amd.jQuery?define(["jquery"],function(e){return r(e,t)}):"object"==typeof exports?module.exports=r:r(t.jQuery||t.$,t)}(this,function(v,u){"use strict";var k=v.ajax,b=[],S=[],C=[],l=/=\?(&|$)/,c=(new Date).getTime(),n=500;function M(t,r){L.debug(t,["Checking mock data against request data",t,r]);var n=!0;if(v.isFunction(t))return!!t(r);if("string"==typeof r){if(v.isFunction(t.test))return t.test(r);if("object"!=typeof t)return t===r;r=function(e){var t,r,n,o,s={},a=String(e).split(/&/);for(t=0,r=a.length;t<r;++t){n=a[t];try{n=(n=decodeURIComponent(n.replace(/\+/g," "))).split(/=/)}catch(e){continue}s[n[0]]?(s[n[0]].splice||(o=s[n[0]],s[n[0]]=[],s[n[0]].push(o)),s[n[0]].push(n[1])):s[n[0]]=n[1]}return L.debug(null,["Getting query params from string",e,s]),s}(r)}return v.each(t,function(e){if(void 0===r[e])return n=!1;n="object"==typeof r[e]&&null!==r[e]?(n=n&&v.isArray(r[e])?v.isArray(t[e])&&r[e].length===t[e].length:n)&&M(t[e],r[e]):t[e]&&v.isFunction(t[e].test)?n&&t[e].test(r[e]):n&&t[e]===r[e]}),n}function a(e,t){return e[t]===v.mockjaxSettings[t]}function o(e){return"number"==typeof e&&0<=e}function i(e){if(v.isArray(e)&&2===e.length){var t=e[0],r=e[1];if(o(t)&&o(r))return Math.floor(Math.random()*(r-t))+t}else if(o(e))return e;return n}function O(t,r,n){L.debug(t,["Sending fake XHR request",t,r,n]);o=this;var o,s=function(){return function(){this.status=t.status,this.statusText=t.statusText,this.readyState=1;function e(){var e;this.readyState=4,"json"===r.dataType&&"object"==typeof t.responseText?this.responseText=JSON.stringify(t.responseText):"xml"===r.dataType?"string"==typeof t.responseXML?(this.responseXML=function(t){void 0===u.DOMParser&&u.ActiveXObject&&(u.DOMParser=function(){},DOMParser.prototype.parseFromString=function(e){var t=new ActiveXObject("Microsoft.XMLDOM");return t.async="false",t.loadXML(e),t});try{var e=(new DOMParser).parseFromString(t,"text/xml");if(!v.isXMLDoc(e))throw new Error("Unable to parse XML");if(1===v("parsererror",e).length)throw new Error("Error: "+v(e).text());return e}catch(e){t=void 0===e.name?e:e.name+": "+e.message;v(document).trigger("xmlParseError",[t])}}(t.responseXML),this.responseText=t.responseXML):this.responseXML=t.responseXML:"object"==typeof t.responseText&&null!==t.responseText?(t.contentType="application/json",this.responseText=JSON.stringify(t.responseText)):this.responseText=t.responseText,v.isArray(t.status)?(e=Math.floor(Math.random()*t.status.length),this.status=t.status[e]):"number"!=typeof t.status&&"string"!=typeof t.status||(this.status=t.status),"string"==typeof t.statusText&&(this.statusText=t.statusText),e=this.onload||this.onreadystatechange,v.isFunction(e)?(t.isTimeout&&(this.status=-1),e.call(this,t.isTimeout?"timeout":void 0)):t.isTimeout&&(this.status=-1)}if(v.isFunction(t.response)){if(2===t.response.length)return void t.response(n,function(){e.call(o)});t.response(n)}e.call(o)}.apply(o)};t.proxy?(L.info(t,["Retrieving proxy file: "+t.proxy,t]),k({global:!1,url:t.proxy,type:t.proxyType,data:t.data,async:r.async,dataType:"script"===r.dataType?"text/plain":r.dataType,complete:function(e){t.responseXML=t.responseText=e.responseText,a(t,"status")&&(t.status=e.status),a(t,"statusText")&&(t.statusText=e.statusText),!1===r.async?s():this.responseTimer=setTimeout(s,i(t.responseTime))}})):!1===r.async?s():this.responseTimer=setTimeout(s,i(t.responseTime))}function w(e,t,r){if("GET"===(s=e).type.toUpperCase()?l.test(s.url)||(s.url+=(/\?/.test(s.url)?"&":"?")+(s.jsonp||"callback")+"=?"):s.data&&l.test(s.data)||(s.data=(s.data?s.data+"&":"")+(s.jsonp||"callback")+"=?"),e.dataType="json",e.data&&l.test(e.data)||l.test(e.url)){var n=e,o=t,s=r,a=s&&s.context||n,i="string"==typeof n.jsonpCallback&&n.jsonpCallback||"jsonp"+c++,s=(n.data&&(n.data=(n.data+"").replace(l,"="+i+"$1")),n.url=n.url.replace(l,"="+i+"$1"),u[i]=u[i]||function(){f(n,a,o),g(n,a),u[i]=void 0;try{delete u[i]}catch(e){}},n.jsonpCallback=i,/^(\w+:)?\/\/([^\/?#]+)/.exec(e.url)),s=s&&(s[1]&&s[1]!==location.protocol||s[2]!==location.host);if(e.dataType="script","GET"===e.type.toUpperCase()&&s)return function(t,r,e){L.debug(r,["Performing JSONP request",r,t,e]);var n=e&&e.context||t,o=v.Deferred?new v.Deferred:null;if(r.response&&v.isFunction(r.response))r.response(e);else if("object"==typeof r.responseText)v.globalEval("("+JSON.stringify(r.responseText)+")");else{if(r.proxy)return L.info(r,["Performing JSONP proxy request: "+r.proxy,r]),k({global:!1,url:r.proxy,type:r.proxyType,data:r.data,dataType:"script"===t.dataType?"text/plain":t.dataType,complete:function(e){v.globalEval("("+e.responseText+")"),p(t,r,n,o)}}),o;v.globalEval("("+("string"==typeof r.responseText?'"'+r.responseText+'"':r.responseText)+")")}return p(t,r,n,o),o}(e,t,r)||!0}return null}function p(e,t,r,n){var o;setTimeout(function(){if(f(e,r,t),g(e,r),n){try{o=v.parseJSON(t.responseText)}catch(e){}n.resolveWith(r,[o||t.responseText]),L.log(t,["JSONP mock call complete",t,n])}},i(t.responseTime))}function f(e,t,r){e.success&&e.success.call(t,r.responseText||"","success",{}),e.global&&(e.context?v(e.context):v.event).trigger("ajaxSuccess",[{},e])}function g(e,t){e.complete&&e.complete.call(t,{statusText:"success",status:200},"success"),e.global&&(e.context?v(e.context):v.event).trigger("ajaxComplete",[{},e]),e.global&&!--v.active&&v.event.trigger("ajaxStop")}v.extend({ajax:function e(t,n){var r,o,s;L.debug(null,["Ajax call intercepted",t,n]),"object"==typeof t?(n=t,t=void 0):(n=n||{}).url=t||n.url,(o=v.ajaxSetup({},n)).type=o.method=o.method||o.type;for(var a=function(e,t){var r=n[e.toLowerCase()];return function(){v.isFunction(r)&&r.apply(this,[].slice.call(arguments)),t["onAfter"+e]()}},i=0;i<b.length;i++){var u=v.mockjaxSettings.matchInRegistrationOrder?i:b.length-1-i;if(u=b[u]){if(s=function(e,r){if(v.isFunction(e))return e(r);var t=e.namespace||void 0===e.namespace&&v.mockjaxSettings.namespace;if(v.isFunction(e.url.test)){if(t&&(t=t.replace(/(\/+)$/,""),n=e.url.source.replace(/^(\^+)/,"").replace(/^/,"^("+t+")?/?"),e.url=new RegExp(n)),!e.url.test(r.url))return null}else{var n=e.url,t=(n=t?[t.replace(/(\/+)$/,""),e.url.replace(/^(\/+)/,"")].join("/"):n).indexOf("*");if(n!==r.url&&-1===t||!new RegExp(n.replace(/[-[\]{}()+?.,\\^$|#\s]/g,"\\$&").replace(/\*/g,".+")).test(r.url))return null}if(e.requestHeaders){if(void 0===r.headers)return null;var o=!1;if(v.each(e.requestHeaders,function(e,t){if(r.headers[e]!==t)return!(o=!0)}),o)return null}return!(!e.data||r.data&&M(e.data,r.data))||e&&e.type&&e.type.toLowerCase()!==r.type.toLowerCase()?null:e}(u,o)){if(v.mockjaxSettings.retainAjaxCalls&&S.push(o),L.info(s,["MOCK "+o.type.toUpperCase()+": "+o.url,v.ajaxSetup({},o)]),(301===s.status||302===s.status)&&("GET"===o.type.toUpperCase()||"HEAD"===o.type.toUpperCase())&&s.headers.Location){L.debug("Doing mock redirect to",s.headers.Location,o.type);for(var l={},c=Object.keys(n),p=0;p<c.length;p++)l[c[p]]=n[c[p]];return l.url=s.headers.Location,l.headers={Referer:n.url},e(l)}if(!o.dataType||"JSONP"!==o.dataType.toUpperCase()||!(r=w(o,s,n))){n.crossDomain=!1,s.cache=o.cache,s.timeout=o.timeout,s.global=o.global,s.isTimeout&&(1<s.responseTime?n.timeout=s.responseTime-1:(s.responseTime=2,n.timeout=1)),v.isFunction(s.onAfterSuccess)&&(n.success=a("Success",s)),v.isFunction(s.onAfterError)&&(n.error=a("Error",s)),v.isFunction(s.onAfterComplete)&&(n.complete=a("Complete",s)),j=T=y=h=m=x=d=g=f=void 0;var f=s,g=n;if(f.url instanceof RegExp&&f.hasOwnProperty("urlParams")){var d=f.url.exec(g.url);if(1!==d.length){d.shift();for(var x=0,m=d.length,h=f.urlParams.length,y=Math.min(m,h),T={};x<y;x++){var j=f.urlParams[x];T[j]=d[x]}g.urlParams=T}}!function(o,s,a,i){r=k.call(v,v.extend(!0,{},a,{xhr:function(){return t=o,r=s,e=a,n=i,L.debug(t,["Creating new mock XHR object",t,r,e,n]),void 0===(t=v.extend(!0,{},v.mockjaxSettings,t)).headers&&(t.headers={}),void 0===r.headers&&(r.headers={}),t.contentType&&(t.headers["content-type"]=t.contentType),{status:t.status,statusText:t.statusText,readyState:1,open:function(){},send:function(){n.fired=!0,O.call(this,t,r,e)},abort:function(){clearTimeout(this.responseTimer)},setRequestHeader:function(e,t){r.headers[e]=t},getResponseHeader:function(e){return t.headers&&t.headers[e]?t.headers[e]:"last-modified"===e.toLowerCase()?t.lastModified||(new Date).toString():"etag"===e.toLowerCase()?t.etag||"":"content-type"===e.toLowerCase()?t.contentType||"text/plain":void 0},getAllResponseHeaders:function(){var r="";return t.contentType&&(t.headers["content-type"]=t.contentType),v.each(t.headers,function(e,t){r+=e+": "+t+"\n"}),r}};var t,r,e,n}}))}(s,o,n,u)}return r}L.debug(u,["Mock does not match request",t,o])}}if(L.log(null,["No mock matched to request",t,n]),v.mockjaxSettings.retainAjaxCalls&&C.push(n),!0===v.mockjaxSettings.throwUnmocked)throw new Error("AJAX not mocked: "+n.url);return L.log("Real ajax call to",n.url),k.apply(v,[n])}});var L={_log:function(e,t,r){var n=v.mockjaxSettings.logging;if(e&&void 0!==e.logging&&(n=e.logging),r=0===r?r:r||s.LOG,t=t.splice?t:[t],!(!1===n||n<r))return v.mockjaxSettings.log?v.mockjaxSettings.log(e,t[1]||t[0]):v.mockjaxSettings.logger&&v.mockjaxSettings.logger[v.mockjaxSettings.logLevelMethods[r]]?v.mockjaxSettings.logger[v.mockjaxSettings.logLevelMethods[r]].apply(v.mockjaxSettings.logger,t):void 0},debug:function(e,t){return L._log(e,t,s.DEBUG)},log:function(e,t){return L._log(e,t,s.LOG)},info:function(e,t){return L._log(e,t,s.INFO)},warn:function(e,t){return L._log(e,t,s.WARN)},error:function(e,t){return L._log(e,t,s.ERROR)}},s={DEBUG:4,LOG:3,INFO:2,WARN:1,ERROR:0};return v.mockjaxSettings={log:null,logger:u.console,logging:2,logLevelMethods:["error","warn","info","log","debug"],matchInRegistrationOrder:!0,namespace:null,status:200,statusText:"OK",responseTime:n,isTimeout:!1,throwUnmocked:!1,retainAjaxCalls:!0,contentType:"text/plain",response:"",responseText:"",responseXML:"",proxy:"",proxyType:"GET",lastModified:null,etag:"",headers:{etag:"IJF@H#@923uf8023hFO@I#H#","content-type":"text/plain"}},v.mockjax=function(e){var t;return v.isArray(e)?v.map(e,function(e){return v.mockjax(e)}):(t=b.length,b[t]=e,L.log(e,["Created new mock handler",e]),t)},v.mockjax._logger=L,v.mockjax.clear=function(e){"string"==typeof e||e instanceof RegExp?b=function(t){for(var e,r=[],n=t instanceof RegExp?function(e){return t.test(e)}:function(e){return t===e},o=0,s=b.length;o<s;o++)n((e=b[o]).url)?L.log(e,["Clearing mock: "+(e&&e.url),e]):r.push(e);return r}(e):e||0===e?(L.log(b[e],["Clearing mock: "+(b[e]&&b[e].url),b[e]]),b[e]=null):(L.log(null,"Clearing all mocks"),b=[]),S=[],C=[]},v.mockjax.clearRetainedAjaxCalls=function(){S=[],C=[],L.debug(null,"Cleared retained ajax calls")},v.mockjax.handler=function(e){if(1===arguments.length)return b[e]},v.mockjax.handlers=function(){return b},v.mockjax.mockedAjaxCalls=function(){return S},v.mockjax.unfiredHandlers=function(){for(var e=[],t=0,r=b.length;t<r;t++){var n=b[t];null===n||n.fired||e.push(n)}return e},v.mockjax.unmockedAjaxCalls=function(){return C},v.mockjax}); | ||
!function(t,r){"use strict";"function"==typeof define&&define.amd&&define.amd.jQuery?define(["jquery"],function(e){return r(e,t)}):"object"==typeof exports?module.exports=r:r(t.jQuery||t.$,t)}(this,function(v,u){"use strict";var k=v.ajax,b=[],S=[],C=[],l=/=\?(&|$)/,c=(new Date).getTime(),n=500;function M(t,r){w.debug(t,["Checking mock data against request data",t,r]);var n=!0;if("function"==typeof t)return!!t(r);if("string"==typeof r){if("function"==typeof t.test)return t.test(r);if("object"!=typeof t)return t===r;r=function(e){var t,r,n,o,s={},a=String(e).split(/&/);for(t=0,r=a.length;t<r;++t){n=a[t];try{n=(n=decodeURIComponent(n.replace(/\+/g," "))).split(/=/)}catch(e){continue}s[n[0]]?(s[n[0]].splice||(o=s[n[0]],s[n[0]]=[],s[n[0]].push(o)),s[n[0]].push(n[1])):s[n[0]]=n[1]}return w.debug(null,["Getting query params from string",e,s]),s}(r)}return v.each(t,function(e){if(void 0===r[e])return n=!1;n="object"==typeof r[e]&&null!==r[e]?(n=n&&Array.isArray(r[e])?Array.isArray(t[e])&&r[e].length===t[e].length:n)&&M(t[e],r[e]):t[e]&&"function"==typeof t[e].test?n&&t[e].test(r[e]):n&&t[e]===r[e]}),n}function a(e,t){return e[t]===v.mockjaxSettings[t]}function o(e){return"number"==typeof e&&0<=e}function i(e){if(Array.isArray(e)&&2===e.length){var t=e[0],r=e[1];if(o(t)&&o(r))return Math.floor(Math.random()*(r-t))+t}else if(o(e))return e;return n}function O(t,r,n){w.debug(t,["Sending fake XHR request",t,r,n]);o=this;var o,s=function(){return function(){this.status=t.status,this.statusText=t.statusText,this.readyState=1;function e(){var e;this.readyState=4,"json"===r.dataType&&"object"==typeof t.responseText?this.responseText=JSON.stringify(t.responseText):"xml"===r.dataType?"string"==typeof t.responseXML?(this.responseXML=function(t){void 0===u.DOMParser&&u.ActiveXObject&&(u.DOMParser=function(){},DOMParser.prototype.parseFromString=function(e){var t=new ActiveXObject("Microsoft.XMLDOM");return t.async="false",t.loadXML(e),t});try{var e=(new DOMParser).parseFromString(t,"text/xml");if(!v.isXMLDoc(e))throw new Error("Unable to parse XML");if(1===v("parsererror",e).length)throw new Error("Error: "+v(e).text());return e}catch(e){t=void 0===e.name?e:e.name+": "+e.message;v(document).trigger("xmlParseError",[t])}}(t.responseXML),this.responseText=t.responseXML):this.responseXML=t.responseXML:"object"==typeof t.responseText&&null!==t.responseText?(t.contentType="application/json",this.responseText=JSON.stringify(t.responseText)):this.responseText=t.responseText,Array.isArray(t.status)?(e=Math.floor(Math.random()*t.status.length),this.status=t.status[e]):"number"!=typeof t.status&&"string"!=typeof t.status||(this.status=t.status),"string"==typeof t.statusText&&(this.statusText=t.statusText),"function"==typeof(e=this.onload||this.onreadystatechange)?(t.isTimeout&&(this.status=-1),e.call(this,t.isTimeout?"timeout":void 0)):t.isTimeout&&(this.status=-1)}if("function"==typeof t.response){if(2===t.response.length)return void t.response(n,function(){e.call(o)});t.response(n)}e.call(o)}.apply(o)};t.proxy?(w.info(t,["Retrieving proxy file: "+t.proxy,t]),k({global:!1,url:t.proxy,type:t.proxyType,data:t.data,async:r.async,dataType:"script"===r.dataType?"text/plain":r.dataType,complete:function(e){t.responseXML=t.responseText=e.responseText,a(t,"status")&&(t.status=e.status),a(t,"statusText")&&(t.statusText=e.statusText),!1===r.async?s():this.responseTimer=setTimeout(s,i(t.responseTime))}})):!1===r.async?s():this.responseTimer=setTimeout(s,i(t.responseTime))}function A(e,t,r){if("GET"===(s=e).type.toUpperCase()?l.test(s.url)||(s.url+=(/\?/.test(s.url)?"&":"?")+(s.jsonp||"callback")+"=?"):s.data&&l.test(s.data)||(s.data=(s.data?s.data+"&":"")+(s.jsonp||"callback")+"=?"),e.dataType="json",e.data&&l.test(e.data)||l.test(e.url)){var n=e,o=t,s=r,a=s&&s.context||n,i="string"==typeof n.jsonpCallback&&n.jsonpCallback||"jsonp"+c++,s=(n.data&&(n.data=(n.data+"").replace(l,"="+i+"$1")),n.url=n.url.replace(l,"="+i+"$1"),u[i]=u[i]||function(){f(n,a,o),g(n,a),u[i]=void 0;try{delete u[i]}catch(e){}},n.jsonpCallback=i,/^(\w+:)?\/\/([^\/?#]+)/.exec(e.url)),s=s&&(s[1]&&s[1]!==location.protocol||s[2]!==location.host);if(e.dataType="script","GET"===e.type.toUpperCase()&&s)return function(t,r,e){w.debug(r,["Performing JSONP request",r,t,e]);var n=e&&e.context||t,o=v.Deferred?new v.Deferred:null;if(r.response&&"function"==typeof r.response)r.response(e);else if("object"==typeof r.responseText)v.globalEval("("+JSON.stringify(r.responseText)+")");else{if(r.proxy)return w.info(r,["Performing JSONP proxy request: "+r.proxy,r]),k({global:!1,url:r.proxy,type:r.proxyType,data:r.data,dataType:"script"===t.dataType?"text/plain":t.dataType,complete:function(e){v.globalEval("("+e.responseText+")"),p(t,r,n,o)}}),o;v.globalEval("("+("string"==typeof r.responseText?'"'+r.responseText+'"':r.responseText)+")")}return p(t,r,n,o),o}(e,t,r)||!0}return null}function p(e,t,r,n){var o;setTimeout(function(){if(f(e,r,t),g(e,r),n){try{o=JSON.parse(t.responseText)}catch(e){}n.resolveWith(r,[o||t.responseText]),w.log(t,["JSONP mock call complete",t,n])}},i(t.responseTime))}function f(e,t,r){e.success&&e.success.call(t,r.responseText||"","success",{}),e.global&&(e.context?v(e.context):v.event).trigger("ajaxSuccess",[{},e])}function g(e,t){e.complete&&e.complete.call(t,{statusText:"success",status:200},"success"),e.global&&(e.context?v(e.context):v.event).trigger("ajaxComplete",[{},e]),e.global&&!--v.active&&v.event.trigger("ajaxStop")}v.extend({ajax:function e(t,n){var r,o,s;w.debug(null,["Ajax call intercepted",t,n]),"object"==typeof t?(n=t,t=void 0):(n=n||{}).url=t||n.url,(o=v.ajaxSetup({},n)).type=o.method=o.method||o.type;for(var a=function(e,t){var r=n[e.toLowerCase()];return function(){"function"==typeof r&&r.apply(this,[].slice.call(arguments)),t["onAfter"+e]()}},i=0;i<b.length;i++){var u=v.mockjaxSettings.matchInRegistrationOrder?i:b.length-1-i;if(u=b[u]){if(s=function(e,r){if("function"==typeof e)return e(r);var t=e.namespace||void 0===e.namespace&&v.mockjaxSettings.namespace;if("function"==typeof e.url.test){if(t&&(t=t.replace(/(\/+)$/,""),n=e.url.source.replace(/^(\^+)/,"").replace(/^/,"^("+t+")?/?"),e.url=new RegExp(n)),!e.url.test(r.url))return null}else{var n=e.url,t=(n=t?[t.replace(/(\/+)$/,""),e.url.replace(/^(\/+)/,"")].join("/"):n).indexOf("*");if(n!==r.url&&-1===t||!new RegExp(n.replace(/[-[\]{}()+?.,\\^$|#\s]/g,"\\$&").replace(/\*/g,".+")).test(r.url))return null}if(e.requestHeaders){if(void 0===r.headers)return null;var o=!1;if(v.each(e.requestHeaders,function(e,t){if(r.headers[e]!==t)return!(o=!0)}),o)return null}return!(!e.data||r.data&&M(e.data,r.data))||e&&e.type&&e.type.toLowerCase()!==r.type.toLowerCase()?null:e}(u,o)){if(v.mockjaxSettings.retainAjaxCalls&&S.push(o),w.info(s,["MOCK "+o.type.toUpperCase()+": "+o.url,v.ajaxSetup({},o)]),(301===s.status||302===s.status)&&("GET"===o.type.toUpperCase()||"HEAD"===o.type.toUpperCase())&&s.headers.Location){w.debug("Doing mock redirect to",s.headers.Location,o.type);for(var l={},c=Object.keys(n),p=0;p<c.length;p++)l[c[p]]=n[c[p]];return l.url=s.headers.Location,l.headers={Referer:n.url},e(l)}if(!o.dataType||"JSONP"!==o.dataType.toUpperCase()||!(r=A(o,s,n))){n.crossDomain=!1,s.cache=o.cache,s.timeout=o.timeout,s.global=o.global,s.isTimeout&&(1<s.responseTime?n.timeout=s.responseTime-1:(s.responseTime=2,n.timeout=1)),"function"==typeof s.onAfterSuccess&&(n.success=a("Success",s)),"function"==typeof s.onAfterError&&(n.error=a("Error",s)),"function"==typeof s.onAfterComplete&&(n.complete=a("Complete",s)),j=T=h=m=y=x=d=g=f=void 0;var f=s,g=n;if(f.url instanceof RegExp&&f.hasOwnProperty("urlParams")){var d=f.url.exec(g.url);if(1!==d.length){d.shift();for(var x=0,y=d.length,m=f.urlParams.length,h=Math.min(y,m),T={};x<h;x++){var j=f.urlParams[x];T[j]=d[x]}g.urlParams=T}}!function(o,s,a,i){r=k.call(v,v.extend(!0,{},a,{xhr:function(){return t=o,r=s,e=a,n=i,w.debug(t,["Creating new mock XHR object",t,r,e,n]),void 0===(t=v.extend(!0,{},v.mockjaxSettings,t)).headers&&(t.headers={}),void 0===r.headers&&(r.headers={}),t.contentType&&(t.headers["content-type"]=t.contentType),{status:t.status,statusText:t.statusText,readyState:1,open:function(){},send:function(){n.fired=!0,O.call(this,t,r,e)},abort:function(){clearTimeout(this.responseTimer)},setRequestHeader:function(e,t){r.headers[e]=t},getResponseHeader:function(e){return t.headers&&t.headers[e]?t.headers[e]:"last-modified"===e.toLowerCase()?t.lastModified||(new Date).toString():"etag"===e.toLowerCase()?t.etag||"":"content-type"===e.toLowerCase()?t.contentType||"text/plain":void 0},getAllResponseHeaders:function(){var r="";return t.contentType&&(t.headers["content-type"]=t.contentType),v.each(t.headers,function(e,t){r+=e+": "+t+"\n"}),r}};var t,r,e,n}}))}(s,o,n,u)}return r}w.debug(u,["Mock does not match request",t,o])}}if(w.log(null,["No mock matched to request",t,n]),v.mockjaxSettings.retainAjaxCalls&&C.push(n),!0===v.mockjaxSettings.throwUnmocked)throw new Error("AJAX not mocked: "+n.url);return w.log("Real ajax call to",n.url),k.apply(v,[n])}});var w={_log:function(e,t,r){var n=v.mockjaxSettings.logging;if(e&&void 0!==e.logging&&(n=e.logging),r=0===r?r:r||s.LOG,t=t.splice?t:[t],!(!1===n||n<r))return v.mockjaxSettings.log?v.mockjaxSettings.log(e,t[1]||t[0]):v.mockjaxSettings.logger&&v.mockjaxSettings.logger[v.mockjaxSettings.logLevelMethods[r]]?v.mockjaxSettings.logger[v.mockjaxSettings.logLevelMethods[r]].apply(v.mockjaxSettings.logger,t):void 0},debug:function(e,t){return w._log(e,t,s.DEBUG)},log:function(e,t){return w._log(e,t,s.LOG)},info:function(e,t){return w._log(e,t,s.INFO)},warn:function(e,t){return w._log(e,t,s.WARN)},error:function(e,t){return w._log(e,t,s.ERROR)}},s={DEBUG:4,LOG:3,INFO:2,WARN:1,ERROR:0};return v.mockjaxSettings={log:null,logger:u.console,logging:2,logLevelMethods:["error","warn","info","log","debug"],matchInRegistrationOrder:!0,namespace:null,status:200,statusText:"OK",responseTime:n,isTimeout:!1,throwUnmocked:!1,retainAjaxCalls:!0,contentType:"text/plain",response:"",responseText:"",responseXML:"",proxy:"",proxyType:"GET",lastModified:null,etag:"",headers:{etag:"IJF@H#@923uf8023hFO@I#H#","content-type":"text/plain"}},v.mockjax=function(e){var t;return Array.isArray(e)?v.map(e,function(e){return v.mockjax(e)}):(t=b.length,b[t]=e,w.log(e,["Created new mock handler",e]),t)},v.mockjax._logger=w,v.mockjax.clear=function(e){"string"==typeof e||e instanceof RegExp?b=function(t){for(var e,r=[],n=t instanceof RegExp?function(e){return t.test(e)}:function(e){return t===e},o=0,s=b.length;o<s;o++)n((e=b[o]).url)?w.log(e,["Clearing mock: "+(e&&e.url),e]):r.push(e);return r}(e):e||0===e?(w.log(b[e],["Clearing mock: "+(b[e]&&b[e].url),b[e]]),b[e]=null):(w.log(null,"Clearing all mocks"),b=[]),S=[],C=[]},v.mockjax.clearRetainedAjaxCalls=function(){S=[],C=[],w.debug(null,"Cleared retained ajax calls")},v.mockjax.handler=function(e){if(1===arguments.length)return b[e]},v.mockjax.handlers=function(){return b},v.mockjax.mockedAjaxCalls=function(){return S},v.mockjax.unfiredHandlers=function(){for(var e=[],t=0,r=b.length;t<r;t++){var n=b[t];null===n||n.fired||e.push(n)}return e},v.mockjax.unmockedAjaxCalls=function(){return C},v.mockjax}); |
@@ -67,3 +67,4 @@ const PORT = 4000; | ||
'2.2.4', | ||
'3.7.1' | ||
'3.7.1', | ||
'4.0.0-beta.2' | ||
] | ||
@@ -79,3 +80,4 @@ }, | ||
'2.2.4', | ||
'3.7.1' | ||
'3.7.1', | ||
'4.0.0-beta.2' | ||
] | ||
@@ -82,0 +84,0 @@ }, |
{ | ||
"name": "jquery-mockjax", | ||
"title": "jQuery Mockjax", | ||
"version": "2.6.1", | ||
"version": "2.7.0-beta.0", | ||
"main": "./src/jquery.mockjax.js", | ||
@@ -6,0 +6,0 @@ "description": "The jQuery Mockjax Plugin provides a simple and extremely flexible interface for mocking or simulating ajax requests and responses.", |
@@ -71,3 +71,3 @@ (function(root, factory) { | ||
if ( $.isFunction(mock) ) { | ||
if (typeof mock === 'function') { | ||
return !!mock(live); | ||
@@ -79,3 +79,3 @@ } | ||
// Querystring may be a regex | ||
if ($.isFunction( mock.test )) { | ||
if (typeof mock.test === 'function') { | ||
return mock.test(live); | ||
@@ -95,8 +95,8 @@ } else if (typeof mock === 'object') { | ||
if ( typeof live[k] === 'object' && live[k] !== null ) { | ||
if ( identical && $.isArray( live[k] ) ) { | ||
identical = $.isArray( mock[k] ) && live[k].length === mock[k].length; | ||
if ( identical && Array.isArray( live[k] ) ) { | ||
identical = Array.isArray( mock[k] ) && live[k].length === mock[k].length; | ||
} | ||
identical = identical && isMockDataEqual(mock[k], live[k]); | ||
} else { | ||
if ( mock[k] && $.isFunction( mock[k].test ) ) { | ||
if ( mock[k] && typeof mock[k].test === 'function') { | ||
identical = identical && mock[k].test(live[k]); | ||
@@ -156,3 +156,3 @@ } else { | ||
// want to mock this request | ||
if ( $.isFunction(handler) ) { | ||
if (typeof handler === 'function') { | ||
return handler( requestSettings ); | ||
@@ -166,3 +166,3 @@ } | ||
// matches the url for this ajax request | ||
if ( $.isFunction(handler.url.test) ) { | ||
if (typeof handler.url.test === 'function') { | ||
// namespace exists prepend handler.url with namespace | ||
@@ -241,3 +241,3 @@ if (!!namespace) { | ||
function parseResponseTimeOpt(responseTime) { | ||
if ($.isArray(responseTime) && responseTime.length === 2) { | ||
if (Array.isArray(responseTime) && responseTime.length === 2) { | ||
var min = responseTime[0]; | ||
@@ -291,3 +291,3 @@ var max = responseTime[1]; | ||
if ($.isArray(mockHandler.status)) { | ||
if (Array.isArray(mockHandler.status)) { | ||
var idxStatus = Math.floor(Math.random() * mockHandler.status.length); | ||
@@ -306,3 +306,3 @@ this.status = mockHandler.status[idxStatus]; | ||
// jQuery < 1.4 doesn't have onreadystate change for xhr | ||
if ( $.isFunction( onReady ) ) { | ||
if (typeof onReady === 'function') { | ||
if( mockHandler.isTimeout) { | ||
@@ -320,3 +320,3 @@ this.status = -1; | ||
// the mock handler a chance to update it's data | ||
if ( $.isFunction(mockHandler.response) ) { | ||
if (typeof mockHandler.response === 'function') { | ||
// Wait for it to finish | ||
@@ -497,3 +497,3 @@ if ( mockHandler.response.length === 2 ) { | ||
// If the response handler on the moock is a function, call it | ||
if ( mockHandler.response && $.isFunction(mockHandler.response) ) { | ||
if ( mockHandler.response && typeof mockHandler.response === 'function' ) { | ||
@@ -548,3 +548,3 @@ mockHandler.response(origSettings); | ||
try { | ||
json = $.parseJSON( mockHandler.responseText ); | ||
json = JSON.parse( mockHandler.responseText ); | ||
} catch (err) { /* just checking... */ } | ||
@@ -645,3 +645,3 @@ | ||
return function() { | ||
if ( $.isFunction(origHandler) ) { | ||
if (typeof origHandler === 'function') { | ||
origHandler.apply(this, [].slice.call(arguments)); | ||
@@ -730,9 +730,9 @@ } | ||
// Set up onAfter[X] callback functions | ||
if ( $.isFunction( mockHandler.onAfterSuccess ) ) { | ||
if (typeof mockHandler.onAfterSuccess === 'function') { | ||
origSettings.success = overrideCallback('Success', mockHandler); | ||
} | ||
if ( $.isFunction( mockHandler.onAfterError ) ) { | ||
if (typeof mockHandler.onAfterError === 'function') { | ||
origSettings.error = overrideCallback('Error', mockHandler); | ||
} | ||
if ( $.isFunction( mockHandler.onAfterComplete ) ) { | ||
if (typeof mockHandler.onAfterComplete === 'function') { | ||
origSettings.complete = overrideCallback('Complete', mockHandler); | ||
@@ -941,3 +941,3 @@ } | ||
// Multiple mocks. | ||
if ( $.isArray(settings) ) { | ||
if (Array.isArray(settings)) { | ||
return $.map(settings, function(s) { | ||
@@ -944,0 +944,0 @@ return $.mockjax(s); |
@@ -295,3 +295,3 @@ (function(qunit, $) { | ||
type: 'GET', | ||
url: 'http://jquery-mockjax-foobar.com/somefile.js', | ||
url: 'http://crossdomain.test/somefile.js', | ||
responseText: '(window.mockjaxCrossDomain=true)' | ||
@@ -303,3 +303,4 @@ }); | ||
dataType: 'script', | ||
url: 'http://jquery-mockjax-foobar.com/somefile.js', | ||
url: 'http://crossdomain.test/somefile.js', | ||
crossOrigin: 'anonymous', | ||
error: qunit.noErrorCallbackExpected, | ||
@@ -318,3 +319,3 @@ success: function() { | ||
type: 'POST', | ||
url: 'http://jquery-mockjax-foobar.com/somefile.js', | ||
url: 'http://crossdomain.test/somefile.js', | ||
responseText: '(window.mockjaxCrossDomain=true)' | ||
@@ -326,3 +327,4 @@ }); | ||
dataType: 'script', | ||
url: 'http://jquery-mockjax-foobar.com/somefile.js', | ||
url: 'http://crossdomain.test/somefile.js', | ||
crossOrigin: 'anonymous', | ||
error: qunit.noErrorCallbackExpected, | ||
@@ -443,2 +445,2 @@ success: function() { | ||
})(window.QUnit, window.jQuery); | ||
})(window.QUnit, window.jQuery); |
@@ -20,3 +20,3 @@ (function(qunit, $) { | ||
$.mockjax({ | ||
url: 'http://foobar.com/jsonp-delay?callback=?', | ||
url: 'http://connection.test/jsonp-delay?callback=?', | ||
contentType: 'text/json', | ||
@@ -110,3 +110,3 @@ proxy: 'test_jsonp.js', | ||
$.ajax({ | ||
url: 'http://foobar.com/jsonp-delay?callback=?', | ||
url: 'http://connection.test/jsonp-delay?callback=?', | ||
dataType: 'jsonp', | ||
@@ -136,3 +136,3 @@ complete: function() { | ||
$.ajax({ | ||
url: 'http://foobar.com/jsonp-delay?callback=?', | ||
url: 'http://connection.test/jsonp-delay?callback=?', | ||
dataType: 'jsonp' | ||
@@ -139,0 +139,0 @@ }).done(function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1385962
93
40024
1
1