Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 19, 2019
1 parent 8d2f9db commit a25c00c
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
const http = require('http');
const { OpenApiMiddleware } = require('../');

var app = express();

app.use(bodyParser.json());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));

// const ov = OpenAPIRequestValidator();
// var router = express.Router();

app.use(
new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validate: true,
enableObjectCoercion: true, // should be default
errorTransformer: (a, b) => {
console.log('---error trans---', a, b);

return a;
}
}).middleware()
);
/* GET home page. */
app.get('/v1/pets', function(req, res, next) {
console.log('at /v1/pets here');
res.json({
test: 'hi'
});
});

export const server = http.createServer(app);
server.listen(3000);
console.log('Listening on port 3000');
console.log('Try visiting http://localhost:3000/greet?name=Jason');
console.log('-----STARTED[---');

export default app;
30 changes: 30 additions & 0 deletions test/middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const expect = require('chai').expect;
const { OpenApiMiddleware } = require('../');
const packageJson = require('../package.json');

describe(packageJson.name, () => {
it('existing spec', async () => {
const oam = new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validate: true,
enableObjectCoercion: true // should be default
});

expect(oam)
.to.have.property('middleware')
.that.is.a('function');
});

it('missing spec', async () => {
const createMiddleware = () =>
new OpenApiMiddleware({
apiSpecPath: './not-found.yaml',
validate: true,
enableObjectCoercion: true // should be default
}).middleware();

expect(createMiddleware).to.throw(
'spec could not be read at ./not-found.yaml'
);
});
});
69 changes: 69 additions & 0 deletions test/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const expect = require('chai').expect;
import * as request from 'supertest';
import app, { server } from './app';
const { OpenApiMiddleware } = require('../');
const packageJson = require('../package.json');

describe(packageJson.name, () => {
after(done => {
console.log('done', app);
server.close();
done();
});
it(`should test something`, () => {
expect('a').to.equal('a');
});

it('should throw 400 on missing required query parameter', async () =>
request(app)
.get('/v1/pets')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(400)
.then(r => {
const e = r.body;
expect(e).to.have.length(2);
expect(e[0].path).to.equal('limit');
expect(e[1].path).to.equal('test');
}));

it('should respond with json on proper get call', async () =>
request(app)
.get('/v1/pets')
.query({
test: 'one',
limit: 10
})
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200));

it('should return 200 with unknown query parameter', async () =>
request(app)
.get('/v1/pets')
.query({
test: 'one',
limit: 10,
bad_param: 'test'
})
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200));

it('should return 400 when improper range specified', async () =>
request(app)
.get('/v1/pets')
.query({
test: 'one',
limit: 2
})
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(400)
.then(r => {
const e = r.body;
expect(e).to.have.length(1);
expect(e[0].path).to.equal('limit');
expect(e[0].message).to.equal('should be >= 5');
}));
});

0 comments on commit a25c00c

Please sign in to comment.