|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as request from 'supertest'; |
| 3 | + |
| 4 | +describe('multi-spec', () => { |
| 5 | + let app = null; |
| 6 | + |
| 7 | + before(async () => { |
| 8 | + // Set up the express app |
| 9 | + app = createServer(); |
| 10 | + }); |
| 11 | + |
| 12 | + after(() => { |
| 13 | + app.server.close(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('create campaign should return 200', async () => |
| 17 | + request(app) |
| 18 | + .get(`/v1/pets`) |
| 19 | + .expect(400) |
| 20 | + .then((r) => { |
| 21 | + expect(r.body.message).include('limit'); |
| 22 | + expect(r.body.message).include(`'type'`); |
| 23 | + })); |
| 24 | + |
| 25 | + it('create campaign should return 200', async () => |
| 26 | + request(app) |
| 27 | + .get(`/v2/pets`) |
| 28 | + .expect(400) |
| 29 | + .then((r) => { |
| 30 | + expect(r.body.message).include(`'pet_type'`); |
| 31 | + })); |
| 32 | +}); |
| 33 | + |
| 34 | +function createServer() { |
| 35 | + const express = require('express'); |
| 36 | + const path = require('path'); |
| 37 | + const bodyParser = require('body-parser'); |
| 38 | + const http = require('http'); |
| 39 | + const OpenApiValidator = require('../src'); |
| 40 | + |
| 41 | + const app = express(); |
| 42 | + app.use(bodyParser.urlencoded({ extended: false })); |
| 43 | + app.use(bodyParser.text()); |
| 44 | + app.use(bodyParser.json()); |
| 45 | + |
| 46 | + const versions = [1, 2]; |
| 47 | + |
| 48 | + for (const v of versions) { |
| 49 | + const apiSpec = path.join(__dirname, `api.v${v}.yaml`); |
| 50 | + app.use( |
| 51 | + OpenApiValidator.middleware({ |
| 52 | + apiSpec, |
| 53 | + validateResponses: true, |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + routes(app, v); |
| 58 | + } |
| 59 | + |
| 60 | + const server = http.createServer(app); |
| 61 | + server.listen(3000); |
| 62 | + console.log('Listening on port 3000'); |
| 63 | + |
| 64 | + function routes(app, v) { |
| 65 | + if (v === 1) routesV1(app); |
| 66 | + if (v === 2) routesV2(app); |
| 67 | + } |
| 68 | + |
| 69 | + function routesV1(app) { |
| 70 | + const v = '/v1'; |
| 71 | + app.post(`${v}/pets`, (req, res, next) => { |
| 72 | + res.json({ ...req.body }); |
| 73 | + }); |
| 74 | + app.get(`${v}/pets`, (req, res, next) => { |
| 75 | + res.json([ |
| 76 | + { |
| 77 | + id: 1, |
| 78 | + name: 'happy', |
| 79 | + type: 'cat', |
| 80 | + }, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + app.use((err, req, res, next) => { |
| 85 | + // format error |
| 86 | + res.status(err.status || 500).json({ |
| 87 | + message: err.message, |
| 88 | + errors: err.errors, |
| 89 | + }); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + function routesV2(app) { |
| 94 | + const v = '/v2'; |
| 95 | + app.get(`${v}/pets`, (req, res, next) => { |
| 96 | + res.json([ |
| 97 | + { |
| 98 | + pet_id: 1, |
| 99 | + pet_name: 'happy', |
| 100 | + pet_type: 'kitty', |
| 101 | + }, |
| 102 | + ]); |
| 103 | + }); |
| 104 | + app.post(`${v}/pets`, (req, res, next) => { |
| 105 | + res.json({ ...req.body }); |
| 106 | + }); |
| 107 | + |
| 108 | + app.use((err, req, res, next) => { |
| 109 | + // format error |
| 110 | + res.status(err.status || 500).json({ |
| 111 | + message: err.message, |
| 112 | + errors: err.errors, |
| 113 | + }); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + app.server = server; |
| 118 | + return app; |
| 119 | +} |
0 commit comments