diff --git a/openapi.yaml b/openapi.yaml index f07404aa..03b6b80d 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -164,6 +164,32 @@ paths: application/json: schema: $ref: "#/components/schemas/Error" + + /route_not_defined_within_express: + get: + description: Returns attributes for this pet + operationId: route-not-defined-within-express + parameters: + - name: name + in: query + description: the name + required: true + schema: + type: string + responses: + "200": + description: pet response + content: + application/json: + schema: + $ref: "#/components/schemas/Attribute" + default: + description: unexpected error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + components: schemas: NewPet: diff --git a/test/app.common.ts b/test/app.common.ts index 675e2942..991c01a5 100644 --- a/test/app.common.ts +++ b/test/app.common.ts @@ -35,4 +35,15 @@ export function routes(app) { id: req.params.id, }); }); + + app.post('/v1/route_defined_in_express_not_openapi', function( + req, + res, + next + ) { + // here + res.json({ + id: req.params.id, + }); + }); } diff --git a/test/routes.spec.ts b/test/routes.spec.ts index e150a194..7e69dfb5 100644 --- a/test/routes.spec.ts +++ b/test/routes.spec.ts @@ -98,15 +98,64 @@ describe(packageJson.name, () => { }); describe('POST failures', () => { - it('should return 200 when post props are met', async () => + it('should return 400 if route is defined in openapi but not express and is called with invalid parameters', async () => + request(app) + .get('/v1/route_not_defined_within_express') + .expect(400) + .then(r => { + const e = r.body.errors; + expect(e[0].message).to.equal("should have required property 'name'"); + })); + + it('should return 404 if route is defined in swagger but not express', async () => + request(app) + .get('/v1/route_not_defined_within_express') + .query({ name: 'test' }) + .expect(404) + .then(r => { + const e = r.body; + // There is no route defined by express, hence the validator verifies parameters, + // then it fails over to the express error handler. In this case returns empty + expect(e).to.be.empty; + })); + + it('should return 405 if route is defined in swagger but not express and media type is invalid', async () => + request(app) + .post('/v1/route_not_defined_within_express') + .send() + .expect(405) + .then(r => { + const e = r.body.errors; + expect(e[0].message).to.equal('POST method not allowed'); + expect(e[0].path).to.equal('/v1/route_not_defined_within_express'); + })); + + it('should return 404 for unknown_route', async () => request(app) - .post('/v1/unknown-route') + .post('/v1/unknown_route') .send({ name: 'test', }) .expect(404) .then(r => { - console.log(r.body); + const e = r.body; + // There is no route defined by express, hence the validator verifies parameters, + // then it fails over to the express error handler. In this case returns empty + expect(e).to.be.empty; + })); + + it.skip('should return 404 for a route defined in express and not openapi', async () => + request(app) + .post('/v1/route_defined_in_express_not_openapi') + .send({ + name: 'test', + }) + .expect(404) + .then(r => { + const e = r.body; + // There is no route defined by express, hence the validator verifies parameters, + // then it fails over to the express error handler. In this case returns empty + expect(e).to.be.empty; })); it('should return 415 when media type is not supported', async () =>