Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases for default parameters - see issue #45 #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion test/assets/project/api/controllers/hello_world.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var util = require('util');

module.exports = {
hello: hello,
hello_array: hello_array,
hello_body: hello_body,
hello_file: hello_file,
get: hello,
Expand All @@ -17,6 +18,22 @@ function hello(req, res) {
res.json(hello);
}

function hello_array(req, res) {
var hello = 'Hello';
var names = req.swagger.params.names.value
for (let n =0; n < names.length; n ++) {
if (names.hasOwnProperty(n)) {
if ((n + 1) === names.length){
hello += " and "
}else {
hello += ", "
}
hello += names[n]
}
}
res.json(hello);
}

function hello_body(req, res) {
var name = req.swagger.params.nameRequest.value.name || 'stranger';
var hello = util.format('Hello, %s!', name);
Expand All @@ -40,4 +57,4 @@ function hello_text_body(req, res) {
var name = req.swagger.params.name.value || 'stranger';
var hello = util.format('Hello, %s!', name);
res.json(hello);
}
}
55 changes: 55 additions & 0 deletions test/assets/project/api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,61 @@ paths:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/hello_w_default:
x-swagger-router-controller: hello_world
get:
description: Returns 'Hello' to the caller
operationId: hello
parameters:
- name: name
in: query
description: The name of the person to whom to say hello
default: "no stranger"
type: string
responses:
200:
description: Success
schema:
type: object
$ref: "#/definitions/HelloWorldResponse"
examples:
application/json:
message: 'An example message'
application/x-yaml:
message: 'A yaml example'
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/hello_w_default_array:
x-swagger-router-controller: hello_world
get:
description: Returns 'Hello' to the caller
operationId: hello_array
parameters:
- name: names
in: query
description: The names of the people to whom to say hello
default: ["Tom", "Maria"]
type: array
items:
type: string
collectionFormat: csv
responses:
200:
description: Success
schema:
type: object
$ref: "#/definitions/HelloWorldResponse"
examples:
application/json:
message: 'An example message'
application/x-yaml:
message: 'A yaml example'
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
/multiple_writes:
x-swagger-router-controller: hello_world
get:
Expand Down
52 changes: 52 additions & 0 deletions test/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,58 @@ module.exports = function() {
});
});

it('should execute with a default name', function(done) {
request(this.app)
.get('/hello_w_default')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
should.not.exist(err);
res.body.should.eql('Hello, no stranger!');
done();
});
});

it('should execute and ignore default name if name is provided', function(done) {
request(this.app)
.get('/hello_w_default?name=Peter')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
should.not.exist(err);
res.body.should.eql('Hello, Peter!');
done();
});
});

it('default names should be replaced with new parameter if supplied', function(done) {
request(this.app)
.get('/hello_w_default_array?names[]=Tom&names[]=Peter')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
should.not.exist(err);
res.body.should.eql('Hello, Tom and Peter');
done();
});
});

it('should execute with default names', function(done) {
request(this.app)
.get('/hello_w_default_array')
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
should.not.exist(err);
res.body.should.eql('Hello, Tom and Maria');
done();
});
});

it('should execute without operationId', function(done) {
request(this.app)
.get('/hello_no_operationid')
Expand Down