Skip to content

Commit

Permalink
Got generated swagger to pass muster with swagger-diff
Browse files Browse the repository at this point in the history
bbyars committed Mar 27, 2017
1 parent cb97246 commit 74e8926
Showing 6 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion functionalTest/httpInterceptorTest.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ var assert = require('assert'),
httpClient = require('./httpClient'),
httpServer = require('./httpServer');

describe.only('httpInterceptor', function () {
describe('httpInterceptor', function () {
describe('#_intercept', function () {
promiseIt('should translate request and response into simpler structure', function () {
var interceptedRequest, interceptedResponse, server, interception;
2 changes: 1 addition & 1 deletion src/contract.js
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ function create (templatizer) {

function ensureResponseAdded (response, responsesSpec) {
if (!defined(responsesSpec[response.statusCode])) {
responsesSpec[response.statusCode] = { schema: {} };
responsesSpec[response.statusCode] = { schema: {}, description: '' };
}

if (defined(response.body)) {
48 changes: 23 additions & 25 deletions src/defacto.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ var Templatizer = require('./templatizer'),
function capture (options) {
var host = url.parse(options.baseURL).host,
basePath = url.parse(options.baseURL).pathname,
templatizer = Templatizer.create(options.possiblePaths),
templatizer = Templatizer.create(options.paths),
contract = Contract.create(templatizer);

function read () {
@@ -21,31 +21,13 @@ function capture (options) {
fs.writeFileSync(options.filename, JSON.stringify(spec, null, 4));
}

function withSpec (handler) {
var spec = read();
handler(spec);
write(spec);
}

function getPathFrom (requestOptions) {
var parts = url.parse(requestOptions.path, true),
function getPathFrom (request) {
var parts = url.parse(request.path, true),
path = parts.pathname.replace(basePath, '/');

return templatizer.parse(path).template;
}

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

if (request.port && request.port !== 80) {
requestHost += ':' + request.port;
}
return requestHost.toLowerCase() === host.toLowerCase() &&
request.path.indexOf(basePath) === 0 &&
options.possiblePaths.indexOf(path) >= 0;
}

function isJSON (str) {
try {
JSON.parse(str);
@@ -56,15 +38,31 @@ function capture (options) {
}
}

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

return requestHost.toLowerCase() === host.toLowerCase() &&
request.path.indexOf(basePath) === 0 &&
options.paths.indexOf(path) >= 0 &&
isJSON(response.body);
}

interceptor.intercept(function (request, response) {
if (!shouldCapture(request)) {
if (!shouldCapture(request, response)) {
return;
}

withSpec(function (spec) {
contract.merge(spec, request, response);
});
if (isJSON(request.body)) {
request.body = JSON.parse(request.body);
}
if (isJSON(response.body)) {
response.body = JSON.parse(response.body);
}

var spec = read();
contract.merge(spec, request, response);
write(spec);
});

// Initialize with bare-bones spec
8 changes: 6 additions & 2 deletions src/templatizer.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ function create (possiblePaths) {
patterns[path] = {
namePattern: new RegExp(path.replace(templatePattern, '(\\$1)')),
valuePattern: new RegExp('^' + path.replace(templatePattern, '([^/]+)') + '$')
}
};
});

function findTemplateFor (path) {
@@ -18,6 +18,10 @@ function create (possiblePaths) {
}

function parametersFor (path) {
if (!test(path)) {
return {};
}

var template = findTemplateFor(path),
names = patterns[template].namePattern.exec(template),
values = patterns[template].valuePattern.exec(path),
@@ -36,7 +40,7 @@ function create (possiblePaths) {

function parse (path) {
return {
template: findTemplateFor(path) || path,
template: findTemplateFor(path) || '',
parameters: parametersFor(path)
};
}
15 changes: 8 additions & 7 deletions test/contractTest.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ describe('contract', function () {
get: {
parameters: [],
responses: {
'200': { schema: {} }
'200': { schema: {}, description: '' }
}
}
}
@@ -41,7 +41,7 @@ describe('contract', function () {
get: {
parameters: [],
responses: {
'200': { schema: {} }
'200': { schema: {}, description: '' }
}
}
}
@@ -72,7 +72,7 @@ describe('contract', function () {
type: 'string'
}
],
responses: { '200': { schema: {} } }
responses: { '200': { schema: {}, description: '' } }
}
}
}
@@ -102,7 +102,7 @@ describe('contract', function () {
type: 'string'
}
],
responses: { '200': { schema: {} } }
responses: { '200': { schema: {}, description: '' } }
}
}
}
@@ -137,7 +137,7 @@ describe('contract', function () {
}
}
],
responses: { '200': { schema: {} } }
responses: { '200': { schema: {}, description: '' } }
}
}
}
@@ -184,7 +184,7 @@ describe('contract', function () {
}
}
],
responses: { '200': { schema: {} } }
responses: { '200': { schema: {}, description: '' } }
}
}
}
@@ -232,7 +232,7 @@ describe('contract', function () {
}
}
],
responses: { '200': { schema: {} } }
responses: { '200': { schema: {}, description: '' } }
}
}
}
@@ -256,6 +256,7 @@ describe('contract', function () {
parameters: [],
responses: {
'200': {
description: '',
schema: {
type: 'object',
properties: {
6 changes: 6 additions & 0 deletions test/templatizerTest.js
Original file line number Diff line number Diff line change
@@ -45,5 +45,11 @@ describe('templatizer', function () {
parameters: { id: '123', subId: 'abc' }
});
});

it('should return empty string if not in possiblePaths', function () {
var templatizer = Templatizer.create([]),
template = templatizer.parse('/resource');
assert.deepEqual(template, { template: '', parameters: {} });
});
});
});

0 comments on commit 74e8926

Please sign in to comment.