/
nullable.spec.ts
87 lines (76 loc) 路 2.21 KB
/
nullable.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
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
const packageJson = require('../package.json');
describe(packageJson.name, () => {
let app = null;
let basePath = null;
before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'nullable.yaml');
app = await createApp({ apiSpec, coerceTypes: false }, 3005);
basePath = app.basePath;
app.use(
`${basePath}`,
express.Router().post(`/pets/nullable`, (req, res) => res.json(req.body)),
);
});
after(() => {
app.server.close();
});
it('should allow null to be set (name: nullable true)', async () =>
request(app)
.post(`${basePath}/pets/nullable`)
.send({
name: null,
})
.expect(200)
.then(r => {
expect(r.body.name).to.be.null;
}));
it('should not fill an explicity null with default when coerceTypes is false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: null,
})
.expect(400));
it('should fill unspecified field with default when coerceTypes is false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: 'name',
})
.expect(200)
.then(r => {
expect(r.body.tag).to.equal('my default value');
}));
it('should fail if required and not provided (nullable true)', async () =>
request(app)
.post(`${basePath}/pets/nullable`)
.send({})
.expect(400)
.then(r => {
expect(r.body.errors[0].path).to.equal('.body.name');
}));
it('should fail if required and not provided (nullable false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({})
.expect(400)
.then(r => {
expect(r.body.errors[0].path).to.equal('.body.name');
}));
it('should fail if required and provided as null when nullable is false', async () =>
request(app)
.post(`${basePath}/pets`)
.send({
name: null,
})
.expect(400)
.then(r => {
expect(r.body.errors[0].path).to.equal('.body.name');
}));
});