This function creates a mock object from existing JavaScript objects.
var mock = function(a,b,c,d){d='prototype';for(c in a[d])b[d][c]=function(k){return function(){b(k,arguments)}}(c);return b};
// replace XMLHttpRequest with a mock
// the callback function should check if "method" is defined
XMLHttpRequest = mock(
XMLHttpRequest,
function(method, parameters){
method && console.log(method, [].slice.call(parameters))
}
)
// use on a page that has jQuery or Zepto loaded
$.ajax();
// output in console
// open ["GET", "http://zeptojs.com/", true]
// setRequestHeader ["X-Requested-With", "XMLHttpRequest"]
// send [undefined]
// augment with your own implementations
var MockXHR = function(method, parameters){
method && console.log(method, [].slice.call(parameters))
}
XMLHttpRequest = mock(XMLHttpRequest, MockXHR);
MockXHR.prototype.setRequestHeader = function(mehtod, name, value){
console.log('!!!OMG!!!', name, value);
}
$.ajax();
// open ["GET", "https://gist.github.com/980912", true]
// !!!OMG!!! XMLHttpRequest undefined
// !!!OMG!!! */* undefined
// !!!OMG!!! text/javascript undefined
// send [null]
// on a page that has Prototype.js loaded
new Ajax.Request('/')
// open ["POST", "/", true]
// setRequestHeader ["X-Requested-With", "XMLHttpRequest"]
// setRequestHeader ["X-Prototype-Version", "1.6.1"]
// setRequestHeader ["Accept", "text/javascript, text/html, application/xml, text/xml, */*"]
// setRequestHeader ["Content-type", "application/x-www-form-urlencoded; charset=UTF-8"]
// send [""]
Demo on a site that uses Prototype: