Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#99 option to remove additional props from responses
- Loading branch information
Carmine DiMascio
committed
Nov 2, 2019
1 parent
150279b
commit 1edde3c
Showing
6 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import * as path from 'path'; | ||
import * as express from 'express'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { createApp } from './common/app'; | ||
|
||
const packageJson = require('../package.json'); | ||
const apiSpecPath = path.join('test', 'resources', 'response.validation.yaml'); | ||
|
||
describe(packageJson.name, () => { | ||
let app = null; | ||
|
||
before(async () => { | ||
// set up express app | ||
app = await createApp( | ||
{ | ||
apiSpec: apiSpecPath, | ||
validateResponses: { | ||
removeAdditional: 'failing', | ||
}, | ||
}, | ||
3005, | ||
app => { | ||
app.get(`${app.basePath}/users`, (req, res) => { | ||
const json = ['user1', 'user2', 'user3']; | ||
return res.json(json); | ||
}); | ||
app.get(`${app.basePath}/pets`, (req, res) => { | ||
let json = {}; | ||
if ((req.query.mode = 'bad_type')) { | ||
json = [{ id: 'bad_id', name: 'name', tag: 'tag' }]; | ||
} | ||
return res.json(json); | ||
}); | ||
app.post(`${app.basePath}/no_additional_props`, (req, res) => { | ||
res.json(req.body); | ||
}); | ||
app.use((err, req, res, next) => { | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
code: err.status || 500, | ||
}); | ||
}); | ||
}, | ||
false, | ||
); | ||
}); | ||
|
||
after(() => { | ||
app.server.close(); | ||
}); | ||
|
||
it('should fail if response field has a value of incorrect type', async () => | ||
request(app) | ||
.get(`${app.basePath}/pets?mode=bad_type`) | ||
.expect(500) | ||
.then((r: any) => { | ||
expect(r.body.message).to.contain('should be integer'); | ||
expect(r.body) | ||
.to.have.property('code') | ||
.that.equals(500); | ||
})); | ||
|
||
it('should remove additional properties when set false', async () => | ||
request(app) | ||
.post(`${app.basePath}/no_additional_props`) | ||
.send({ | ||
token_type: 'token', | ||
expires_in: 1000, | ||
access_token: 'token', | ||
refresh_token: 'refresh_token', | ||
user: { | ||
id: 10, | ||
}, | ||
some_invalid_prop: 'test', | ||
}) | ||
.expect(200) | ||
.then((r: any) => { | ||
const body = r.body; | ||
expect(body).to.have.property('token_type'); | ||
expect(body).to.not.have.property('some_invalid_prop'); | ||
})); | ||
|
||
it('should remove nested additional prop if additionalProperties is false', async () => | ||
request(app) | ||
.post(`${app.basePath}/no_additional_props`) | ||
.send({ | ||
token_type: 'token', | ||
expires_in: 1000, | ||
access_token: 'token', | ||
refresh_token: 'refresh_token', | ||
user: { | ||
id: 10, | ||
extra_prop: true, | ||
}, | ||
}) | ||
.expect(200) | ||
.then((r: any) => { | ||
const body = r.body; | ||
expect(body.user).to.have.property('id'); | ||
expect(body.user).to.not.have.property('extra_prop'); | ||
})); | ||
|
||
it('should pass if response is a list', async () => | ||
request(app) | ||
.get(`${app.basePath}/users`) | ||
.expect(200) | ||
.then((r: any) => { | ||
expect(r.body) | ||
.is.an('array') | ||
.with.length(3); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters