-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for default response >=400
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
openapi: '3.0.0' | ||
info: | ||
version: 1.0.0 | ||
title: Sample | ||
servers: | ||
- url: /v1 | ||
paths: | ||
/default_inline: | ||
get: | ||
parameters: | ||
- name: q | ||
in: 'query' | ||
required: true | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: Success | ||
content: | ||
'application/json': | ||
schema: | ||
type: object | ||
properties: | ||
data: | ||
description: Some data | ||
type: string | ||
required: | ||
- data | ||
default: | ||
description: Unexpected error | ||
content: | ||
'application/json': | ||
schema: | ||
required: | ||
- code | ||
- message | ||
properties: | ||
code: | ||
type: integer | ||
format: int32 | ||
message: | ||
type: string |
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,56 @@ | ||
import * as path from 'path'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { createApp } from './common/app'; | ||
|
||
const apiSpecPath = path.join( | ||
'test', | ||
'resources', | ||
'response.validation.defaults.yaml', | ||
); | ||
|
||
describe('response validation with type coercion', () => { | ||
let app = null; | ||
|
||
before(async () => { | ||
// set up express app | ||
app = await createApp( | ||
{ | ||
apiSpec: apiSpecPath, | ||
validateResponses: true, | ||
}, | ||
3005, | ||
(app) => { | ||
app.get(`${app.basePath}/default_inline`, (req, res) => { | ||
const q = req.query.q; | ||
|
||
if (q === '200') { | ||
return res.status(200).json({ data: 'good' }); | ||
} else if (q === '400') { | ||
return res.status(400).json({ message: 'message', code: 400 }); | ||
} else if (q === '400_bad') { | ||
return res.status(400).json({ bad: 'malformed' }); | ||
} | ||
}); | ||
}, | ||
); | ||
}); | ||
|
||
after(() => { | ||
app.server.close(); | ||
}); | ||
|
||
it('should validate 200 using explicit response', async () => | ||
request(app).get(`${app.basePath}/default_inline?q=200`).expect(200)); | ||
|
||
it('should validate undeclared 400 using default response', async () => | ||
request(app).get(`${app.basePath}/default_inline?q=400`).expect(400)); | ||
|
||
it('should validate undeclared 400 using default response', async () => | ||
request(app) | ||
.get(`${app.basePath}/default_inline?q=400_bad`) | ||
.expect(500) | ||
.then((r) => { | ||
expect(r.body.message).to.include('should have required property'); | ||
})); | ||
}); |