Skip to content

Commit

Permalink
Merge pull request #9 from idanto/add-support-in-swagger-basepath
Browse files Browse the repository at this point in the history
add support for swagger base path
  • Loading branch information
idanto authored Dec 27, 2017
2 parents 20a1bd6 + cb6efa9 commit 51704fd
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function init(swaggerPath, options) {
var dereferenced = swaggers[0];
Object.keys(dereferenced.paths).forEach(function (currentPath) {
let pathParameters = dereferenced.paths[currentPath].parameters || [];
let parsedPath = currentPath.replace(/{/g, ':').replace(/}/g, '');
let parsedPath = dereferenced.basePath !== '/' ? dereferenced.basePath.concat(currentPath.replace(/{/g, ':').replace(/}/g, '')) : currentPath.replace(/{/g, ':').replace(/}/g, '');
schemas[parsedPath] = {};
Object.keys(dereferenced.paths[currentPath]).filter(function (parameter) { return parameter !== 'parameters' })
.forEach(function (currentMethod) {
Expand Down
351 changes: 351 additions & 0 deletions test/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,357 @@ describe('input-validation middleware tests', function () {
});
});
});
describe('Simple server - with base path', function () {
var app;
before(function () {
return require('./test-simple-server-with-base-path').then(function (testServer) {
app = testServer;
});
});
it('valid request - should pass validation', function (done) {
request(app)
.get('/v1/pets')
.set('api-version', '1.0')
.set('request-id', '123456')
.query({ page: 0 })
.expect(200, function (err, res) {
if (err) {
throw err;
}
expect(res.body.result).to.equal('OK');
done();
});
});
it('missing header - should fail', function (done) {
request(app)
.get('/v1/pets')
.set('request-id', '123456')
.query({ page: 0 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('api-version');
expect(res.body.more_info).to.includes('should have required property \'api-version\'');
done();
});
});
it('bad header - invalid pattern', function (done) {
request(app)
.get('/v1/pets')
.set('request-id', '123456')
.set('api-version', '1')
.query({ page: 0 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('api-version');
expect(res.body.more_info).to.includes('should match pattern');
done();
});
});
it('bad header - empty header', function (done) {
request(app)
.get('/v1/pets')
.set('request-id', '')
.set('api-version', '1.0')
.query({ page: 0 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('request-id');
expect(res.body.more_info).to.includes('should NOT be shorter than 1 characters');
done();
});
});
it('bad body - wrong type', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '123234')
.set('api-version', '1.0')
.send({
name: '111',
tag: 12344,
'test': {
field1: '1'
}
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('tag');
done();
});
});
it('bad body - missing required params', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '123324')
.set('api-version', '1.0')
.send({
tag: 'tag',
'test': {
field1: '1'
}
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('name');
done();
});
});
it('bad body - missing required object attribute', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '123434')
.set('api-version', '1.0')
.send({
name: 'name',
tag: 'tag'
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('test');
done();
});
});
it('bad body - wrong type object attribute', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '12334')
.set('api-version', '1.0')
.send({
name: 'name',
tag: 'tag',
test: ''
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('test');
done();
});
});
it('bad body - missing required nested attribute', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '12343')
.set('api-version', '1.0')
.send({
name: 'name',
tag: 'tag',
test: {}
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('field1');
done();
});
});
it('bad body - wrong format nested attribute', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '12343')
.set('api-version', '1.0')
.send({
name: 'name',
tag: 'tag',
test: {
field1: 1234
}
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('field1');
done();
});
});
it('bad body - wrong enum value', function (done) {
request(app)
.post('/v1/pets')
.set('request-id', '1234')
.set('api-version', '1.0')
.send({
name: 'name',
tag: 'tag',
test: {
field1: 'field1'
}
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('should be equal to one of the allowed values');
done();
});
});
it('bad query param - missing required params', function (done) {
request(app)
.get('/v1/pets')
.set('request-id', '1234')
.set('api-version', '1.0')
.query({ limit: 100 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('page');
done();
});
});
it('bad query param - over limit', function (done) {
request(app)
.get('/v1/pets')
.set('request-id', '1234')
.set('api-version', '1.0')
.query({ limit: 150, page: 0 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('limit');
done();
});
});
it('bad query param - under limit', function (done) {
request(app)
.get('/v1/pets')
.set('request-id', '1234')
.set('api-version', '1.0')
.query({ limit: 0, page: 0 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('limit');
done();
});
});
it('bad path param - wrong format', function (done) {
request(app)
.get('/v1/pets/12')
.set('request-id', '1234')
.set('api-version', '1.0')
.query({ limit: '50', page: 0 })
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('petId');
done();
});
});
it('bad body - wrong format nested attribute (not parameters)', function (done) {
request(app)
.put('/v1/pets')
.send([{
name: 'name',
tag: 'tag',
test: {
field1: 1234
}
}])
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(moreInfoAsJson.length).to.equal(2);
expect(res.body.more_info).to.includes('field1');
done();
});
});
it('bad body - wrong format in array item body (second item)', function (done) {
request(app)
.put('/v1/pets')
.send([
{
name: 'name',
tag: 'tag',
test: {
field1: 'enum1'
}
},
{
name: 'name',
tag: 'tag',
test: {
field1: 1234
}
}])
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('[1].test.field1');
done();
});
});
it('bad body - wrong format body (should be an array)', function (done) {
request(app)
.put('/v1/pets')
.send({
name: 'name',
tag: 'tag',
test: {
field1: '1234'
}
})
.expect(400, function (err, res) {
if (err) {
throw err;
}
let moreInfoAsJson = JSON.parse(res.body.more_info);
expect(moreInfoAsJson).to.be.instanceof(Array);
expect(res.body.more_info).to.includes('should be array');
done();
});
});
});
describe('Simple server using routes', function () {
var app;
before(function () {
Expand Down
4 changes: 2 additions & 2 deletions test/pet-store-swagger-inheritance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ info:
license:
name: MIT
host: petstore.swagger.io
basePath: /v1
basePath: /
schemes:
- http
consumes:
Expand Down Expand Up @@ -215,4 +215,4 @@ parameters:
in: path
description: 'api version in path'
type: string
enum: ['v1']
enum: ['v1']
Loading

0 comments on commit 51704fd

Please sign in to comment.