-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carmine DiMascio
committed
Mar 19, 2019
1 parent
8d2f9db
commit a25c00c
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
})); | ||
}); |