-
-
Save sckott/c13ac1ea0c860563a6a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Sinon.js fakeServer demo</title> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js"></script> | |
<script src="sinon-fake-server.js"></script> | |
</head> | |
<body> | |
<label for="fsOn">use sinon.js fakeServer</label> | |
<input type="checkbox" id="fsOn" /> | |
<br /> | |
<button type="button" id="call">Make AJAX call</button> | |
</body> | |
</html> |
This file contains hidden or 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
["predefined","value","from","resource.json","file"] |
This file contains hidden or 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
var url = 'resource.json'; | |
var value = ['sinon', 'is', 'a', 'nice', 'tool']; | |
// sinon fake server wrapper | |
var fakeServerWrapper = { | |
init: function() { | |
this.fs = sinon.fakeServer.create(); | |
this.fs.xhr.useFilters = true; | |
this.fs.xhr.addFilter( | |
function(method, url, async, username, password) { | |
return !(new RegExp(url)).test(url); | |
}); | |
this.fs.respondWith("GET", url, | |
[200, { "Content-Type": "application/json" }, | |
JSON.stringify(value) ]); | |
this.fs.autoRespond = true; | |
}, | |
restore: function() { | |
this.fs.restore(); | |
} | |
}; | |
// simple ui | |
$(document).ready(function() { | |
var callButton = $('#call'), | |
fsCheckbox = $('#fsOn'); | |
fsCheckbox.change(function() { | |
if (fsCheckbox.is(':checked')) { | |
fakeServerWrapper.init(); | |
} else { | |
fakeServerWrapper.restore(); | |
} | |
}); | |
callButton.click(function() { | |
$.ajax({ | |
url: url | |
}).done(function(value) { | |
console.info(value); | |
}).fail(function(value) { | |
console.error(value); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment