From eac835b2d4660ff513601ee673aa72d334e9d471 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Wed, 3 Mar 2021 13:59:54 -0500 Subject: [PATCH] test: multi spec tests --- test/api.v3.yaml | 93 ++++++++++++++++++++++++++ test/api.v4.yaml | 75 +++++++++++++++++++++ test/multi.spec.operation.spec.ts | 104 ++++++++++++++++++++++++++++++ 3 files changed, 272 insertions(+) create mode 100644 test/api.v3.yaml create mode 100644 test/api.v4.yaml create mode 100644 test/multi.spec.operation.spec.ts diff --git a/test/api.v3.yaml b/test/api.v3.yaml new file mode 100644 index 00000000..d0b2b1f4 --- /dev/null +++ b/test/api.v3.yaml @@ -0,0 +1,93 @@ +openapi: '3.0.0' +info: + version: 1.0.0 + title: Swagger Petstore + description: A sample API + termsOfService: http://swagger.io/terms/ + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html +paths: + /users: + get: + description: | + Returns all pets + operationId: findUser + parameters: + - name: type + in: query + description: maximum number of results to return + required: true + schema: + type: string + enum: + - dog + - cat + - name: tags + in: query + description: tags to filter by + required: false + style: form + schema: + type: array + items: + type: string + - name: limit + in: query + description: maximum number of results to return + required: true + schema: + type: integer + format: int32 + minimum: 1 + maximum: 20 + responses: + '200': + description: pet response + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pet' + default: + description: unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + +components: + schemas: + Pet: + required: + - id + - name + - type + properties: + id: + readOnly: true + type: number + name: + type: string + tag: + type: string + type: + $ref: '#/components/schemas/PetType' + + PetType: + type: string + enum: + - dog + - cat + + Error: + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string diff --git a/test/api.v4.yaml b/test/api.v4.yaml new file mode 100644 index 00000000..82b9bf66 --- /dev/null +++ b/test/api.v4.yaml @@ -0,0 +1,75 @@ +openapi: '3.0.0' +info: + version: 1.0.0 + title: Swagger Petstore + description: A sample API + termsOfService: http://swagger.io/terms/ + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html +paths: + /pets: + get: + description: | + Returns all pets + operationId: findPets + parameters: + - name: pet_type + in: query + description: maximum number of results to return + required: true + schema: + type: string + enum: + - doggy + - kitty + responses: + '200': + description: pet response + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pet' + default: + description: unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + +components: + schemas: + Pet: + required: + - id + - name + - type + properties: + pet_id: + readOnly: true + type: number + pet_name: + type: string + pet_tag: + type: string + -pet_type: + $ref: '#/components/schemas/PetType' + + PetType: + type: string + enum: + - doggy + - kitty + + Error: + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string diff --git a/test/multi.spec.operation.spec.ts b/test/multi.spec.operation.spec.ts new file mode 100644 index 00000000..ecb01998 --- /dev/null +++ b/test/multi.spec.operation.spec.ts @@ -0,0 +1,104 @@ +import { expect } from 'chai'; +import * as request from 'supertest'; + +describe('multi-spec', () => { + let app = null; + + before(async () => { + // Set up the express app + app = createServer(); + }); + + after(() => { + app.server.close(); + }); + + it('should return users', async () => request(app).get(`/users`).expect(400)); + + it('should return pets', async () => request(app).get(`/pets`).expect(400)); +}); + +function createServer() { + const express = require('express'); + const path = require('path'); + const bodyParser = require('body-parser'); + const http = require('http'); + const OpenApiValidator = require('../src'); + + const app = express(); + app.use(bodyParser.urlencoded({ extended: false })); + app.use(bodyParser.text()); + app.use(bodyParser.json()); + + const versions = [3, 4]; + + for (const v of versions) { + const apiSpec = path.join(__dirname, `api.v${v}.yaml`); + app.use( + OpenApiValidator.middleware({ + apiSpec, + }), + ); + + routes(app, v); + } + + const server = http.createServer(app); + server.listen(3000); + console.log('Listening on port 3000'); + + function routes(app, v) { + routesUsers(app); + routesPets(app); + } + + function routesUsers(app) { + app.post(`/users`, (req, res, next) => { + res.json({ ...req.body }); + }); + app.get(`/users`, (req, res, next) => { + res.json([ + { + id: 1, + name: 'happy', + type: 'cat', + }, + ]); + }); + + app.use((err, req, res, next) => { + // format error + res.status(err.status || 500).json({ + message: err.message, + errors: err.errors, + }); + }); + } + + function routesPets(app) { + app.get('/pets', (req, res, next) => { + res.json([ + { + pet_id: 1, + pet_name: 'happy', + pet_type: 'kitty', + }, + ]); + }); + + app.post('/pets', (req, res, next) => { + res.json({ ...req.body }); + }); + + app.use((err, req, res, next) => { + // format error + res.status(err.status || 500).json({ + message: err.message, + errors: err.errors, + }); + }); + } + + app.server = server; + return app; +}