Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Allow clients to specify query params in requestData (#139)
Browse files Browse the repository at this point in the history
Clients can now specify query parameters in the format:-

		requestData: {
          '/user': {
            get: {
              200: [longitude: 10, description: 'some description'}]
            }
          }
        }

Clients can now add requestData query params to GET requests

Clients can now use strings as query param request data
  • Loading branch information
baynezy authored and noahdietz committed May 4, 2017
1 parent 6bf388e commit 5ac4575
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 26 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ function testGenResponse(swagger, apiPath, operation, response, config, consume,

data.contentType = consume;
data.returnType = produce;
data.requestParameters = {};

// compile template source and return test string
var templatePath = path.join(config.templatesPath, config.testModule, operation, operation + '.handlebars');
Expand All @@ -267,6 +268,13 @@ function testGenResponse(swagger, apiPath, operation, response, config, consume,
result = '';
for (var i = 0; i < data.requestData.length; i++) {
data.request = JSON.stringify(data.requestData[i].body);

for (var key in data.requestData[i]) {
if (['body', 'description'].indexOf(key) === -1) {
data.requestParameters[key] = data.requestData[i][key];
}
}

data.requestMessage = data.requestData[i].description.replace(/'/g, "\\'"); // eslint-disable-line quotes
result += templateFn(data);
}
Expand Down Expand Up @@ -550,6 +558,7 @@ handlebars.registerHelper('validateResponse', helpers.validateResponse);
handlebars.registerHelper('length', helpers.length);
handlebars.registerHelper('pathify', helpers.pathify);
handlebars.registerHelper('printJSON', helpers.printJSON);
handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamFormatter);


module.exports = {
Expand Down
17 changes: 16 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
pathify: pathify,
printJSON: printJSON,
len: len,
setLen: setLen
setLen: setLen,
requestDataParamFormatter : requestDataParamFormatter
};

function setLen(descriptionLength) {
Expand Down Expand Up @@ -190,3 +191,17 @@ function prettyPrintJson(obj, indent) {
}
return result.replace(/,\n$/, '');
}


function requestDataParamFormatter(paramName, paramType, requestParameters){
var delimiter = "'";
if (['integer', 'number', 'boolean', 'null'].indexOf(paramType.toLowerCase()) > -1) {
delimiter = '';
}

if (typeof requestParameters[paramName] != 'undefined') {
return delimiter + requestParameters[paramName] + delimiter;
}

return "'DATA GOES HERE'";
}
2 changes: 1 addition & 1 deletion templates/request/delete/delete.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#ifCond queryParameters queryApiKey}}
qs: {
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
},
{{/ifCond}}
method: 'DELETE',
Expand Down
2 changes: 1 addition & 1 deletion templates/request/get/get.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#ifCond queryParameters queryApiKey}}
qs: {
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
},
{{/ifCond}}
method: 'GET',
Expand Down
2 changes: 1 addition & 1 deletion templates/request/patch/patch.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#ifCond queryParameters queryApiKey}}
qs: {
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
},
{{/ifCond}} method: 'PATCH',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion templates/request/post/post.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#ifCond queryParameters queryApiKey}}
qs: {
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
},
{{/ifCond}}
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion templates/request/put/put.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#ifCond queryParameters queryApiKey}}
qs: {
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
},
{{/ifCond}}
method: 'PUT',
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/delete/delete.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.query({
{{#if queryApiKey}}
{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/get/get.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.query({
{{#if queryApiKey}}
{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/head/head.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.query({
{{#if queryApiKey}}
{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/options/options.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.query({
{{#if queryApiKey}}
{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/patch/patch.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.query({
{{#if queryApiKey}}
{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/post/post.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{#ifCond queryParameters queryApiKey}}
.query({
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
2 changes: 1 addition & 1 deletion templates/supertest/put/put.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{#ifCond queryParameters queryApiKey}}
.query({
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}
{{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: {{requestDataParamFormatter this.name this.type ../requestParameters}}{{#unless @last}},{{/unless}}{{/each}}{{/if}}
})
{{/ifCond}}
{{#if headerSecurity}}
Expand Down
80 changes: 80 additions & 0 deletions test/request-data/compare/request/expect/qs1-user-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';
var chai = require('chai');
var request = require('request');
var expect = chai.expect;

describe('/user', function() {
describe('post', function() {
it('should respond with 200 OK and some description', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
longitude: 10
},
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: {"my-id":2}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(200);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

it('should respond with 400 NOT OK', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
longitude: 'DATA GOES HERE'
},
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: {
latitude: 'DATA GOES HERE'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(400);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

it('should respond with 500 SERVER ERROR', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
longitude: 'DATA GOES HERE'
},
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: {
latitude: 'DATA GOES HERE'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(500);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

});

});
73 changes: 73 additions & 0 deletions test/request-data/compare/request/expect/qs2-user-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';
var chai = require('chai');
var request = require('request');
var expect = chai.expect;

describe('/user', function() {
describe('get', function() {
it('should respond with 200 OK and some description', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
longitude: 10
},
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(200);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

it('should respond with 400 NOT OK', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
longitude: 'DATA GOES HERE'
},
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(400);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

it('should respond with 500 SERVER ERROR', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
longitude: 'DATA GOES HERE'
},
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(500);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

});

});
73 changes: 73 additions & 0 deletions test/request-data/compare/request/expect/qs3-user-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';
var chai = require('chai');
var request = require('request');
var expect = chai.expect;

describe('/user', function() {
describe('get', function() {
it('should respond with 200 OK and some description', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
name: 'Simon'
},
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(200);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

it('should respond with 400 NOT OK', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
name: 'DATA GOES HERE'
},
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(400);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

it('should respond with 500 SERVER ERROR', function(done) {
request({
url: 'https://api.uber.com/user',
qs: {
name: 'DATA GOES HERE'
},
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
},
function(error, res, body) {
if (error) return done(error);

expect(res.statusCode).to.equal(500);

expect(body).to.equal(null); // non-json response or no schema
done();
});
});

});

});
Loading

0 comments on commit 5ac4575

Please sign in to comment.