forked from jakerella/jquery-mockjax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.mockjax.min.js
22 lines (22 loc) · 3.88 KB
/
jquery.mockjax.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*!
* MockJax - Mock for Ajax requests
*
* Version: 1.3.1
* Released: 2010-08-11
* Source: http://github.com/appendto/jquery-mockjax
* Plugin: mockjax
* Author: Jonathan Sharp (http://jdsharp.com)
* License: MIT,GPL
*
* Copyright (c) 2010 appendTo LLC.
* Dual licensed under the MIT or GPL licenses.
* http://appendto.com/open-source-licenses
*/
(function(c){var m=c.ajax,f=[];c.extend({ajax:function(j){var a=jQuery.extend(true,{},jQuery.ajaxSettings,j),n=false;c.each(f,function(g){if(f[g]){var b=null;if(c.isFunction(f[g]))b=f[g](a);else{b=f[g];if(c.isFunction(b.url.test))b.url.test(a.url)||(b=null);else{g=b.url.indexOf("*");if(b.url!="*"&&b.url!=a.url&&g==-1||g>-1&&b.url.substr(0,g)!=a.url.substr(0,g))b=null}if(b){if(b.data&&a.data){var l=false;(function d(h,k){c.each(h,function(i){if(k[i]===undefined)return l=false;else{l=true;if(typeof k[i]==
"object")return d(h[i],k[i]);else return l=c.isFunction(h[i].test)?h[i].test(k[i]):h[i]==k[i]}})})(b.data,a.data);if(l==false)b=null}if(b&&b.type&&b.type!=a.type)b=null}}if(b){typeof console!=="undefined"&&console.log&&console.log("MOCK GET: "+a.url);n=true;if(a.dataType==="jsonp"){if(type==="GET")e.test(a.url)||(a.url+=(rquery.test(a.url)?"&":"?")+(a.jsonp||"callback")+"=?");else if(!a.data||!e.test(a.data))a.data=(a.data?a.data+"&":"")+(a.jsonp||"callback")+"=?";a.dataType="json"}var e=/=\?(&|$)/;
if(a.dataType==="json"&&(a.data&&e.test(a.data)||e.test(a.url))){jsonp=a.jsonpCallback||"jsonp"+jsc++;if(a.data)a.data=(a.data+"").replace(e,"="+jsonp+"$1");a.url=a.url.replace(e,"="+jsonp+"$1");a.dataType="script";window[jsonp]=window[jsonp]||function(d){data=d;o();p();window[jsonp]=undefined;try{delete window[jsonp]}catch(h){}head&&head.removeChild(script)}}e=(e=/^(\w+:)?\/\/([^\/?#]+)/.exec(a.url))&&(e[1]&&e[1]!==location.protocol||e[2]!==location.host);if(a.dataType==="script"&&a.type==="GET"&&
e){var q=j&&j.context||a,o=function(){if(a.success)a.success.call(q,b.response?b.response.toString():b.responseText||"",status,{});if(a.global){var d=[{},a];(a.context?jQuery(a.context):jQuery.event).trigger("ajaxSuccess",d)}},p=function(){a.complete&&a.complete.call(q,{},status);if(a.global){var d=[{},a];(a.context?jQuery(a.context):jQuery.event).trigger("ajaxComplete",d)}a.global&&!--jQuery.active&&jQuery.event.trigger("ajaxStop")};c.globalEval(b.responseText);o();p();return false}m.call(c,c.extend(true,
{},j,{xhr:function(){b=c.extend({},c.mockjaxSettings,b);return{status:b.status,readyState:1,open:function(){},send:function(){var d=c.proxy(function(){this.status=b.status;this.readyState=4;c.isFunction(b.response)&&b.response();if(a.dataType=="json"&&typeof b.responseText=="object")this.responseText=JSON.stringify(b.responseText);else if(a.dataType=="xml")this.responseXML=c.xmlDOM&&typeof b.responseXML=="string"?c.xmlDOM(b.responseXML)[0]:b.responseXML;else this.responseText=b.responseText;this.onreadystatechange(b.isTimeout?
"timeout":undefined)},this);if(b.proxy)m({global:false,url:b.proxy,type:b.type,data:b.data,dataType:a.dataType,complete:function(h){b.responseXML=h.responseXML;b.responseText=h.responseText;d()}});else if(a.async===false)d();else this.responseTimer=setTimeout(d,b.responseTime||50)},abort:function(){clearTimeout(this.responseTimer)},setRequestHeader:function(){},getResponseHeader:function(d){if(b.headers&&b.headers[d])return b.headers[d];else if(d=="Last-modified")return b.lastModified||(new Date).toString();
else if(d=="Etag")return b.etag||"";else if(d=="content-type")return b.contentType||"text/plain"}}}}));return false}}});if(!n)return m.apply(c,arguments)}});c.mockjaxSettings={status:200,responseTime:500,isTimeout:false,contentType:"text/plain",response:"",responseText:"",responseXML:"",proxy:"",lastModified:null,etag:"",headers:{etag:"IJF@H#@923uf8023hFO@I#H#","content-type":"text/plain"}};c.mockjax=function(j){var a=f.length;f[a]=j;return a};c.mockjaxClear=function(j){if(arguments.length==1)f[j]=
null;else f=[]}})(jQuery);