forked from pretenderjs/pretender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretender.js
489 lines (417 loc) · 14.6 KB
/
pretender.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
(function(self) {
'use strict';
function getModuleDefault(module) {
return module.default || module;
}
var appearsBrowserified = typeof self !== 'undefined' &&
typeof process !== 'undefined' &&
Object.prototype.toString.call(process) === '[object Object]';
var RouteRecognizer = appearsBrowserified ? getModuleDefault(require('route-recognizer')) : self.RouteRecognizer;
var FakeXMLHttpRequest = appearsBrowserified ? getModuleDefault(require('fake-xml-http-request')) :
self.FakeXMLHttpRequest;
/**
* parseURL - decompose a URL into its parts
* @param {String} url a URL
* @return {Object} parts of the URL, including the following
*
* 'https://www.yahoo.com:1234/mypage?test=yes#abc'
*
* {
* host: 'www.yahoo.com:1234',
* protocol: 'https:',
* search: '?test=yes',
* hash: '#abc',
* href: 'https://www.yahoo.com:1234/mypage?test=yes#abc',
* pathname: '/mypage',
* fullpath: '/mypage?test=yes'
* }
*/
function parseURL(url) {
// TODO: something for when document isn't present... #yolo
var anchor = document.createElement('a');
anchor.href = url;
if (!anchor.host) {
anchor.href = anchor.href; // IE: load the host and protocol
}
var pathname = anchor.pathname;
if (pathname.charAt(0) !== '/') {
pathname = '/' + pathname; // IE: prepend leading slash
}
var host = anchor.host;
if (anchor.port === '80' || anchor.port === '443') {
host = anchor.hostname; // IE: remove default port
}
return {
host: host,
protocol: anchor.protocol,
search: anchor.search,
hash: anchor.hash,
href: anchor.href,
pathname: pathname,
fullpath: pathname + (anchor.search || '') + (anchor.hash || '')
};
}
/**
* Registry
*
* A registry is a map of HTTP verbs to route recognizers.
*/
function Registry(/* host */) {
// Herein we keep track of RouteRecognizer instances
// keyed by HTTP method. Feel free to add more as needed.
this.verbs = {
GET: new RouteRecognizer(),
PUT: new RouteRecognizer(),
POST: new RouteRecognizer(),
DELETE: new RouteRecognizer(),
PATCH: new RouteRecognizer(),
HEAD: new RouteRecognizer(),
OPTIONS: new RouteRecognizer()
};
}
/**
* Hosts
*
* a map of hosts to Registries, ultimately allowing
* a per-host-and-port, per HTTP verb lookup of RouteRecognizers
*/
function Hosts() {
this._registries = {};
}
/**
* Hosts#forURL - retrieve a map of HTTP verbs to RouteRecognizers
* for a given URL
*
* @param {String} url a URL
* @return {Registry} a map of HTTP verbs to RouteRecognizers
* corresponding to the provided URL's
* hostname and port
*/
Hosts.prototype.forURL = function(url) {
var host = parseURL(url).host;
var registry = this._registries[host];
if (registry === undefined) {
registry = (this._registries[host] = new Registry(host));
}
return registry.verbs;
};
function Pretender(/* routeMap1, routeMap2, ..., options*/) {
this.hosts = new Hosts();
var lastArg = arguments[arguments.length - 1];
var options = typeof lastArg === 'object' ? lastArg : null;
var shouldNotTrack = options && (options.trackRequests === false);
var noopArray = { push: function() {}, length: 0 };
this.handlers = [];
this.handledRequests = shouldNotTrack ? noopArray: [];
this.passthroughRequests = shouldNotTrack ? noopArray: [];
this.unhandledRequests = shouldNotTrack ? noopArray: [];
this.requestReferences = [];
this.forcePassthrough = options && (options.forcePassthrough === true);
this.disableUnhandled = options && (options.disableUnhandled === true);
// reference the native XMLHttpRequest object so
// it can be restored later
this._nativeXMLHttpRequest = self.XMLHttpRequest;
// capture xhr requests, channeling them into
// the route map.
self.XMLHttpRequest = interceptor(this, this._nativeXMLHttpRequest);
// 'start' the server
this.running = true;
// trigger the route map DSL.
var argLength = options ? arguments.length - 1 : arguments.length;
for (var i = 0; i < argLength; i++) {
this.map(arguments[i]);
}
}
function interceptor(pretender, nativeRequest) {
function FakeRequest() {
// super()
FakeXMLHttpRequest.call(this);
}
// extend
var proto = new FakeXMLHttpRequest();
proto.send = function send() {
if (!pretender.running) {
throw new Error('You shut down a Pretender instance while there was a pending request. ' +
'That request just tried to complete. Check to see if you accidentally shut down ' +
'a pretender earlier than you intended to');
}
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
if (pretender.checkPassthrough(this)) {
var xhr = createPassthrough(this);
xhr.send.apply(xhr, arguments);
} else {
pretender.handleRequest(this);
}
};
function createPassthrough(fakeXHR) {
// event types to handle on the xhr
var evts = ['error', 'timeout', 'abort', 'readystatechange'];
// event types to handle on the xhr.upload
var uploadEvents = [];
// properties to copy from the native xhr to fake xhr
var lifecycleProps = ['readyState', 'responseText', 'responseXML', 'status', 'statusText'];
var xhr = fakeXHR._passthroughRequest = new pretender._nativeXMLHttpRequest();
xhr.open(fakeXHR.method, fakeXHR.url, fakeXHR.async, fakeXHR.username, fakeXHR.password);
if (fakeXHR.responseType === 'arraybuffer') {
lifecycleProps = ['readyState', 'response', 'status', 'statusText'];
xhr.responseType = fakeXHR.responseType;
}
// use onload if the browser supports it
if ('onload' in xhr) {
evts.push('load');
}
// add progress event for async calls
// avoid using progress events for sync calls, they will hang https://bugs.webkit.org/show_bug.cgi?id=40996.
if (fakeXHR.async && fakeXHR.responseType !== 'arraybuffer') {
evts.push('progress');
uploadEvents.push('progress');
}
// update `propertyNames` properties from `fromXHR` to `toXHR`
function copyLifecycleProperties(propertyNames, fromXHR, toXHR) {
for (var i = 0; i < propertyNames.length; i++) {
var prop = propertyNames[i];
if (prop in fromXHR) {
toXHR[prop] = fromXHR[prop];
}
}
}
// fire fake event on `eventable`
function dispatchEvent(eventable, eventType, event) {
eventable.dispatchEvent(event);
if (eventable['on' + eventType]) {
eventable['on' + eventType](event);
}
}
// set the on- handler on the native xhr for the given eventType
function createHandler(eventType) {
xhr['on' + eventType] = function(event) {
copyLifecycleProperties(lifecycleProps, xhr, fakeXHR);
dispatchEvent(fakeXHR, eventType, event);
};
}
// set the on- handler on the native xhr's `upload` property for
// the given eventType
function createUploadHandler(eventType) {
if (xhr.upload) {
xhr.upload['on' + eventType] = function(event) {
dispatchEvent(fakeXHR.upload, eventType, event);
};
}
}
var i;
for (i = 0; i < evts.length; i++) {
createHandler(evts[i]);
}
for (i = 0; i < uploadEvents.length; i++) {
createUploadHandler(uploadEvents[i]);
}
if (fakeXHR.async) {
xhr.timeout = fakeXHR.timeout;
xhr.withCredentials = fakeXHR.withCredentials;
}
for (var h in fakeXHR.requestHeaders) {
xhr.setRequestHeader(h, fakeXHR.requestHeaders[h]);
}
return xhr;
}
proto._passthroughCheck = function(method, args) {
if (this._passthroughRequest) {
return this._passthroughRequest[method].apply(this._passthroughRequest, args);
}
return FakeXMLHttpRequest.prototype[method].apply(this, args);
};
proto.abort = function abort() {
return this._passthroughCheck('abort', arguments);
};
proto.getResponseHeader = function getResponseHeader() {
return this._passthroughCheck('getResponseHeader', arguments);
};
proto.getAllResponseHeaders = function getAllResponseHeaders() {
return this._passthroughCheck('getAllResponseHeaders', arguments);
};
FakeRequest.prototype = proto;
if (nativeRequest.prototype._passthroughCheck) {
console.warn('You created a second Pretender instance while there was already one running. ' +
'Running two Pretender servers at once will lead to unexpected results and will ' +
'be removed entirely in a future major version.' +
'Please call .shutdown() on your instances when you no longer need them to respond.');
}
return FakeRequest;
}
function verbify(verb) {
return function(path, handler, async) {
return this.register(verb, path, handler, async);
};
}
function scheduleProgressEvent(request, startTime, totalTime) {
setTimeout(function() {
if (!request.aborted && !request.status) {
var ellapsedTime = new Date().getTime() - startTime.getTime();
request.upload._progress(true, ellapsedTime, totalTime);
request._progress(true, ellapsedTime, totalTime);
scheduleProgressEvent(request, startTime, totalTime);
}
}, 50);
}
function isArray(array) {
return Object.prototype.toString.call(array) === '[object Array]';
}
var PASSTHROUGH = {};
Pretender.prototype = {
get: verbify('GET'),
post: verbify('POST'),
put: verbify('PUT'),
'delete': verbify('DELETE'),
patch: verbify('PATCH'),
head: verbify('HEAD'),
options: verbify('OPTIONS'),
map: function(maps) {
maps.call(this);
},
register: function register(verb, url, handler, async) {
if (!handler) {
throw new Error('The function you tried passing to Pretender to handle ' +
verb + ' ' + url + ' is undefined or missing.');
}
handler.numberOfCalls = 0;
handler.async = async;
this.handlers.push(handler);
var registry = this.hosts.forURL(url)[verb];
registry.add([{
path: parseURL(url).fullpath,
handler: handler
}]);
return handler;
},
passthrough: PASSTHROUGH,
checkPassthrough: function checkPassthrough(request) {
var verb = request.method.toUpperCase();
var path = parseURL(request.url).fullpath;
var recognized = this.hosts.forURL(request.url)[verb].recognize(path);
var match = recognized && recognized[0];
if ((match && match.handler === PASSTHROUGH) || this.forcePassthrough) {
this.passthroughRequests.push(request);
this.passthroughRequest(verb, path, request);
return true;
}
return false;
},
handleRequest: function handleRequest(request) {
var verb = request.method.toUpperCase();
var path = request.url;
var handler = this._handlerFor(verb, path, request);
if (handler) {
handler.handler.numberOfCalls++;
var async = handler.handler.async;
this.handledRequests.push(request);
var pretender = this;
var _handleRequest = function(statusHeadersAndBody) {
if (!isArray(statusHeadersAndBody)) {
var note = 'Remember to `return [status, headers, body];` in your route handler.';
throw new Error('Nothing returned by handler for ' + path + '. ' + note);
}
var status = statusHeadersAndBody[0],
headers = pretender.prepareHeaders(statusHeadersAndBody[1]),
body = pretender.prepareBody(statusHeadersAndBody[2], headers);
pretender.handleResponse(request, async, function() {
request.respond(status, headers, body);
pretender.handledRequest(verb, path, request);
});
};
try {
var result = handler.handler(request);
if (result && typeof result.then === 'function') {
// `result` is a promise, resolve it
result.then(function(resolvedResult) {
_handleRequest(resolvedResult);
});
} else {
_handleRequest(result);
}
} catch (error) {
this.erroredRequest(verb, path, request, error);
this.resolve(request);
}
} else {
if (!this.disableUnhandled) {
this.unhandledRequests.push(request);
this.unhandledRequest(verb, path, request);
}
}
},
handleResponse: function handleResponse(request, strategy, callback) {
var delay = typeof strategy === 'function' ? strategy() : strategy;
delay = typeof delay === 'boolean' || typeof delay === 'number' ? delay : 0;
if (delay === false) {
callback();
} else {
var pretender = this;
pretender.requestReferences.push({
request: request,
callback: callback
});
if (delay !== true) {
scheduleProgressEvent(request, new Date(), delay);
setTimeout(function() {
pretender.resolve(request);
}, delay);
}
}
},
resolve: function resolve(request) {
for (var i = 0, len = this.requestReferences.length; i < len; i++) {
var res = this.requestReferences[i];
if (res.request === request) {
res.callback();
this.requestReferences.splice(i, 1);
break;
}
}
},
requiresManualResolution: function(verb, path) {
var handler = this._handlerFor(verb.toUpperCase(), path, {});
if (!handler) { return false; }
var async = handler.handler.async;
return typeof async === 'function' ? async() === true : async === true;
},
prepareBody: function(body) { return body; },
prepareHeaders: function(headers) { return headers; },
handledRequest: function(/* verb, path, request */) { /* no-op */},
passthroughRequest: function(/* verb, path, request */) { /* no-op */},
unhandledRequest: function(verb, path/*, request */) {
throw new Error('Pretender intercepted ' + verb + ' ' +
path + ' but no handler was defined for this type of request');
},
erroredRequest: function(verb, path, request, error) {
error.message = 'Pretender intercepted ' + verb + ' ' +
path + ' but encountered an error: ' + error.message;
throw error;
},
_handlerFor: function(verb, url, request) {
var registry = this.hosts.forURL(url)[verb];
var matches = registry.recognize(parseURL(url).fullpath);
var match = matches ? matches[0] : null;
if (match) {
request.params = match.params;
request.queryParams = matches.queryParams;
}
return match;
},
shutdown: function shutdown() {
self.XMLHttpRequest = this._nativeXMLHttpRequest;
// 'stop' the server
this.running = false;
}
};
Pretender.parseURL = parseURL;
Pretender.Hosts = Hosts;
Pretender.Registry = Registry;
if (typeof module === 'object') {
module.exports = Pretender;
} else if (typeof define !== 'undefined') {
define('pretender', [], function() {
return Pretender;
});
}
self.Pretender = Pretender;
}(self));