lundi 23 février 2015

is faking GoogleMaps API script request with sinon.fakeServer possible?

I'm trying to mock googleMaps API with sinon.js and specifically to intercept the call and replace it with a mock. Is this possible with sinon.js fake server?


even more specifically, I'd like to mock the getScript() request



window.google = window.google || {};
google.maps = google.maps || {};
(function() {

function getScript(src) {
var s = document.createElement('script');

s.src = src;
document.body.appendChild(s);
}

var modules = google.maps.modules = {};
google.maps.__gjsload__ = function(name, text) {
modules[name] = text;
};

google.maps.Load = function(apiLoad) {
delete google.maps.Load;
apiLoad([0.009999999776482582,[[["http://ift.tt/1a5Qtkn","http://ift.tt/1EIFqH6"],null,null,null,null,"m@292000000",["http://ift.tt/1a5Qtkv","http://ift.tt/1EIFpTD"]],[["http://ift.tt/1a5QqVK","http://ift.tt/1EIFqHa"],null,null,null,1,"166",["http://ift.tt/1a5QqVO","http://ift.tt/1EIFqHc"]],[["http://ift.tt/1a5Qrc6","http://ift.tt/1EIFpTF"],null,null,null,null,"h@292000000",["http://ift.tt/1a5Qrca","http://ift.tt/1EIFqHg"]],[["http://ift.tt/1a5Qrci","http://ift.tt/1EIFpTJ"],null,null,null,null,"t@132,r@292000000",["http://ift.tt/1EIFpTL","http://ift.tt/1EIFqHk"]],null,null,[["http://ift.tt/Tlz1PK?","http://ift.tt/1lYfi5w?"]],[["http://ift.tt/1EIFqXD","http://ift.tt/1a5QrsN"],null,null,null,null,"84",["http://ift.tt/1a5Qu7N","http://ift.tt/1EIFqa6"]],[["http://ift.tt/1a5Qu7P","http://ift.tt/1EIFqXL"]],[["http://ift.tt/1a5Quon","http://ift.tt/1a5Quop"]],[["http://ift.tt/1EIFqXR","http://ift.tt/1a5Quov"]],[["http://ift.tt/1EIFqa9","http://ift.tt/1a5Qu81"]],[["http://ift.tt/1a5Qu7P","http://ift.tt/1EIFqXL"]],[["http://ift.tt/1a5Quon","http://ift.tt/1a5Quop"]],[["http://ift.tt/1EIFqa9","http://ift.tt/1a5Qu81"]]],["en-GB","US",null,0,null,null,"http://ift.tt/1lYfiCs","https://csi.gstatic.com","http://ift.tt/Tlz26q","http://ift.tt/Tlz26q",null,"https://maps.google.com","https://gg.google.com","http://ift.tt/1a5QuEM"],["http://ift.tt/1EIFreb","3.20.0"],[1244554589],1,null,null,null,null,null,"window._TO.googleMapAPI.onGoogleMapsLoad",null,null,1,"http://ift.tt/1a5Qx3H",null,"http://ift.tt/Tlz2mK","http://ift.tt/Tlz2mK",null,"http://ift.tt/1lYfguh",[["http://ift.tt/1lYfeTt","http://ift.tt/Tlz3qU"],["http://ift.tt/1lYfeTt","http://ift.tt/Tlz3qU"],null,null,null,null,null,null,null,null,null,null,["http://ift.tt/Tlz3qW","http://ift.tt/1lYfiT2"],"/maps/vt",292000000,132],2,500,[null,"http://ift.tt/1EIFt5G","http://ift.tt/1a5Qxkb","","http://ift.tt/1EIFt5I","","http://ift.tt/Tlz2mX",["http://ift.tt/1EIFt5K","http://ift.tt/1a5QxAt","http://ift.tt/1EIFruB","http://ift.tt/1a5QuVB"]],["http://ift.tt/1EIFruD","http://ift.tt/1a5QxAB"],null,0,0], loadScriptTime);
};
var loadScriptTime = (new Date).getTime();
getScript("http://ift.tt/1EIFt5U");
})();


on this endpoint http://ift.tt/1EIFt5U


I tried this mock but it didn't work.



beforeEach(function() {
module(function($provide) {
$provide.value('jquery', $);
server = sinon.fakeServer.create();

server.autoRespond = true;

server.respondWith("GET", "http://ift.tt/1lrRXYd",
[200, { "Content-Type": "text/javascript" },
'(function(){return {}})()']);

server.respondWith("GET", "http://ift.tt/1EIFt5W",
[200, { "Content-Type": "text/javascript" },
'(function(){return {}})()']);
});
});

afterEach(function(){
server.restore();
});


The script tag is doing the request normally resolving to random javascript errors during test.


So the Question is...



  • Is it faking script requests with sinon possible?

  • If yes can you point me to the right direction to fake them?

  • If no is there any other way to fake this script requests?


Aucun commentaire:

Enregistrer un commentaire