-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing request body to be coerced (#468)
* allowing to coerce body * changing readme to more informative tone * fix readme * add missing bit of readme * fixed formatting * README - made it clear that validateRequests.coerceTypes only applies to body
- Loading branch information
Showing
8 changed files
with
145 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,4 +71,4 @@ | |
"ts-node": "^9.0.0", | ||
"typescript": "^4.0.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
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,101 @@ | ||
import * as path from 'path'; | ||
import * as request from 'supertest'; | ||
import { createApp } from './common/app'; | ||
|
||
describe('request body validation coercion', () => { | ||
let coerceApp = null; | ||
let nonCoerceApp = null; | ||
|
||
const defineRoutes = app => { | ||
app.post(`${app.basePath}/coercion_test`, (req, res) => { | ||
res.status(200).send() | ||
} | ||
) | ||
} | ||
|
||
before(async () => { | ||
// Set up the express app | ||
const apiSpec = path.join('test', 'resources', 'request.bodies.ref.yaml'); | ||
coerceApp = await createApp( | ||
{ | ||
apiSpec, | ||
validateRequests: { | ||
coerceTypes: true | ||
}, | ||
}, | ||
3005, | ||
defineRoutes, | ||
true, | ||
); | ||
|
||
nonCoerceApp = await createApp( | ||
{ | ||
apiSpec, | ||
// not specifying coercion as it should be false by default | ||
}, | ||
3006, | ||
defineRoutes, | ||
true, | ||
); | ||
}); | ||
|
||
after(() => { | ||
coerceApp.server.close(); | ||
nonCoerceApp.server.close(); | ||
}); | ||
|
||
it('should return 200 if coercion is enabled and the type is correct', async () => { | ||
return request(coerceApp) | ||
.post(`${coerceApp.basePath}/coercion_test`) | ||
.set('accept', 'application/json') | ||
.set('content-type', 'application/json') | ||
.send({ | ||
aNumberProperty: 4 | ||
}) | ||
.expect(200) | ||
}); | ||
|
||
it('should return 200 if coercion is enabled and the type is incorrect but can be coerced', async () => { | ||
return request(coerceApp) | ||
.post(`${coerceApp.basePath}/coercion_test`) | ||
.set('accept', 'application/json') | ||
.set('content-type', 'application/json') | ||
.send({ | ||
aNumberProperty: '4' | ||
}) | ||
.expect(200) | ||
}); | ||
|
||
it('should return 400 if coercion is enabled and the type is incorrect and cannot be coerced', async () => { | ||
return request(coerceApp) | ||
.post(`${coerceApp.basePath}/coercion_test`) | ||
.set('accept', 'application/json') | ||
.set('content-type', 'application/json') | ||
.send({ | ||
aNumberProperty: 'this is a string and definitely not a number' | ||
}) | ||
.expect(400) | ||
}); | ||
|
||
it('should return 200 if coercion is disabled and the type is correct', async () => { | ||
return request(nonCoerceApp) | ||
.post(`${nonCoerceApp.basePath}/coercion_test`) | ||
.set('accept', 'application/json') | ||
.set('content-type', 'application/json') | ||
.send({ | ||
aNumberProperty: 4 | ||
}) | ||
.expect(200) | ||
}); | ||
|
||
it('should return 400 if coercion is disabled and the type is incorrect', async () => { | ||
return request(nonCoerceApp) | ||
.post(`${nonCoerceApp.basePath}/coercion_test`) | ||
.set('accept', 'application/json') | ||
.set('content-type', 'application/json') | ||
.send({ | ||
aNumberProperty: '4' | ||
}) | ||
.expect(400) | ||
}); | ||
}); |
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