-
Notifications
You must be signed in to change notification settings - Fork 4
/
mock-fetch-api.js
164 lines (113 loc) · 3.95 KB
/
mock-fetch-api.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
ISC License:
Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC")
Copyright (c) 1995-2003 by Internet Software Consortium
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
(function(global) {
require('es6-promise').polyfill();
require('isomorphic-fetch');
var extend = require('object-extend');
var conditions = [];
var failNextCall = false;
global.fetch = function(uri, options) {
var options = extend({
method: 'GET',
headers: null,
}, options || {});
if (uri instanceof Request) {
options = uri;
uri = uri.url;
if (!options.method) {
options.method = 'GET'
}
}
return new Promise(function(resolve, reject) {
if(failNextCall) {
failNextCall = false;
return reject("as requested");
}
for (var ii = 0; ii < conditions.length; ii++) {
var criteria = conditions[ii];
// Compare methods
if(criteria.method == options.method && criteria.uri == uri) {
// Compare headers
for(var jj=0; jj<criteria.headers.length; jj++) {
var expectedHeader = criteria.headers[jj];
if(!options.headers || !options.headers.has(expectedHeader.header)
|| options.headers.get(expectedHeader.header) != expectedHeader.value) {
if(expectedHeader.elseResponse) {
return resolve(new Response("", {
status: expectedHeader.elseResponse.status,
statusText: expectedHeader.elseResponse.statusText
}));
}
return resolve(new Response("", {
status: 404,
statusText: "Not Found"
}));
}
}
return resolve(new Response(criteria.response.jsonData, {
status: criteria.response.status,
headers: criteria.response.headers
}));
}
}
return resolve(new Response("", {
status: 404,
statusText: "Not Found"
}));
});
}
module.exports = {
when: function(method, uri) {
var condition = {
method: method,
uri: uri,
headers: [],
response: null,
withExpectedHeader: function(header, value) {
condition.headers.push({
header: header,
value: value,
elseResponse: null
});
return condition;
},
otherwiseRespondWith: function(status, statusText) {
if(condition.headers.length > 0) {
condition.headers[condition.headers.length-1].elseResponse = {
status: status,
statusText: statusText,
};
return condition;
}
throw "no preceding header set";
},
respondWith: function(status, data, headers) {
condition.response = {
status: status,
jsonData: data,
headers: headers
};
conditions.push(condition);
return true;
}
};
return condition;
},
failNextCall: function () {
failNextCall = true;
}
};
})(typeof window === 'undefined' ? this : window);