-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
query.params.spec.ts
145 lines (132 loc) 路 4.14 KB
/
query.params.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';
describe(packageJson.name, () => {
let app = null;
before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'query.params.yaml');
app = await createApp({ apiSpec }, 3005, app =>
app.use(
`${app.basePath}`,
express
.Router()
.post(`/pets/nullable`, (req, res) => res.json(req.body))
.get(`/no_reserved`, (req, res) => res.json(req.body))
.get(`/no_query_params`, (req, res) => res.json({ complete: true }))
.get(`/allow_reserved`, (req, res) => res.json(req.body))
.get(`/unknown_query_params/allow`, (req, res) =>
res.json({ complete: true }),
),
),
);
});
after(() => {
app.server.close();
});
it('should pass if known query params are specified', async () =>
request(app)
.get(`${app.basePath}/pets`)
.query({
name: 'max',
tags: 'one,two,three',
limit: 10,
breed: 'german_shepherd',
owner_name: 'carmine',
})
.expect(200));
it('should reject any query param when endpoint declares none', async () =>
request(app)
.get(`${app.basePath}/no_query_params`)
.query({
name: 'max',
})
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array');
}));
it('should return 200 if no query params are supplied', async () =>
request(app)
.get(`${app.basePath}/no_query_params`)
.expect(200)
.then(r => {
expect(r.body.complete).to.equal(true);
}));
it('should fail if unknown query param is specified', async () =>
request(app)
.get(`${app.basePath}/pets`)
.query({
name: 'max',
tags: 'one,two,three',
limit: 10,
breed: 'german_shepherd',
owner_name: 'carmine',
unknown_prop: 'test',
})
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array');
}));
it('should return 200 if operation overrides x-allow-unknown-query-parameters=true', async () =>
request(app)
.get(`${app.basePath}/unknown_query_params/allow`)
.query({
value: 'foobar',
unknown_prop: 'test',
})
.expect(200));
it('should not allow empty query param value', async () =>
request(app)
.get(`${app.basePath}/pets`)
.query({
name: 'max',
tags: 'one,two,three',
limit: 10,
breed: '',
owner_name: 'carmine',
})
.expect(400)
.then(r => {
expect(r.body)
.to.have.property('message')
.that.equals("Empty value found for query parameter 'breed'");
expect(r.body.errors)
.to.be.an('array')
.with.length(1);
expect(r.body.errors[0].path).to.equal('.query.breed');
}));
it('should allow empty query param value with allowEmptyValue: true', async () =>
request(app)
.get(`${app.basePath}/pets`)
.query({
name: '',
tags: 'one,two,three',
limit: 10,
breed: 'german_shepherd',
owner_name: 'carmine',
})
.expect(200));
it("should succeed when query param 'name' has empty value and sets allowEmptyValue: true", async () =>
request(app)
.get(
`${app.basePath}/pets?name=&tags=one&limit=10&breed=german_shepherd&owner_name=carmine`,
)
.expect(200));
it('should not allow reserved characters', async () =>
request(app)
.get(`${app.basePath}/no_reserved?value=ThisHas$ReservedChars!`)
.expect(400)
.then(r => {
const body = r.body;
expect(body.message).equals(
"Parameter 'value' must be url encoded. Its value may not contain reserved characters.",
);
}));
it('should may allow reserved characters when allowedReserved: true', async () =>
request(app)
.get(`${app.basePath}/allow_reserved?value=ThisHas$ReservedChars!`)
.expect(200));
});