diff --git a/test/resources/576/components/cluster.yaml b/test/resources/576/components/cluster.yaml new file mode 100644 index 00000000..74b1b7b8 --- /dev/null +++ b/test/resources/576/components/cluster.yaml @@ -0,0 +1,12 @@ +type: object +properties: + cluster_id: + type: string + cluster_name: + type: string +required: + - cluster_id + - cluster_name +example: + cluster_id: abcdefgh123456789 + cluster_name: MyCluster diff --git a/test/resources/576/components/user.yaml b/test/resources/576/components/user.yaml new file mode 100644 index 00000000..4018d209 --- /dev/null +++ b/test/resources/576/components/user.yaml @@ -0,0 +1,9 @@ +type: object +properties: + user_id: + type: string + name: + type: string +example: + user_id: "1" + name: John diff --git a/test/resources/576/errors/400.yaml b/test/resources/576/errors/400.yaml new file mode 100644 index 00000000..425de85c --- /dev/null +++ b/test/resources/576/errors/400.yaml @@ -0,0 +1,13 @@ +type: object +properties: + error: + type: object + properties: + type: + type: number + message: + type: string +example: + error: + type: 400 + message: 'Bad request' diff --git a/test/resources/576/index.yaml b/test/resources/576/index.yaml new file mode 100644 index 00000000..c1e7f3c5 --- /dev/null +++ b/test/resources/576/index.yaml @@ -0,0 +1,17 @@ +openapi: '3.0.3' +info: + version: 0.0.1 + title: My API + description: Best API ever + contact: + name: My Support + email: support@example.com +servers: + - url: 127.0.0.1/v1 +paths: + $ref: paths.yaml +components: + schemas: {} +tags: + - name: Clusters + - name: Users diff --git a/test/resources/576/paths.yaml b/test/resources/576/paths.yaml new file mode 100644 index 00000000..979b40c5 --- /dev/null +++ b/test/resources/576/paths.yaml @@ -0,0 +1,4 @@ +/clusters: + $ref: "paths/clusters.yaml" +/users: + $ref: "paths/users.yaml" diff --git a/test/resources/576/paths/clusters.yaml b/test/resources/576/paths/clusters.yaml new file mode 100644 index 00000000..b4228f35 --- /dev/null +++ b/test/resources/576/paths/clusters.yaml @@ -0,0 +1,10 @@ +get: + tags: + - Clusters + description: Returns a list of clusters + operationId: getClusters + responses: + "200": + $ref: "../responses/clusters.yaml" + "400": + $ref: "../responses/400.yaml" diff --git a/test/resources/576/paths/users.yaml b/test/resources/576/paths/users.yaml new file mode 100644 index 00000000..4a671a2a --- /dev/null +++ b/test/resources/576/paths/users.yaml @@ -0,0 +1,10 @@ +get: + tags: + - Users + description: Returns a list of users + operationId: getUsers + responses: + '200': + $ref: '../responses/users.yaml' + '400': + $ref: '../responses/400.yaml' diff --git a/test/resources/576/responses/400.yaml b/test/resources/576/responses/400.yaml new file mode 100644 index 00000000..a305ebf0 --- /dev/null +++ b/test/resources/576/responses/400.yaml @@ -0,0 +1,5 @@ +description: Bad request +content: + 'application/json': + schema: + $ref: '../errors/400.yaml' diff --git a/test/resources/576/responses/404.yaml b/test/resources/576/responses/404.yaml new file mode 100644 index 00000000..e3b759c7 --- /dev/null +++ b/test/resources/576/responses/404.yaml @@ -0,0 +1,5 @@ +description: Not found +content: + "application/json": + schema: + $ref: "../errors/404.yaml" diff --git a/test/resources/576/responses/clusters.yaml b/test/resources/576/responses/clusters.yaml new file mode 100644 index 00000000..9960a614 --- /dev/null +++ b/test/resources/576/responses/clusters.yaml @@ -0,0 +1,7 @@ +description: Clusters +content: + 'application/json': + schema: + type: array + items: + $ref: '../components/cluster.yaml' diff --git a/test/resources/576/responses/users.yaml b/test/resources/576/responses/users.yaml new file mode 100644 index 00000000..75a476a8 --- /dev/null +++ b/test/resources/576/responses/users.yaml @@ -0,0 +1,7 @@ +description: Users +content: + "application/json": + schema: + type: array + items: + $ref: "../components/user.yaml" diff --git a/test/response.validation.576.spec.ts b/test/response.validation.576.spec.ts new file mode 100644 index 00000000..7918ea7b --- /dev/null +++ b/test/response.validation.576.spec.ts @@ -0,0 +1,41 @@ +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', '576', 'index.yaml'); + +describe('response validation for multiple responses', () => { + let app = null; + + before(async () => { + // set up express app + app = await createApp( + { + apiSpec: apiSpecPath, + validateResponses: true, + $refParser: { + mode: 'dereference', + }, + }, + 3005, + (app) => { + app.get(`${app.basePath}/clusters`, (req, res) => { + return res.status(200).json({ data: 'bad' }); + }); + }, + ); + }); + + after(() => { + app.server.close(); + }); + + it('should validate bad response', async () => + request(app) + .get(`${app.basePath}/clusters`) + .expect(500) + .then((r) => { + expect(r.body.message).to.include('.response should be array'); + })); +});