-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathrequest_validation_open_api_3_test.rb
474 lines (391 loc) · 15.8 KB
/
request_validation_open_api_3_test.rb
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# frozen_string_literal: true
require "test_helper"
describe Committee::Middleware::RequestValidation do
include Rack::Test::Methods
def app
@app
end
it "OpenAPI3 pass through a valid request" do
@app = new_rack_app(schema: open_api_3_schema)
params = {
"string_post_1" => "cloudnasium"
}
header "Content-Type", "application/json"
post "/characters", JSON.generate(params)
assert_equal 200, last_response.status
end
it "not parameter request" do
check_parameter_string = lambda { |_|
[200, {integer: 1}, []]
}
@app = new_rack_app_with_lambda(check_parameter_string, schema: open_api_3_schema)
put "/validate_no_parameter", {no_schema: 'no'}
end
it "passes given a datetime and with coerce_date_times enabled on GET endpoint" do
params = { "datetime_string" => "2016-04-01T16:00:00.000+09:00" }
check_parameter = lambda { |env|
assert_equal DateTime, env['rack.request.query_hash']["datetime_string"].class
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_schema, coerce_date_times: true)
get "/string_params_coercer", params
assert_equal 200, last_response.status
end
it "passes given a valid parameter on GET endpoint with request body and allow_get_body=true" do
params = { "data" => "abc" }
@app = new_rack_app(schema: open_api_3_schema, allow_get_body: true)
get "/get_endpoint_with_requered_parameter", { no_problem: true }, { input: params.to_json }
assert_equal 200, last_response.status
end
it "errors given valid parameter on GET endpoint with request body and allow_get_body=false" do
params = { "data" => "abc" }
@app = new_rack_app(schema: open_api_3_schema, allow_get_body: false)
get "/get_endpoint_with_requered_parameter", { no_problem: true }, { input: params.to_json }
assert_equal 400, last_response.status
end
it "passes given a datetime and with coerce_date_times enabled on GET endpoint with request body" do
params = { "datetime_string" => "2016-04-01T16:00:00.000+09:00" }
check_parameter = lambda { |env|
assert_equal DateTime, env['committee.params']["datetime_string"].class
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter,
schema: open_api_3_schema,
coerce_date_times: true,
allow_get_body: true)
get "/string_params_coercer", { no_problem: true }, { input: params.to_json }
assert_equal 200, last_response.status
end
it "passes given a datetime and with coerce_date_times enabled on POST endpoint" do
params = {
"nested_array" => [
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
"nested_coercer_object" => {
"update_time" => "2016-04-01T16:00:00.000+09:00",
},
"nested_no_coercer_object" => {
"update_time" => "2016-04-01T16:00:00.000+09:00",
},
"nested_coercer_array" => [
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
}
],
"nested_no_coercer_array" => [
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
}
]
},
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
}
]
}
check_parameter = lambda { |env|
nested_array = env['committee.params']["nested_array"]
first_data = nested_array[0]
assert_kind_of DateTime, first_data["update_time"]
second_data = nested_array[1]
assert_kind_of DateTime, second_data["update_time"]
assert_kind_of DateTime, first_data["nested_coercer_object"]["update_time"]
assert_kind_of String, first_data["nested_no_coercer_object"]["update_time"]
assert_kind_of DateTime, first_data["nested_coercer_array"].first["update_time"]
assert_kind_of String, first_data["nested_no_coercer_array"].first["update_time"]
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_schema, coerce_date_times: true, coerce_recursive: true)
header "Content-Type", "application/json"
post "/string_params_coercer", JSON.generate(params)
assert_equal 200, last_response.status
end
it "passes given an invalid datetime string with coerce_date_times enabled" do
@app = new_rack_app(schema: open_api_3_schema, coerce_date_times: true)
params = {
"datetime_string" => "invalid_datetime_format"
}
get "/string_params_coercer", params
assert_equal 400, last_response.status
assert_match(/invalid_datetime/i, last_response.body)
end
it "passes a nested object with recursive option" do
params = {
"nested_array" => [
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
"per_page" => "1",
}
],
}
check_parameter = lambda { |env|
hash = env['rack.request.query_hash']
assert_equal DateTime, hash['nested_array'].first['update_time'].class
assert_equal 1, hash['nested_array'].first['per_page']
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter,
coerce_query_params: true,
coerce_recursive: true,
coerce_date_times: true,
schema: open_api_3_schema)
get "/string_params_coercer", params
assert_equal 200, last_response.status
end
it "passes given a nested datetime and with coerce_recursive=true and coerce_date_times=true on POST endpoint" do
params = {
"nested_array" => [
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
"per_page" => 1,
"nested_coercer_object" => {
"update_time" => "2016-04-01T16:00:00.000+09:00",
"threshold" => 1.5
},
"nested_no_coercer_object" => {
"per_page" => 1,
"threshold" => 1.5
},
"nested_coercer_array" => [
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
"threshold" => 1.5
}
],
"nested_no_coercer_array" => [
{
"per_page" => 1,
"threshold" => 1.5
}
],
"integer_array" => [
1, 2, 3
],
"datetime_array" => [
"2016-04-01T16:00:00.000+09:00",
"2016-04-01T17:00:00.000+09:00",
"2016-04-01T18:00:00.000+09:00"
]
},
{
"update_time" => "2016-04-01T16:00:00.000+09:00",
"per_page" => 1,
"threshold" => 1.5
},
{
"threshold" => 1.5,
"per_page" => 1
}
]
}
check_parameter = lambda { |env|
hash = env['committee.params']
array = hash['nested_array']
assert_equal DateTime, array.first['update_time'].class
assert_equal 1, array.first['per_page']
assert_equal DateTime, array.first['nested_coercer_object']['update_time'].class
assert_equal 1, array.first['nested_no_coercer_object']['per_page']
assert_equal 2, array.first['integer_array'][1]
assert_equal DateTime, array.first['datetime_array'][0].class
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter,
coerce_date_times: true,
schema: open_api_3_schema)
header "Content-Type", "application/json"
post "/string_params_coercer", JSON.generate(params)
assert_equal 200, last_response.status
end
it "OpenAPI3 detects an invalid request" do
@app = new_rack_app(schema: open_api_3_schema, strict: true)
header "Content-Type", "application/json"
params = {
"string_post_1" => 1
}
post "/characters", JSON.generate(params)
assert_equal 400, last_response.status
assert_match(/expected string, but received Integer:/i, last_response.body)
end
it "rescues JSON errors" do
@app = new_rack_app(schema: open_api_3_schema)
header "Content-Type", "application/json"
post "/characters", "{x:y}"
assert_equal 400, last_response.status
assert_match(/valid json/i, last_response.body)
end
it "take a prefix" do
@app = new_rack_app(prefix: "/v1", schema: open_api_3_schema)
params = {
"string_post_1" => "cloudnasium"
}
header "Content-Type", "application/json"
post "/v1/characters", JSON.generate(params)
assert_equal 200, last_response.status
end
it "take a prefix with invalid data" do
@app = new_rack_app(prefix: "/v1", schema: open_api_3_schema)
params = {
"string_post_1" => 1
}
header "Content-Type", "application/json"
post "/v1/characters", JSON.generate(params)
assert_equal 400, last_response.status
assert_match(/expected string, but received Integer: /i, last_response.body)
end
it "ignores paths outside the prefix" do
@app = new_rack_app(prefix: "/v1", schema: open_api_3_schema)
params = {
"string_post_1" => 1
}
header "Content-Type", "application/json"
post "/characters", JSON.generate(params)
assert_equal 200, last_response.status
end
it "don't check prefix with no option" do
@app = new_rack_app(schema: open_api_3_schema)
params = {
"string_post_1" => 1
}
header "Content-Type", "application/json"
post "/v1/characters", JSON.generate(params)
assert_equal 200, last_response.status
end
it "OpenAPI3 pass not exist href" do
@app = new_rack_app(schema: open_api_3_schema)
get "/unknown"
assert_equal 200, last_response.status
end
it "OpenAPI3 pass not exist href in strict mode" do
@app = new_rack_app(schema: open_api_3_schema, strict: true)
get "/unknown"
assert_equal 404, last_response.status
end
it "OpenAPI3 parser not exist required key" do
@app = new_rack_app(raise: true, schema: open_api_3_schema)
e = assert_raises(Committee::InvalidRequest) do
get "/validate", nil
end
assert_match(/missing required parameters: query_string/i, e.message)
end
it "raises error when required path parameter is invalid" do
@app = new_rack_app(raise: true, schema: open_api_3_schema)
e = assert_raises(Committee::InvalidRequest) do
not_an_integer = 'abc'
get "/coerce_path_params/#{not_an_integer}", nil
end
assert_match(/expected integer, but received String: abc/i, e.message)
end
it "optionally raises an error" do
@app = new_rack_app(raise: true, schema: open_api_3_schema)
header "Content-Type", "application/json"
assert_raises(Committee::InvalidRequest) do
post "/characters", "{x:y}"
end
end
it "optionally coerces query params" do
@app = new_rack_app(coerce_query_params: true, schema: open_api_3_schema)
header "Content-Type", "application/json"
get "/string_params_coercer", {"integer_1" => "1"}
assert_equal 200, last_response.status
end
it "still raises an error if query param coercion is not possible" do
@app = new_rack_app(coerce_query_params: false, schema: open_api_3_schema)
header "Content-Type", "application/json"
get "/string_params_coercer", {"integer_1" => "1"}
assert_equal 400, last_response.status
assert_match(/expected integer, but received String:/i, last_response.body)
end
it "passes through a valid request for OpenAPI3" do
check_parameter = lambda { |env|
assert_equal 3, env['rack.request.query_hash']['limit']
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_schema)
get "/characters?limit=3"
assert_equal 200, last_response.status
end
it "detects an invalid request for OpenAPI" do
@app = new_rack_app(schema: open_api_3_schema)
get "/characters?limit=foo"
assert_equal 400, last_response.status
assert_match(/expected integer, but received String: foo/i, last_response.body)
end
it "ignores errors when ignore_error: true" do
@app = new_rack_app(schema: open_api_3_schema, ignore_error: true)
get "/characters?limit=foo"
assert_equal 200, last_response.status
end
it "coerce string to integer" do
check_parameter_string = lambda { |env|
assert env['committee.params']['integer'].is_a?(Integer)
[200, {}, []]
}
@app = new_rack_app_with_lambda(check_parameter_string, schema: open_api_3_schema, coerce_path_params: true)
get "/coerce_path_params/1"
end
it "OpenAPI3 raise not support method" do
@app = new_rack_app(schema: open_api_3_schema)
e = assert_raises(RuntimeError) {
head "/characters", {}
}
assert_equal 'Committee OpenAPI3 not support head method', e.message
end
describe 'check header' do
[
{ check_header: true, description: 'valid value', value: 1, expected: { status: 200 } },
{ check_header: true, description: 'missing value', value: nil, expected: { status: 400, error: 'missing required parameters: integer' } },
{ check_header: true, description: 'invalid value', value: 'x', expected: { status: 400, error: 'expected integer, but received String: x' } },
{ check_header: false, description: 'valid value', value: 1, expected: { status: 200 } },
{ check_header: false, description: 'missing value', value: nil, expected: { status: 200 } },
{ check_header: false, description: 'invalid value', value: 'x', expected: { status: 200 } },
].each do |h|
check_header = h[:check_header]
description = h[:description]
value = h[:value]
expected = h[:expected]
describe "when #{check_header}" do
%w(get post put patch delete).each do |method|
describe method do
describe description do
it (expected[:error].nil? ? 'should pass' : 'should fail') do
@app = new_rack_app(schema: open_api_3_schema, check_header: check_header)
header 'integer', value
send(method, "/header")
assert_equal expected[:status], last_response.status
assert_match(expected[:error], last_response.body) if expected[:error]
end
end
end
end
end
end
end
describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |h|
description = h[:description]
accept_request_filter = h[:accept_request_filter]
expected = h[:expected]
it description do
@app = new_rack_app(prefix: '/v1', schema: open_api_3_schema, accept_request_filter: accept_request_filter)
post 'v1/characters', JSON.generate(string_post_1: 1)
assert_equal expected[:status], last_response.status
end
end
end
private
def new_rack_app(options = {})
new_rack_app_with_lambda(lambda { |_|
[200, {}, []]
}, options)
end
def new_rack_app_with_lambda(check_lambda, options = {})
Rack::Builder.new {
use Committee::Middleware::RequestValidation, options
run check_lambda
}
end
end