Skip to content

Commit

Permalink
add response validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 15, 2020
1 parent 6b882db commit b874239
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 22 deletions.
109 changes: 87 additions & 22 deletions test/resources/response.validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,55 @@ paths:
responses:
'200':
description: empty
/object:
description: endpoints for pets
summary: endpoints for pets
get:
operationId: object
parameters:
- name: mode
in: query
schema:
type: string
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Object'
# type: object
# required:
# - name
# - id
# properties:
# id:
# type: integer
# format: int64
# bought_at:
# type: string
# format: date-time
# nullable: true
# name:
# type: string
# nullable: true
# tag:
# type: string
post:
operationId: postObject
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'

/pets:
description: endpoints for pets
summary: endpoints for pets
Expand Down Expand Up @@ -107,10 +156,26 @@ paths:
schema:
$ref: '#/components/schemas/NoAdditionalProps'



components:
schemas:
Object:
type: object
required:
- name
- id
properties:
id:
type: integer
format: int64
bought_at:
type: string
format: date-time
nullable: true
name:
type: string
nullable: true
tag:
type: string
NewPet:
required:
- name
Expand Down Expand Up @@ -138,7 +203,7 @@ components:
Users:
description: 'Generic list of values from database schema'
type: array
items:
items:
type: string

Human:
Expand All @@ -156,25 +221,25 @@ components:
$ref: '#/components/schemas/Human'

NoAdditionalProps:
type: object
additionalProperties: false
properties:
token_type:
type: string
expires_in:
type: integer
access_token:
type: string
refresh_token:
type: string
user:
additionalProperties: false
type: object
required:
- id
properties:
id:
type: integer
type: object
additionalProperties: false
properties:
token_type:
type: string
expires_in:
type: integer
access_token:
type: string
refresh_token:
type: string
user:
additionalProperties: false
type: object
required:
- id
properties:
id:
type: integer
Error:
required:
- code
Expand Down
32 changes: 32 additions & 0 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe(packageJson.name, () => {
app.get(`${app.basePath}/empty_response`, (req, res) => {
return res.end();
});
app.get(`${app.basePath}/object`, (req, res) => {
return res.json([
{ id: 1, name: 'name', tag: 'tag', bought_at: null },
]);
});
app.post(`${app.basePath}/object`, (req, res) => {
return res.json(req.body);
});
app.get(`${app.basePath}/users`, (req, res) => {
const json = ['user1', 'user2', 'user3'];
return res.json(json);
Expand Down Expand Up @@ -78,6 +86,30 @@ describe(packageJson.name, () => {
.that.equals(500);
}));

it('should fail if response is array when expecting object', async () =>
request(app)
.get(`${app.basePath}/object`)
.expect(500)
.then((r: any) => {
expect(r.body.message).to.contain('should be object');
expect(r.body)
.to.have.property('code')
.that.equals(500);
}));

// TODO fix me - fails for response and request validation what allOf is in use
it.skip('should fail if request is array when expecting object', async () =>
request(app)
.post(`${app.basePath}/object`)
.send([{ id: 1, name: 'name', tag: 'tag', bought_at: null }])
.expect(400)
.then((r: any) => {
expect(r.body.message).to.contain('should be object');
expect(r.body)
.to.have.property('code')
.that.equals(500);
}));

it('should fail if response is response is empty object', async () =>
request(app)
.get(`${app.basePath}/pets?mode=empty_object`)
Expand Down

0 comments on commit b874239

Please sign in to comment.