Skip to content

Commit

Permalink
Added better handling of path var recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
bbyars committed Mar 17, 2017
1 parent 0949f7e commit f8d33f8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.iml
node_modules/
test-swagger.json
TODO
83 changes: 58 additions & 25 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function type (obj) {
else if (obj === null) {
return 'null';
}
else if (obj === 'true' || obj === 'false') {
return 'boolean';
}
else {
return typeof obj;
}
Expand All @@ -41,16 +44,16 @@ function intercept (obj, fn, interceptor, resultProcessor) {
};
}

function capture (baseURL, filename) {
var host = url.parse(baseURL).host,
basePath = url.parse(baseURL).pathname;
function capture (options) {
var host = url.parse(options.baseURL).host,
basePath = url.parse(options.baseURL).pathname;

function read () {
return JSON.parse(fs.readFileSync(filename));
return JSON.parse(fs.readFileSync(options.filename));
}

function write (spec) {
fs.writeFileSync(filename, JSON.stringify(spec, null, 4));
fs.writeFileSync(options.filename, JSON.stringify(spec, null, 4));
}

function withSpec (handler) {
Expand All @@ -59,27 +62,31 @@ function capture (baseURL, filename) {
write(spec);
}

function shouldCapture (options) {
var requestHost = options.hostname || options.host || 'localhost';
if (options.port) {
requestHost += ':' + options.port;
}
return requestHost.toLowerCase() === host.toLowerCase() &&
options.path.indexOf(basePath) === 0;
}

function isTemplated (path) {
return templatePattern.test(path);
}

function templatize (path) {
return util.format('/%s/{id}', path.match(templatePattern)[1]);
}

function getTemplateIdFrom (path) {
return path.match(templatePattern)[2];
}

function getPathParameterNameFrom (path) {
var base = '/' + path.match(templatePattern)[1],
match = options.paths.find(function (givenPath) {
return isTemplated(givenPath) && givenPath.indexOf(base) === 0;
});
if (match) {
return getTemplateIdFrom(match);
}
else {
return 'id';
}
}

function templatize (path) {
return util.format('/%s/%s', path.match(templatePattern)[1], getPathParameterNameFrom(path));
}

function getPathFrom (requestOptions) {
var parts = url.parse(requestOptions.path, true),
path = parts.pathname.replace(basePath, '/');
Expand All @@ -95,6 +102,18 @@ function capture (baseURL, filename) {
return url.parse(requestOptions.path, true).query;
}

function shouldCapture (requestOptions) {
var requestHost = requestOptions.hostname || requestOptions.host || 'localhost',
path = getPathFrom(requestOptions);

if (requestOptions.port) {
requestHost += ':' + requestOptions.port;
}
return requestHost.toLowerCase() === host.toLowerCase() &&
requestOptions.path.indexOf(basePath) === 0 &&
options.paths.indexOf(path) >= 0;
}

function getPathSpec (path, spec) {
if (!defined(spec.paths[path])) {
spec.paths[path] = {};
Expand Down Expand Up @@ -241,12 +260,23 @@ function capture (baseURL, filename) {

var currentRequest = {};

intercept(http, 'request', function (options, callback) {
if (typeof options === 'string') {
options = url.parse(options);
intercept(http, 'request', function (requestOptions, callback) {
if (typeof requestOptions === 'string') {
requestOptions = url.parse(requestOptions);
}

if (!shouldCapture(options)) {
if (!shouldCapture(requestOptions)) {
if (requestOptions.path.indexOf('http://localhost:2525/imposters/') === 0) {
var requestHost = requestOptions.hostname || requestOptions.host || 'localhost',
path = getPathFrom(requestOptions);

if (requestOptions.port) {
requestHost += ':' + requestOptions.port;
}
console.log('REQUESTHOST: ' + requestHost + ', HOST: ' + host);
console.log('PATH: ' + path + ', BASEPATH: ' + basePath);
console.log(JSON.stringify(options.paths));
}
return;
}

Expand Down Expand Up @@ -275,7 +305,10 @@ function capture (baseURL, filename) {

ensureParametersAdded(query, 'query', operationSpec.parameters);
if (isTemplated(path)) {
ensureParametersAdded({ id: getTemplateIdFrom(path) }, 'path', operationSpec.parameters);
var params = {};
params[getPathParameterNameFrom(path)] = getTemplateIdFrom(
url.parse(currentRequest.options.path).pathname.replace(basePath, '/'));
ensureParametersAdded(params, 'path', operationSpec.parameters);
}

if (isJSON(currentRequest.body)) {
Expand All @@ -293,10 +326,10 @@ function capture (baseURL, filename) {
};

// Save for next call
currentRequest = { options: options, body: '' };
currentRequest = { options: requestOptions, body: '' };

// Return changed callback
return [options, callbackWithInterceptor];
return [requestOptions, callbackWithInterceptor];
}, function (request) {
intercept(request, 'write', function (body) {
currentRequest.body = body;
Expand Down

0 comments on commit f8d33f8

Please sign in to comment.