-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsinon_server_sugar.js
83 lines (72 loc) · 2.17 KB
/
sinon_server_sugar.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this.Response = (function() {
function Response(server) {
this.log = __bind(this.log, this);
this.respond = __bind(this.respond, this);
this.restore = __bind(this.restore, this);
this.queue = __bind(this.queue, this);
this["default"] = __bind(this["default"], this);
this["with"] = __bind(this["with"], this);
this.server = server || sinon.fakeServer.create();
this["default"]();
}
Response.prototype["with"] = function(options) {
var data;
data = options;
if (_.isFunction(options.url) && _.isFunction(options.toJSON)) {
data = {
url: options.url(),
content: JSON.stringify(options.toJSON())
};
}
_.extend(this.options, data);
return this;
};
Response.prototype.defaultOptions = function() {
return {
method: "GET",
url: '/',
status: 200,
headers: {
"Content-Type": "application/json"
},
content: '{}'
};
};
Response.prototype["default"] = function() {
this.options = this.defaultOptions();
return this;
};
Response.prototype.queue = function(options) {
if (options) {
this["with"](options);
}
this.server.respondWith(this.options.method, this.options.url, [this.options.status, this.options.headers, this.options.content]);
return this;
};
Response.prototype.restore = function() {
return this.server.restore();
};
Response.prototype.respond = function() {
return this.server.respond();
};
Response.prototype.log = function(key) {
if (key) {
if (typeof console !== "undefined" && console !== null) {
if (typeof console.log === "function") {
console.log(this.options[key]);
}
}
} else {
if (typeof console !== "undefined" && console !== null) {
if (typeof console.log === "function") {
console.log(this.options);
}
}
}
return this;
};
return Response;
})();
}).call(this);