Skip to content

Commit

Permalink
Infer even if x-amples are present
Browse files Browse the repository at this point in the history
  • Loading branch information
earldouglas committed Feb 6, 2015
1 parent 9de04a2 commit 1393b75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
4 changes: 3 additions & 1 deletion lib/swagger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var template = require('url-template');
function parseXample(spec, uri, method, xample) {
var uriTemplate = template.parse(uri);
var expandedUri = uriTemplate.expand(xample.request.params);
delete xample.request.params;
xample.request.method = method;
xample.request.uri = spec.host + spec.basePath + expandedUri;
return {
Expand Down Expand Up @@ -48,7 +49,8 @@ function parse(spec, options) {
operation['x-amples'].forEach(function (xample) {
xamples.push(parseXample(spec, uri, method, xample));
});
} else if (options.inferXamples) {
}
if (options.inferXamples) {
Object.keys(operation.responses || {}).forEach(function (statusString) {
xamples.push(inferXample(spec, uri, method, operation, statusString));
});
Expand Down
63 changes: 26 additions & 37 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ describe('test generation with inference', function () {
var buffer = fs.readFileSync(testDir + '/swagger.json');
var spec = JSON.parse(buffer);

var xamples = swaggerTest.parse(spec, { inferXamples: true });
var xamples = swaggerTest.parse(spec, {
inferXamples: true,
});

it('should contain three test cases', function () {
assert.equal(xamples.length, 3);
it('should contain four test cases', function () {
assert.equal(xamples.length, 4);
});

it('should first test GET /pets', function () {
it('should start with x-ample test: get /pets', function () {
assert.deepEqual(xamples[0], {
"description": "get /pets",
"request": {
Expand All @@ -34,42 +36,29 @@ describe('test generation with inference', function () {
});
});

it ('should next test GET /pets/fido4', function () {
assert.deepEqual(xamples[1], {
"description": "get /pets/{id}",
"request": {
"params": {
"id": "fido4"
},
"method": "get",
"uri": "localhost/v1/pets/fido4"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
}
function expectedPetIdXample(id) {
return {
description: 'get /pets/{id}',
request: {
method: 'get',
uri: 'localhost/v1/pets/' + id
},
response: {
status: 200,
headers: {
'content-type': 'application/json'
}
});
}
};
}

it('should follow with two x-ample tests', function () {
assert.deepEqual(xamples[1], expectedPetIdXample('fido4'));
assert.deepEqual(xamples[2], expectedPetIdXample('fido7'));
});

it ('should next test GET /pets/fido7', function () {
assert.deepEqual(xamples[2], {
"description": "get /pets/{id}",
"request": {
"params": {
"id": "fido7"
},
"method": "get",
"uri": "localhost/v1/pets/fido7"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
}
}
});
it('should follow with one inferre test', function () {
assert.deepEqual(xamples[3], expectedPetIdXample('{id}'));
});

});

0 comments on commit 1393b75

Please sign in to comment.