Created
July 14, 2010 12:29
-
-
Save mistersourcerer/475348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.extend({ | |
mocker: { | |
sleeper: function() {}, | |
ajax: function() {}, | |
ajaxRequestData: {}, | |
} | |
}); | |
$.mocker.sleeper = function(milisecs, func) { | |
startTime = new Date().getTime(); | |
endTime = startTime + milisecs; | |
isDelayedCall = func != undefined; | |
if (isDelayedCall) | |
{ | |
setTimeout(func, milisecs); | |
} | |
else | |
{ | |
while(new Date().getTime() < endTime); | |
} | |
}; | |
$.mocker.ajaxRequestData = { | |
'url': null, | |
'method': null, | |
'data': null | |
}; | |
$.mocker.ajax = function(opts) { | |
var contentTypes = { | |
'script': 'text/javascript; charset=utf-8' | |
}; | |
var fakeXhr = { | |
'responseText': (opts['responseText'] != undefined) ? opts['responseText'] : '', | |
'status': 0, | |
'readyState': 0, | |
'_headers': {}, | |
'_responseHeaders': { | |
'content-type': contentTypes['script'] | |
}, | |
'open': function(method, url, async, user, password) { | |
asyncStr = (async != undefined && async) ? '(async) ' : ' '; | |
userStr = (user != undefined) ? '@'+ user : ' '; | |
passwordStr = (password != undefined) ? ':'+ password : ' '; | |
console.log('[open] '+ method +' '+ url +' '+ asyncStr + userStr + passwordStr); | |
this.readyState = 1; | |
this.onreadystatechange(); | |
$.mocker.ajaxRequestData.method = method; | |
$.mocker.ajaxRequestData.url = url; | |
}, | |
'send': function(data) { | |
$.mocker.ajaxRequestData.data = data; | |
console.log('request send with data: '+ data); | |
this.status = 200; | |
this.readyState = 2; | |
this.onreadystatechange(); | |
console.log("sending (status available)"); | |
var xhr = this; | |
$.mocker.sleeper(1, function() { | |
xhr.readyState = 3; | |
xhr.onreadystatechange(); | |
console.log("send!"); | |
$.mocker.sleeper(1, function() { | |
xhr.readyState = 4; | |
xhr.onreadystatechange(); | |
console.log("complete!"); | |
}); | |
}); | |
}, | |
'abort': function() { | |
console.log("aborted"); | |
}, | |
'onreadystatechange': function() { | |
console.log('state changed') | |
}, | |
'setRequestHeader': function(key, value) { | |
console.log('request header ['+ key + '] => '+ value); | |
this._headers[key] = value; | |
}, | |
'getResponseHeader': function(header) { | |
console.log('response header ['+ header +'] => '+ this._responseHeaders[header]); | |
return this._responseHeaders[header]; | |
} | |
}; | |
$.ajaxSetup({xhr: function() { | |
console.log("chamada ajax fake."); | |
return fakeXhr; | |
}}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment