Comparing version 0.4.1 to 0.5.0
@@ -10,10 +10,13 @@ "use strict"; | ||
/** | ||
* Mutate the request to enhance it with pattern matching and routing and cool stuff | ||
* Mutate the request to enhance it : | ||
* - pattern matching : match site.com/foo/{id} and site.com/foo/1 to return { id: 1 } in the route bag | ||
* - extract URL arguments (foo=bar&qux) into the queryBag | ||
* - fill in the request bag | ||
* @param request | ||
* @param pattern | ||
* @param url | ||
*/ | ||
function enhanceRequest(request, pattern) { | ||
function enhanceRequest(request, url) { | ||
var queryBag = Object.assign({}, (0, _routingUtilitary.extractURLArgs)(request.url), request.query), | ||
requestBag = {}, | ||
routeBag = (0, _routingUtilitary.extractRouteParameters)(pattern.url, request.url); | ||
routeBag = (0, _routingUtilitary.applyTemplateToUrl)(url, request.url); | ||
@@ -29,6 +32,2 @@ // In case of anything but a GET request, we fill the request variable with the body's data | ||
} | ||
/** | ||
* Copyright (c) 2017-present, Evosphere. | ||
* All rights reserved. | ||
*/ | ||
@@ -54,2 +53,8 @@ /** | ||
*/ | ||
/** | ||
* Copyright (c) 2017-present, Evosphere. | ||
* All rights reserved. | ||
*/ | ||
function Faussaire() { | ||
@@ -90,3 +95,3 @@ this.waiters = []; | ||
if (typeof correspondingWaiter.action === "function") { | ||
enhanceRequest(request, correspondingWaiter); | ||
enhanceRequest(request, correspondingWaiter.url); | ||
@@ -113,2 +118,21 @@ response = correspondingWaiter.action(request); | ||
var defaultResults = { | ||
timeout: { | ||
response: null, | ||
status: "timeout" | ||
}, | ||
networkError: { | ||
response: null, | ||
status: "network-error" | ||
}, | ||
notFound: { | ||
response: { | ||
statusCode: 404, | ||
data: null, | ||
headers: null | ||
}, | ||
status: "done" | ||
} | ||
}; | ||
/** | ||
@@ -119,6 +143,3 @@ * Create a generic timeout result | ||
Faussaire.createTimeoutResult = function () { | ||
return { | ||
response: null, | ||
status: "timeout" | ||
}; | ||
return defaultResults.timeout; | ||
}; | ||
@@ -131,6 +152,3 @@ | ||
Faussaire.createNetworkErrorResult = function () { | ||
return { | ||
response: null, | ||
status: "network-error" | ||
}; | ||
return defaultResults.networkError; | ||
}; | ||
@@ -143,10 +161,3 @@ | ||
Faussaire.createNotFoundResult = function () { | ||
return { | ||
response: { | ||
statusCode: 404, | ||
data: null, | ||
headers: null | ||
}, | ||
status: "done" | ||
}; | ||
return defaultResults.notFound; | ||
}; | ||
@@ -153,0 +164,0 @@ |
@@ -26,3 +26,10 @@ "use strict"; | ||
var splitURIAndArgs = exports.splitURIAndArgs = function splitURIAndArgs(url) { | ||
/** | ||
* return an object containing the path and the args of an URL | ||
* e.g : site.com/index?foo=bar would make { uri: site.com/index, args: foo=bar } | ||
* | ||
* @param url | ||
* @returns {{uri: string, args: string}} | ||
*/ | ||
var splitPathAndArgs = exports.splitPathAndArgs = function splitPathAndArgs(url) { | ||
var splitUrl = url.split('?'); | ||
@@ -48,10 +55,9 @@ | ||
var extractURLArgs = exports.extractURLArgs = function extractURLArgs(url) { | ||
var urlObject = splitURIAndArgs(url), | ||
obj = {}; | ||
var urlObject = splitPathAndArgs(url), | ||
args = {}; | ||
if (urlObject.args) { | ||
var pairs = urlObject.args.split('&'); | ||
if (pairs.length === 0) { | ||
return obj; | ||
return args; | ||
} | ||
@@ -61,6 +67,6 @@ | ||
var keyValue = pair.split("="); | ||
obj[keyValue[0]] = keyValue[1]; | ||
args[keyValue[0]] = keyValue[1]; | ||
}); | ||
return obj; | ||
return args; | ||
} | ||
@@ -72,3 +78,3 @@ | ||
/** | ||
* Extract routing parameters | ||
* Apply the template to the url to return captured values | ||
* | ||
@@ -83,3 +89,3 @@ * From template such as http://url.com/post/{id} | ||
*/ | ||
var extractRouteParameters = exports.extractRouteParameters = function extractRouteParameters(template, url) { | ||
var applyTemplateToUrl = exports.applyTemplateToUrl = function applyTemplateToUrl(template, url) { | ||
var keys = []; | ||
@@ -86,0 +92,0 @@ |
{ | ||
"name": "faussaire", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Lightweight library to mock API for testing purpose", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,2 +0,2 @@ | ||
# Faussaire v0.4 - Instable - Documentation not up to date | ||
# Faussaire v0.5 - Instable - Documentation not up to date | ||
Lightweight javascript library to mock network request for testing purposes |
103467
226