2014-07-31 14:51:10 -04:00
|
|
|
(function(window){
|
|
|
|
|
|
|
|
var isNode = typeof process !== 'undefined' && process.toString() === '[object process]';
|
|
|
|
var RouteRecognizer = isNode ? require('route-recognizer')['default'] : window.RouteRecognizer;
|
|
|
|
var FakeXMLHttpRequest = isNode ? require('./bower_components/FakeXMLHttpRequest/fake_xml_http_request') : window.FakeXMLHttpRequest;
|
2015-04-30 16:04:58 -04:00
|
|
|
var slice = [].slice;
|
2014-07-31 14:51:10 -04:00
|
|
|
|
2015-04-30 16:04:58 -04:00
|
|
|
function Pretender(/* routeMap1, routeMap2, ...*/){
|
|
|
|
maps = slice.call(arguments);
|
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.
|
|
|
|
this.registry = {
|
|
|
|
GET: new RouteRecognizer(),
|
|
|
|
PUT: new RouteRecognizer(),
|
|
|
|
POST: new RouteRecognizer(),
|
|
|
|
DELETE: new RouteRecognizer(),
|
|
|
|
PATCH: new RouteRecognizer(),
|
|
|
|
HEAD: new RouteRecognizer()
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
this._nativeXMLHttpRequest = window.XMLHttpRequest;
|
|
|
|
|
|
|
|
// capture xhr requests, channeling them into
|
|
|
|
// the route map.
|
|
|
|
window.XMLHttpRequest = interceptor(this);
|
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
// "start" the server
|
|
|
|
this.running = true;
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
// trigger the route map DSL.
|
2015-04-30 16:04:58 -04:00
|
|
|
for(i=0; i < arguments.length; i++){
|
|
|
|
this.map(arguments[i]);
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function interceptor(pretender) {
|
|
|
|
function FakeRequest(){
|
|
|
|
// super()
|
|
|
|
FakeXMLHttpRequest.call(this);
|
|
|
|
}
|
|
|
|
// extend
|
|
|
|
var proto = new FakeXMLHttpRequest();
|
|
|
|
proto.send = function send(){
|
2015-03-13 12:08:28 -04:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var xhr = createPassthrough(this);
|
|
|
|
xhr.send.apply(xhr, arguments);
|
|
|
|
}
|
2014-07-31 14:51:10 -04:00
|
|
|
};
|
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
// passthrough handling
|
|
|
|
var evts = ['load', 'error', 'timeout', 'progress', 'abort', 'readystatechange'];
|
|
|
|
var lifecycleProps = ['readyState', 'responseText', 'responseXML', 'status', 'statusText'];
|
|
|
|
function createPassthrough(fakeXHR) {
|
|
|
|
var xhr = fakeXHR._passthroughRequest = new pretender._nativeXMLHttpRequest();
|
|
|
|
// listen to all events to update lifecycle properties
|
|
|
|
for (var i = 0; i < evts.length; i++) (function(evt) {
|
|
|
|
xhr['on' + evt] = function(e) {
|
|
|
|
// update lifecycle props on each event
|
|
|
|
for (var i = 0; i < lifecycleProps.length; i++) {
|
|
|
|
var prop = lifecycleProps[i];
|
|
|
|
if (xhr[prop]) {
|
|
|
|
fakeXHR[prop] = xhr[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fire fake events where applicable
|
|
|
|
fakeXHR.dispatchEvent(evt, e);
|
|
|
|
if (fakeXHR['on' + evt]) {
|
|
|
|
fakeXHR['on' + evt](e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(evts[i]);
|
|
|
|
xhr.open(fakeXHR.method, fakeXHR.url, fakeXHR.async, fakeXHR.username, fakeXHR.password);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
proto._passthroughCheck = function(method, arguments) {
|
|
|
|
if (this._passthroughRequest) {
|
|
|
|
return this._passthroughRequest[method].apply(this._passthroughRequest, arguments);
|
|
|
|
}
|
|
|
|
return FakeXMLHttpRequest.prototype[method].apply(this, arguments);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:51:10 -04:00
|
|
|
FakeRequest.prototype = proto;
|
|
|
|
return FakeRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
function verbify(verb){
|
2015-04-30 16:04:58 -04:00
|
|
|
return function(path, handler, async){
|
|
|
|
this.register(verb, path, handler, async);
|
2014-07-31 14:51:10 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-13 12:08:28 -04:00
|
|
|
function throwIfURLDetected(url){
|
|
|
|
var HTTP_REGEXP = /^https?/;
|
|
|
|
var message;
|
|
|
|
|
|
|
|
if(HTTP_REGEXP.test(url)) {
|
|
|
|
var parser = window.document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
|
|
|
|
message = "Pretender will not respond to requests for URLs. It is not possible to accurately simluate the browser's CSP. "+
|
|
|
|
"Remove the " + parser.protocol +"//"+ parser.hostname +" from " + url + " and try again";
|
|
|
|
throw new Error(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'),
|
2015-04-30 16:04:58 -04:00
|
|
|
map: function(maps){
|
|
|
|
maps.call(this);
|
|
|
|
},
|
|
|
|
register: function register(verb, path, handler, async){
|
|
|
|
if (!handler) {
|
|
|
|
throw new Error("The function you tried passing to Pretender to handle " + verb + " " + path + " is undefined or missing.");
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
var registry = this.registry[verb];
|
|
|
|
registry.add([{path: path, handler: handler}]);
|
|
|
|
},
|
2015-03-13 12:08:28 -04:00
|
|
|
passthrough: PASSTHROUGH,
|
|
|
|
checkPassthrough: function(request) {
|
|
|
|
var verb = request.method.toUpperCase();
|
|
|
|
var path = request.url;
|
|
|
|
|
|
|
|
throwIfURLDetected(path);
|
|
|
|
|
|
|
|
verb = verb.toUpperCase();
|
|
|
|
|
|
|
|
var recognized = this.registry[verb].recognize(path);
|
|
|
|
var match = recognized && recognized[0];
|
|
|
|
if (match && match.handler == PASSTHROUGH) {
|
|
|
|
this.passthroughRequests.push(request);
|
|
|
|
this.passthroughRequest(verb, path, request);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
2014-07-31 14:51:10 -04:00
|
|
|
handleRequest: function handleRequest(request){
|
|
|
|
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);
|
|
|
|
|
|
|
|
try {
|
|
|
|
var statusHeadersAndBody = handler.handler(request),
|
|
|
|
status = statusHeadersAndBody[0],
|
2015-03-13 12:08:28 -04:00
|
|
|
headers = this.prepareHeaders(statusHeadersAndBody[1]),
|
2015-04-30 16:04:58 -04:00
|
|
|
body = this.prepareBody(statusHeadersAndBody[2]),
|
|
|
|
pretender = this;
|
2015-03-13 12:08:28 -04:00
|
|
|
|
2015-04-30 16:04:58 -04:00
|
|
|
this.handleResponse(request, async, function() {
|
|
|
|
request.respond(status, headers, body);
|
|
|
|
pretender.handledRequest(verb, path, request);
|
|
|
|
});
|
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) {
|
|
|
|
strategy = typeof strategy === 'function' ? strategy() : strategy;
|
|
|
|
|
|
|
|
if (strategy === false) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
var pretender = this;
|
|
|
|
pretender.requestReferences.push({
|
|
|
|
request: request,
|
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
|
|
|
|
if (strategy !== true) {
|
|
|
|
setTimeout(function() {
|
|
|
|
pretender.resolve(request);
|
|
|
|
}, typeof strategy === 'number' ? strategy : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
},
|
2015-03-13 12:08:28 -04:00
|
|
|
prepareBody: function(body) { return body; },
|
|
|
|
prepareHeaders: function(headers) { return headers; },
|
|
|
|
handledRequest: function(verb, path, request) { /* no-op */},
|
|
|
|
passthroughRequest: function(verb, path, request) { /* no-op */},
|
2014-07-31 14:51:10 -04:00
|
|
|
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, path, request){
|
|
|
|
var registry = this.registry[verb];
|
|
|
|
var matches = registry.recognize(path);
|
|
|
|
|
|
|
|
var match = matches ? matches[0] : null;
|
|
|
|
if (match) {
|
|
|
|
request.params = match.params;
|
|
|
|
request.queryParams = matches.queryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
},
|
|
|
|
shutdown: function shutdown(){
|
|
|
|
window.XMLHttpRequest = this._nativeXMLHttpRequest;
|
2015-03-13 12:08:28 -04:00
|
|
|
|
|
|
|
// "stop" the server
|
|
|
|
this.running = false;
|
2014-07-31 14:51:10 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isNode) {
|
|
|
|
module.exports = Pretender;
|
|
|
|
} else {
|
|
|
|
window.Pretender = Pretender;
|
|
|
|
}
|
|
|
|
|
|
|
|
})(window);
|