-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
response.validation.options.spec.ts
108 lines (100 loc) · 2.97 KB
/
response.validation.options.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
import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';
const apiSpecPath = path.join('test', 'resources', 'response.validation.yaml');
describe(packageJson.name, () => {
let app = null;
before(async () => {
// set up express app
app = await createApp(
{
apiSpec: apiSpecPath,
validateResponses: {
removeAdditional: 'failing',
},
},
3005,
(app) => {
app.get(`${app.basePath}/users`, (req, res) => {
const json = ['user1', 'user2', 'user3'];
return res.json(json);
});
app.get(`${app.basePath}/pets`, (req, res) => {
let json = {};
if ((req.query.mode = 'bad_type')) {
json = [{ id: 'bad_id', name: 'name', tag: 'tag' }];
}
return res.json(json);
});
app.post(`${app.basePath}/no_additional_props`, (req, res) => {
res.json(req.body);
});
app.use((err, req, res, next) => {
res.status(err.status ?? 500).json({
message: err.message,
code: err.status ?? 500,
});
});
},
false,
);
});
after(() => {
app.server.close();
});
it('should fail if response field has a value of incorrect type', async () =>
request(app)
.get(`${app.basePath}/pets?mode=bad_type`)
.expect(500)
.then((r: any) => {
expect(r.body.message).to.contain('must be integer');
expect(r.body).to.have.property('code').that.equals(500);
}));
it('should remove additional properties when set false', async () =>
request(app)
.post(`${app.basePath}/no_additional_props`)
.send({
token_type: 'token',
expires_in: 1000,
access_token: 'token',
refresh_token: 'refresh_token',
user: {
id: 10,
},
some_invalid_prop: 'test',
})
.expect(200)
.then((r: any) => {
const body = r.body;
expect(body).to.have.property('token_type');
expect(body).to.not.have.property('some_invalid_prop');
}));
it('should remove nested additional prop if additionalProperties is false', async () =>
request(app)
.post(`${app.basePath}/no_additional_props`)
.send({
token_type: 'token',
expires_in: 1000,
access_token: 'token',
refresh_token: 'refresh_token',
user: {
id: 10,
extra_prop: true,
},
})
.expect(200)
.then((r: any) => {
const body = r.body;
expect(body.user).to.have.property('id');
expect(body.user).to.not.have.property('extra_prop');
}));
it('should pass if response is a list', async () =>
request(app)
.get(`${app.basePath}/users`)
.expect(200)
.then((r: any) => {
expect(r.body).is.an('array').with.length(3);
}));
});