2017-02-17 16:15:36 -05:00
|
|
|
(function(self) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var appearsBrowserified = typeof self !== 'undefined' &&
|
|
|
|
typeof process !== 'undefined' &&
|
|
|
|
Object.prototype.toString.call(process) === '[object Object]';
|
|
|
|
|
|
|
|
var RouteRecognizer = appearsBrowserified ? require('route-recognizer') : self.RouteRecognizer;
|
|
|
|
var FakeXMLHttpRequest = appearsBrowserified ? 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 || '')
|
|
|
|
};
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
/**
|
|
|
|
* Registry
|
|
|
|
*
|
|
|
|
* A registry is a map of HTTP verbs to route recognizers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Registry(/* host */) {
|
2014-07-31 14:51:10 -04:00
|
|
|
// Herein we keep track of RouteRecognizer instances
|
|
|
|
// keyed by HTTP method. Feel free to add more as needed.
|
2017-02-17 16:15:36 -05:00
|
|
|
this.verbs = {
|
2014-07-31 14:51:10 -04:00
|
|
|
GET: new RouteRecognizer(),
|
|
|
|
PUT: new RouteRecognizer(),
|
|
|
|
POST: new RouteRecognizer(),
|
|
|
|
DELETE: new RouteRecognizer(),
|
|
|
|
PATCH: new RouteRecognizer(),
|
2017-02-17 16:15:36 -05:00
|
|
|
HEAD: new RouteRecognizer(),
|
|
|
|
OPTIONS: new RouteRecognizer()
|
2014-07-31 14:51:10 -04:00
|
|
|
};
|
2017-02-17 16:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, ...*/) {
|
|
|
|
this.hosts = new Hosts();
|
2014-07-31 14:51:10 -04:00
|
|
|
|
|
|
|
this.handlers = [];
|
|
|
|
this.handledRequests = [];
|
2015-03-13 12:08:28 -04:00
|
|
|
this.passthroughRequests = [];
|
2014-07-31 14:51:10 -04:00
|
|
|
this.unhandledRequests = [];
|
2015-04-30 16:04:58 -04:00
|
|
|
this.requestReferences = [];
|
2014-07-31 14:51:10 -04:00
|
|
|
|
|
|
|
// reference the native XMLHttpRequest object so
|
|
|
|
// it can be restored later
|
2017-02-17 16:15:36 -05:00
|
|
|
this._nativeXMLHttpRequest = self.XMLHttpRequest;
|
2014-07-31 14:51:10 -04:00
|
|
|
|
|
|
|
// capture xhr requests, channeling them into
|
|
|
|
// the route map.
|
2017-02-17 16:15:36 -05:00
|
|
|
self.XMLHttpRequest = interceptor(this, this._nativeXMLHttpRequest);
|
2014-07-31 14:51:10 -04:00
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
// 'start' the server
|
2015-03-13 12:08:28 -04:00
|
|
|
this.running = true;
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
// trigger the route map DSL.
|
2017-02-17 16:15:36 -05:00
|
|
|
for (var i = 0; i < arguments.length; i++) {
|
2015-04-30 16:04:58 -04:00
|
|
|
this.map(arguments[i]);
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
function interceptor(pretender, nativeRequest) {
|
|
|
|
function FakeRequest() {
|
2014-07-31 14:51:10 -04:00
|
|
|
// super()
|
|
|
|
FakeXMLHttpRequest.call(this);
|
|
|
|
}
|
|
|
|
// extend
|
|
|
|
var proto = new FakeXMLHttpRequest();
|
2017-02-17 16:15:36 -05:00
|
|
|
proto.send = function send() {
|
2015-03-13 12:08:28 -04:00
|
|
|
if (!pretender.running) {
|
2017-02-17 16:15:36 -05:00
|
|
|
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 ' +
|
2015-03-13 12:08:28 -04:00
|
|
|
'a pretender earlier than you intended to');
|
|
|
|
}
|
|
|
|
|
2015-04-30 16:04:58 -04:00
|
|
|
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
|
2015-03-13 12:08:28 -04:00
|
|
|
if (!pretender.checkPassthrough(this)) {
|
|
|
|
pretender.handleRequest(this);
|
2017-02-17 16:15:36 -05:00
|
|
|
} else {
|
2015-03-13 12:08:28 -04:00
|
|
|
var xhr = createPassthrough(this);
|
|
|
|
xhr.send.apply(xhr, arguments);
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
};
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
function createPassthrough(fakeXHR) {
|
2017-02-17 16:15:36 -05:00
|
|
|
// 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'];
|
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
var xhr = fakeXHR._passthroughRequest = new pretender._nativeXMLHttpRequest();
|
2017-02-17 16:15:36 -05:00
|
|
|
|
|
|
|
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 (fromXHR[prop]) {
|
|
|
|
toXHR[prop] = fromXHR[prop];
|
2015-03-13 12:08:28 -04:00
|
|
|
}
|
2017-02-17 16:15:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2015-03-13 12:08:28 -04:00
|
|
|
};
|
2017-02-17 16:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
xhr.open(fakeXHR.method, fakeXHR.url, fakeXHR.async, fakeXHR.username, fakeXHR.password);
|
2017-02-17 16:15:36 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-04-30 16:04:58 -04:00
|
|
|
for (var h in fakeXHR.requestHeaders) {
|
|
|
|
xhr.setRequestHeader(h, fakeXHR.requestHeaders[h]);
|
|
|
|
}
|
2015-03-13 12:08:28 -04:00
|
|
|
return xhr;
|
|
|
|
}
|
2017-02-17 16:15:36 -05:00
|
|
|
|
|
|
|
proto._passthroughCheck = function(method, args) {
|
2015-03-13 12:08:28 -04:00
|
|
|
if (this._passthroughRequest) {
|
2017-02-17 16:15:36 -05:00
|
|
|
return this._passthroughRequest[method].apply(this._passthroughRequest, args);
|
2015-03-13 12:08:28 -04:00
|
|
|
}
|
2017-02-17 16:15:36 -05:00
|
|
|
return FakeXMLHttpRequest.prototype[method].apply(this, args);
|
|
|
|
};
|
|
|
|
|
|
|
|
proto.abort = function abort() {
|
2015-03-13 12:08:28 -04:00
|
|
|
return this._passthroughCheck('abort', arguments);
|
2017-02-17 16:15:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
proto.getResponseHeader = function getResponseHeader() {
|
2015-03-13 12:08:28 -04:00
|
|
|
return this._passthroughCheck('getResponseHeader', arguments);
|
2017-02-17 16:15:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
proto.getAllResponseHeaders = function getAllResponseHeaders() {
|
2015-03-13 12:08:28 -04:00
|
|
|
return this._passthroughCheck('getAllResponseHeaders', arguments);
|
2017-02-17 16:15:36 -05:00
|
|
|
};
|
2015-03-13 12:08:28 -04:00
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
FakeRequest.prototype = proto;
|
2017-02-17 16:15:36 -05:00
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
return FakeRequest;
|
|
|
|
}
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
function verbify(verb) {
|
|
|
|
return function(path, handler, async) {
|
|
|
|
return this.register(verb, path, handler, async);
|
2014-07-31 14:51:10 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
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);
|
|
|
|
}
|
2015-03-13 12:08:28 -04:00
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
function isArray(array) {
|
|
|
|
return Object.prototype.toString.call(array) === '[object Array]';
|
2015-03-13 12:08:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var PASSTHROUGH = {};
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
Pretender.prototype = {
|
|
|
|
get: verbify('GET'),
|
|
|
|
post: verbify('POST'),
|
|
|
|
put: verbify('PUT'),
|
|
|
|
'delete': verbify('DELETE'),
|
|
|
|
patch: verbify('PATCH'),
|
|
|
|
head: verbify('HEAD'),
|
2017-02-17 16:15:36 -05:00
|
|
|
options: verbify('OPTIONS'),
|
|
|
|
map: function(maps) {
|
2015-04-30 16:04:58 -04:00
|
|
|
maps.call(this);
|
|
|
|
},
|
2017-02-17 16:15:36 -05:00
|
|
|
register: function register(verb, url, handler, async) {
|
2015-04-30 16:04:58 -04:00
|
|
|
if (!handler) {
|
2017-02-17 16:15:36 -05:00
|
|
|
throw new Error('The function you tried passing to Pretender to handle ' +
|
|
|
|
verb + ' ' + url + ' is undefined or missing.');
|
2015-04-30 16:04:58 -04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
handler.numberOfCalls = 0;
|
2015-04-30 16:04:58 -04:00
|
|
|
handler.async = async;
|
2014-07-31 14:51:10 -04:00
|
|
|
this.handlers.push(handler);
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
var registry = this.hosts.forURL(url)[verb];
|
|
|
|
|
|
|
|
registry.add([{
|
|
|
|
path: parseURL(url).fullpath,
|
|
|
|
handler: handler
|
|
|
|
}]);
|
|
|
|
|
|
|
|
return handler;
|
2014-07-31 14:51:10 -04:00
|
|
|
},
|
2015-03-13 12:08:28 -04:00
|
|
|
passthrough: PASSTHROUGH,
|
2017-02-17 16:15:36 -05:00
|
|
|
checkPassthrough: function checkPassthrough(request) {
|
2015-03-13 12:08:28 -04:00
|
|
|
var verb = request.method.toUpperCase();
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
var path = parseURL(request.url).fullpath;
|
2015-03-13 12:08:28 -04:00
|
|
|
|
|
|
|
verb = verb.toUpperCase();
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
var recognized = this.hosts.forURL(request.url)[verb].recognize(path);
|
2015-03-13 12:08:28 -04:00
|
|
|
var match = recognized && recognized[0];
|
2017-02-17 16:15:36 -05:00
|
|
|
if (match && match.handler === PASSTHROUGH) {
|
2015-03-13 12:08:28 -04:00
|
|
|
this.passthroughRequests.push(request);
|
|
|
|
this.passthroughRequest(verb, path, request);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
2017-02-17 16:15:36 -05:00
|
|
|
handleRequest: function handleRequest(request) {
|
2014-07-31 14:51:10 -04:00
|
|
|
var verb = request.method.toUpperCase();
|
|
|
|
var path = request.url;
|
2015-03-13 12:08:28 -04:00
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
var handler = this._handlerFor(verb, path, request);
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
handler.handler.numberOfCalls++;
|
2015-04-30 16:04:58 -04:00
|
|
|
var async = handler.handler.async;
|
2014-07-31 14:51:10 -04:00
|
|
|
this.handledRequests.push(request);
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
var pretender = this;
|
2015-03-13 12:08:28 -04:00
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
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() {
|
2015-04-30 16:04:58 -04:00
|
|
|
request.respond(status, headers, body);
|
|
|
|
pretender.handledRequest(verb, path, request);
|
|
|
|
});
|
2017-02-17 16:15:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
} catch (error) {
|
|
|
|
this.erroredRequest(verb, path, request, error);
|
2015-04-30 16:04:58 -04:00
|
|
|
this.resolve(request);
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.unhandledRequests.push(request);
|
|
|
|
this.unhandledRequest(verb, path, request);
|
|
|
|
}
|
|
|
|
},
|
2015-04-30 16:04:58 -04:00
|
|
|
handleResponse: function handleResponse(request, strategy, callback) {
|
2017-02-17 16:15:36 -05:00
|
|
|
var delay = typeof strategy === 'function' ? strategy() : strategy;
|
|
|
|
delay = typeof delay === 'boolean' || typeof delay === 'number' ? delay : 0;
|
2015-04-30 16:04:58 -04:00
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
if (delay === false) {
|
2015-04-30 16:04:58 -04:00
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
var pretender = this;
|
|
|
|
pretender.requestReferences.push({
|
|
|
|
request: request,
|
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
if (delay !== true) {
|
|
|
|
scheduleProgressEvent(request, new Date(), delay);
|
2015-04-30 16:04:58 -04:00
|
|
|
setTimeout(function() {
|
|
|
|
pretender.resolve(request);
|
2017-02-17 16:15:36 -05:00
|
|
|
}, delay);
|
2015-04-30 16:04:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve: function resolve(request) {
|
2017-02-17 16:15:36 -05:00
|
|
|
for (var i = 0, len = this.requestReferences.length; i < len; i++) {
|
2015-04-30 16:04:58 -04:00
|
|
|
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;
|
|
|
|
},
|
2015-03-13 12:08:28 -04:00
|
|
|
prepareBody: function(body) { return body; },
|
|
|
|
prepareHeaders: function(headers) { return headers; },
|
2017-02-17 16:15:36 -05:00
|
|
|
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');
|
2014-07-31 14:51:10 -04:00
|
|
|
},
|
2017-02-17 16:15:36 -05:00
|
|
|
erroredRequest: function(verb, path, request, error) {
|
|
|
|
error.message = 'Pretender intercepted ' + verb + ' ' +
|
|
|
|
path + ' but encountered an error: ' + error.message;
|
2014-07-31 14:51:10 -04:00
|
|
|
throw error;
|
|
|
|
},
|
2017-02-17 16:15:36 -05:00
|
|
|
_handlerFor: function(verb, url, request) {
|
|
|
|
var registry = this.hosts.forURL(url)[verb];
|
|
|
|
var matches = registry.recognize(parseURL(url).fullpath);
|
2014-07-31 14:51:10 -04:00
|
|
|
|
|
|
|
var match = matches ? matches[0] : null;
|
|
|
|
if (match) {
|
|
|
|
request.params = match.params;
|
|
|
|
request.queryParams = matches.queryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
},
|
2017-02-17 16:15:36 -05:00
|
|
|
shutdown: function shutdown() {
|
|
|
|
self.XMLHttpRequest = this._nativeXMLHttpRequest;
|
2015-03-13 12:08:28 -04:00
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
// 'stop' the server
|
2015-03-13 12:08:28 -04:00
|
|
|
this.running = false;
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-17 16:15:36 -05:00
|
|
|
Pretender.parseURL = parseURL;
|
|
|
|
Pretender.Hosts = Hosts;
|
|
|
|
Pretender.Registry = Registry;
|
|
|
|
|
|
|
|
if (typeof module === 'object') {
|
2014-07-31 14:51:10 -04:00
|
|
|
module.exports = Pretender;
|
2017-02-17 16:15:36 -05:00
|
|
|
} else if (typeof define !== 'undefined') {
|
|
|
|
define('pretender', [], function() {
|
|
|
|
return Pretender;
|
|
|
|
});
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
2017-02-17 16:15:36 -05:00
|
|
|
self.Pretender = Pretender;
|
|
|
|
}(self));
|