-
-
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.
Fixes for 881 - multiple specs w/validateRequests fail (#903)
- Loading branch information
Showing
10 changed files
with
154 additions
and
13 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
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
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
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
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
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
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
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
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,119 @@ | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
|
||
describe('multi-spec', () => { | ||
let app = null; | ||
|
||
before(async () => { | ||
// Set up the express app | ||
app = createServer(); | ||
}); | ||
|
||
after(() => { | ||
app.server.close(); | ||
}); | ||
|
||
it('create campaign should return 200', async () => | ||
request(app) | ||
.get(`/v1/pets`) | ||
.expect(400) | ||
.then((r) => { | ||
expect(r.body.message).include('limit'); | ||
expect(r.body.message).include(`'type'`); | ||
})); | ||
|
||
it('create campaign should return 200', async () => | ||
request(app) | ||
.get(`/v2/pets`) | ||
.expect(400) | ||
.then((r) => { | ||
expect(r.body.message).include(`'pet_type'`); | ||
})); | ||
}); | ||
|
||
function createServer() { | ||
const express = require('express'); | ||
const path = require('path'); | ||
const bodyParser = require('body-parser'); | ||
const http = require('http'); | ||
const OpenApiValidator = require('../src'); | ||
|
||
const app = express(); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.text()); | ||
app.use(bodyParser.json()); | ||
|
||
const versions = [1, 2]; | ||
|
||
for (const v of versions) { | ||
const apiSpec = path.join(__dirname, `api.v${v}.yaml`); | ||
app.use( | ||
OpenApiValidator.middleware({ | ||
apiSpec, | ||
validateResponses: true, | ||
}), | ||
); | ||
|
||
routes(app, v); | ||
} | ||
|
||
const server = http.createServer(app); | ||
server.listen(3000); | ||
console.log('Listening on port 3000'); | ||
|
||
function routes(app, v) { | ||
if (v === 1) routesV1(app); | ||
if (v === 2) routesV2(app); | ||
} | ||
|
||
function routesV1(app) { | ||
const v = '/v1'; | ||
app.post(`${v}/pets`, (req, res, next) => { | ||
res.json({ ...req.body }); | ||
}); | ||
app.get(`${v}/pets`, (req, res, next) => { | ||
res.json([ | ||
{ | ||
id: 1, | ||
name: 'happy', | ||
type: 'cat', | ||
}, | ||
]); | ||
}); | ||
|
||
app.use((err, req, res, next) => { | ||
// format error | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
errors: err.errors, | ||
}); | ||
}); | ||
} | ||
|
||
function routesV2(app) { | ||
const v = '/v2'; | ||
app.get(`${v}/pets`, (req, res, next) => { | ||
res.json([ | ||
{ | ||
pet_id: 1, | ||
pet_name: 'happy', | ||
pet_type: 'kitty', | ||
}, | ||
]); | ||
}); | ||
app.post(`${v}/pets`, (req, res, next) => { | ||
res.json({ ...req.body }); | ||
}); | ||
|
||
app.use((err, req, res, next) => { | ||
// format error | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
errors: err.errors, | ||
}); | ||
}); | ||
} | ||
|
||
app.server = server; | ||
return 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