-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
881.spec.ts
119 lines (102 loc) 路 2.5 KB
/
881.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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;
}