Skip to content

Commit

Permalink
add test for undocumented endpoint on Router
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Jun 13, 2020
1 parent b41cc5c commit 7e981c3
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/router.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { OpenApiValidator } from '../src';

describe('security.defaults', () => {
let app = express();
let basePath = '/api';
let server = null;

before(async () => {
const router = express.Router();
await new OpenApiValidator({
apiSpec: {
openapi: '3.0.0',
info: { version: '1.0.0', title: 'test bug OpenApiValidator' },
servers: [{ url: 'http://localhost:8080/api/' }],
paths: {
'/': { get: { responses: { 200: { description: 'home api' } } } },
},
},
}).install(router);

router.get('/', (req, res) => res.status(200).send('home api\n'));
router.get('/notDefined', (req, res) =>
res.status(200).send('url api not defined\n'),
);

app.get('/', (req, res) => res.status(200).send('home\n'));
app.use(basePath, router);

app.use((err, req, res, next) => {
// console.error(err);
res.status(err.status ?? 500).json({
message: err.message,
errors: err.errors,
});
});

server = app.listen(3000);
console.log('server start port 3000');
});

after(async () => server.close());

it('should return 404 for undocumented route when using Router', async () => {
return request(app)
.get(`${basePath}/notDefined`)
.expect(404)
.then((r) => {
expect(r.body).to.have.property('message').that.equals('not found');
});
});
});

0 comments on commit 7e981c3

Please sign in to comment.