|
| 1 | +import * as express from 'express'; |
| 2 | +import * as request from 'supertest'; |
| 3 | +import { createApp } from './common/app'; |
| 4 | +import * as packageJson from '../package.json'; |
| 5 | +import { OpenAPIV3 } from '../src/framework/types'; |
| 6 | +import {expect} from "chai"; |
| 7 | + |
| 8 | +describe(packageJson.name, () => { |
| 9 | + let app = null; |
| 10 | + before(async () => { |
| 11 | + // Set up the express app |
| 12 | + const apiSpec: OpenAPIV3.DocumentV3 = { |
| 13 | + openapi: '3.0.0', |
| 14 | + info: { title: 'Api test', version: '1.0.0' }, |
| 15 | + servers: [{ url: '/api' }], |
| 16 | + paths: { |
| 17 | + '/test/{id}': { |
| 18 | + description: 'Description', |
| 19 | + parameters: [ |
| 20 | + { |
| 21 | + name: 'id', |
| 22 | + in: 'path', |
| 23 | + required: true, |
| 24 | + schema: { type: 'string' }, |
| 25 | + }, |
| 26 | + ], |
| 27 | + get: { |
| 28 | + responses: { |
| 29 | + '200': { |
| 30 | + description: 'response', |
| 31 | + content: { |
| 32 | + 'application/json': { |
| 33 | + schema: { |
| 34 | + type: 'object', |
| 35 | + properties: { |
| 36 | + id: { type: 'string' }, |
| 37 | + label: { type: 'string' }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + '/test/{id}:clone': { |
| 47 | + description: 'Description', |
| 48 | + parameters: [ |
| 49 | + { |
| 50 | + name: 'id', |
| 51 | + in: 'path', |
| 52 | + required: true, |
| 53 | + schema: { type: 'string' }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + |
| 57 | + post: { |
| 58 | + requestBody: { |
| 59 | + content: { |
| 60 | + 'application/json': { |
| 61 | + schema: { |
| 62 | + type: 'object', |
| 63 | + required: ['id'], |
| 64 | + properties: { |
| 65 | + id: { |
| 66 | + type: 'integer' |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + responses: { |
| 74 | + '200': { |
| 75 | + description: 'response', |
| 76 | + content: { |
| 77 | + 'application/json': { |
| 78 | + schema: { |
| 79 | + type: 'object', |
| 80 | + properties: { |
| 81 | + id: { type: 'string' }, |
| 82 | + label: { type: 'string' }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + app = await createApp( |
| 95 | + { |
| 96 | + apiSpec, |
| 97 | + validateRequests: true, |
| 98 | + validateResponses: true, |
| 99 | + }, |
| 100 | + 3005, |
| 101 | + (app) => |
| 102 | + app.use( |
| 103 | + express |
| 104 | + .Router() |
| 105 | + .get(`/api/test/:id`, (req, res) => res.status(200).json({ id: 'id-test', label: 'label'})) |
| 106 | + .post(`/api/test/:id:clone`, (req, res) => res.status(200).json({...req.body, id: 'id-test'})), |
| 107 | + ), |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + after(() => { |
| 112 | + app.server.close(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('get /test/{id} should return 200', async () => |
| 116 | + request(app).get(`/api/test/abc123`).expect(200)); |
| 117 | + |
| 118 | + it('POST /test/{id}:clone should return 200', async () => |
| 119 | + request(app).post(`/api/test/abc123:clone`) |
| 120 | + .send({ id: 10 }) |
| 121 | + .expect(200)); |
| 122 | + |
| 123 | + it('POST /test/{id}:clone should return 400', async () => |
| 124 | + request(app).post(`/api/test/abc123:clone`) |
| 125 | + .send({ id: 'abc123' }) |
| 126 | + .expect(400) |
| 127 | + .then(r => { |
| 128 | + expect(r.body.message).to.include('id must be integer'); |
| 129 | + })); |
| 130 | +}); |
0 commit comments