From a25c00cc7c82ffaedadc5f3345c6b7e567c61e1f Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Tue, 19 Mar 2019 18:28:23 -0400 Subject: [PATCH] add tests --- test/app.ts | 48 ++++++++++++++++++++++++++++ test/middleware.spec.ts | 30 ++++++++++++++++++ test/routes.spec.ts | 69 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 test/app.ts create mode 100644 test/middleware.spec.ts create mode 100644 test/routes.spec.ts diff --git a/test/app.ts b/test/app.ts new file mode 100644 index 00000000..bde8c253 --- /dev/null +++ b/test/app.ts @@ -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; diff --git a/test/middleware.spec.ts b/test/middleware.spec.ts new file mode 100644 index 00000000..795880c3 --- /dev/null +++ b/test/middleware.spec.ts @@ -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' + ); + }); +}); diff --git a/test/routes.spec.ts b/test/routes.spec.ts new file mode 100644 index 00000000..e60e5683 --- /dev/null +++ b/test/routes.spec.ts @@ -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'); + })); +});