|
| 1 | +import * as path from 'path'; |
| 2 | +import * as request from 'supertest'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { createApp } from './common/app'; |
| 5 | + |
| 6 | +describe('request body validation with and without allErrors', () => { |
| 7 | + let allErrorsApp; |
| 8 | + let notAllErrorsApp; |
| 9 | + |
| 10 | + const defineRoutes = (app) => { |
| 11 | + app.post(`${app.basePath}/persons`, (req, res) => { |
| 12 | + res.send({ success: true }); |
| 13 | + }); |
| 14 | + app.get(`${app.basePath}/persons`, (req, res) => { |
| 15 | + res.send({ bname: req.query.bname }); |
| 16 | + }); |
| 17 | + }; |
| 18 | + |
| 19 | + before(async () => { |
| 20 | + const apiSpec = path.join('test', 'resources', 'multiple-validations.yaml'); |
| 21 | + |
| 22 | + allErrorsApp = await createApp( |
| 23 | + { |
| 24 | + apiSpec, |
| 25 | + formats: { |
| 26 | + 'starts-with-b': (v) => /^b/i.test(v), |
| 27 | + }, |
| 28 | + validateRequests: { |
| 29 | + allErrors: true, |
| 30 | + }, |
| 31 | + validateResponses: { |
| 32 | + allErrors: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + 3005, |
| 36 | + defineRoutes, |
| 37 | + true, |
| 38 | + ); |
| 39 | + |
| 40 | + notAllErrorsApp = await createApp( |
| 41 | + { |
| 42 | + apiSpec, |
| 43 | + formats: { |
| 44 | + 'starts-with-b': (v) => /^b/i.test(v), |
| 45 | + }, |
| 46 | + validateResponses: true, |
| 47 | + }, |
| 48 | + 3006, |
| 49 | + defineRoutes, |
| 50 | + true, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + after(() => { |
| 55 | + allErrorsApp.server.close(); |
| 56 | + notAllErrorsApp.server.close(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return 200 if short b-name is posted', async () => |
| 60 | + request(allErrorsApp) |
| 61 | + .post(`${allErrorsApp.basePath}/persons`) |
| 62 | + .set('content-type', 'application/json') |
| 63 | + .send({ bname: 'Bob' }) |
| 64 | + .expect(200)); |
| 65 | + |
| 66 | + it('should return 200 if short b-name is fetched', async () => |
| 67 | + request(allErrorsApp) |
| 68 | + .get(`${allErrorsApp.basePath}/persons?bname=Bob`) |
| 69 | + .expect(200)); |
| 70 | + |
| 71 | + it('should include all request validation errors when allErrors=true', async () => |
| 72 | + request(allErrorsApp) |
| 73 | + .post(`${allErrorsApp.basePath}/persons`) |
| 74 | + .set('content-type', 'application/json') |
| 75 | + .send({ bname: 'Maximillian' }) |
| 76 | + .expect(400) |
| 77 | + .then((res) => { |
| 78 | + expect(res.body.errors).to.have.lengthOf(2); |
| 79 | + })); |
| 80 | + |
| 81 | + it('should include only first request validation error when allErrors=false', async () => |
| 82 | + request(notAllErrorsApp) |
| 83 | + .post(`${notAllErrorsApp.basePath}/persons`) |
| 84 | + .set('content-type', 'application/json') |
| 85 | + .send({ bname: 'Maximillian' }) |
| 86 | + .expect(400) |
| 87 | + .then((res) => { |
| 88 | + expect(res.body.errors).to.have.lengthOf(1); |
| 89 | + })); |
| 90 | + |
| 91 | + it('should include all response validation errors when allErrors=true', async () => |
| 92 | + request(allErrorsApp) |
| 93 | + .get(`${allErrorsApp.basePath}/persons?bname=Maximillian`) |
| 94 | + .expect(500) |
| 95 | + .then((res) => { |
| 96 | + expect(res.body.errors).to.have.lengthOf(2); |
| 97 | + })); |
| 98 | + |
| 99 | + it('should include only first response validation error when allErrors=false', async () => |
| 100 | + request(notAllErrorsApp) |
| 101 | + .get(`${notAllErrorsApp.basePath}/persons?bname=Maximillian`) |
| 102 | + .expect(500) |
| 103 | + .then((res) => { |
| 104 | + expect(res.body.errors).to.have.lengthOf(1); |
| 105 | + })); |
| 106 | +}); |
0 commit comments