From b87423928ce6804fd84d992305e25d0643ade91d Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sun, 15 Mar 2020 12:18:35 -0400 Subject: [PATCH] add response validation tests --- test/resources/response.validation.yaml | 109 +++++++++++++++++++----- test/response.validation.spec.ts | 32 +++++++ 2 files changed, 119 insertions(+), 22 deletions(-) diff --git a/test/resources/response.validation.yaml b/test/resources/response.validation.yaml index 574ed4e5..b60d477b 100644 --- a/test/resources/response.validation.yaml +++ b/test/resources/response.validation.yaml @@ -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 @@ -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 @@ -138,7 +203,7 @@ components: Users: description: 'Generic list of values from database schema' type: array - items: + items: type: string Human: @@ -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 diff --git a/test/response.validation.spec.ts b/test/response.validation.spec.ts index 6fa5fa90..bc0bb05e 100644 --- a/test/response.validation.spec.ts +++ b/test/response.validation.spec.ts @@ -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); @@ -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`)