Skip to content

Instantly share code, notes, and snippets.

@dragoncreations
Forked from ducin/index.html
Last active August 29, 2015 14:07
Show Gist options
  • Save dragoncreations/a131db8b1c1089f3831a to your computer and use it in GitHub Desktop.
Save dragoncreations/a131db8b1c1089f3831a to your computer and use it in GitHub Desktop.
<!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>
["predefined","value","from","resource.json","file"]
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