Skip to content

Commit

Permalink
add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 22, 2019
1 parent bff3be1 commit 3214fb9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
26 changes: 26 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions test/app.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
}
55 changes: 52 additions & 3 deletions test/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () =>
Expand Down

0 comments on commit 3214fb9

Please sign in to comment.