-
Notifications
You must be signed in to change notification settings - Fork 1
/
fake.server.js
60 lines (51 loc) · 1.25 KB
/
fake.server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var FakeServer = function() {
var self = this;
this.sinonServer = sinon.fakeServer.create();
this.asyncComplete = false;
this.complete = function () {
return self._complete.apply(self, arguments);
};
};
FakeServer.prototype.addRoute = function (options) {
options.method = options.method || 'GET'
options.status = options.status || 200;
options.headers = options.headers || { 'Content-Type': 'text/plain' };
options.body = options.body || '';
if (typeof options.body === 'object') {
options.body = JSON.stringify(options.body);
options.headers['Content-Type'] = 'application/json';
};
this.sinonServer.respondWith(
options.type,
options.url,
function (xhr, id) {
xhr.respond(
options.status,
options.headers,
options.body
);
}
);
};
FakeServer.prototype.routes = function (responses) {
var self = this;
responses.forEach(function (response) {
self.addRoute.call(self, response);
});
};
FakeServer.prototype.respond = function (callback) {
var self = this;
runs(function () {
self.sinonServer.respond();
});
waitsFor(function () {
return self.asyncComplete;
});
runs(function () {
self.asyncComplete = false;
callback();
});
};
FakeServer.prototype._complete = function () {
this.asyncComplete = true;
};