committee 4.99.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/committee/drivers/open_api_2/driver.rb +1 -1
- data/lib/committee/drivers/open_api_2/parameter_schema_builder.rb +1 -1
- data/lib/committee/drivers.rb +21 -9
- data/lib/committee/middleware/base.rb +5 -4
- data/lib/committee/middleware/request_validation.rb +1 -8
- data/lib/committee/middleware/response_validation.rb +13 -14
- data/lib/committee/request_unpacker.rb +1 -1
- data/lib/committee/schema_validator/hyper_schema/response_validator.rb +8 -2
- data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +36 -33
- data/lib/committee/schema_validator/open_api_3/request_validator.rb +11 -2
- data/lib/committee/schema_validator/open_api_3.rb +23 -18
- data/lib/committee/schema_validator/option.rb +3 -18
- data/lib/committee/schema_validator.rb +1 -1
- data/lib/committee/test/methods.rb +2 -7
- data/lib/committee/version.rb +5 -0
- data/lib/committee.rb +9 -2
- data/test/committee_test.rb +28 -2
- data/test/drivers/open_api_3/driver_test.rb +1 -1
- data/test/drivers_test.rb +20 -7
- data/test/middleware/base_test.rb +6 -13
- data/test/middleware/request_validation_open_api_3_test.rb +38 -42
- data/test/middleware/request_validation_test.rb +1 -28
- data/test/middleware/response_validation_open_api_3_test.rb +46 -3
- data/test/middleware/response_validation_test.rb +6 -25
- data/test/schema_validator/hyper_schema/response_validator_test.rb +10 -0
- data/test/schema_validator/hyper_schema/string_params_coercer_test.rb +1 -1
- data/test/schema_validator/open_api_3/operation_wrapper_test.rb +55 -11
- data/test/schema_validator/open_api_3/request_validator_test.rb +25 -1
- data/test/schema_validator/open_api_3/response_validator_test.rb +14 -0
- data/test/test/methods_new_version_test.rb +1 -1
- data/test/test/methods_test.rb +11 -31
- data/test/test_helper.rb +15 -5
- metadata +5 -10
data/test/drivers_test.rb
CHANGED
@@ -37,20 +37,33 @@ describe Committee::Drivers do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'loads OpenAPI 3' do
|
40
|
-
s = Committee::Drivers.load_from_file(open_api_3_schema_path)
|
40
|
+
s = Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options:{strict_reference_validation: true})
|
41
41
|
assert_kind_of Committee::Drivers::Schema, s
|
42
42
|
assert_kind_of Committee::Drivers::OpenAPI3::Schema, s
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'load OpenAPI 3 (patch version 3.0.1)' do
|
46
|
-
s = Committee::Drivers.load_from_file(open_api_3_0_1_schema_path)
|
46
|
+
s = Committee::Drivers.load_from_file(open_api_3_0_1_schema_path, parser_options:{strict_reference_validation: true})
|
47
47
|
assert_kind_of Committee::Drivers::Schema, s
|
48
48
|
assert_kind_of Committee::Drivers::OpenAPI3::Schema, s
|
49
49
|
end
|
50
50
|
|
51
|
+
it 'fails to load OpenAPI 3 with invalid reference' do
|
52
|
+
parser_options = { strict_reference_validation: true }
|
53
|
+
assert_raises(OpenAPIParser::MissingReferenceError) do
|
54
|
+
Committee::Drivers.load_from_file(open_api_3_invalid_reference_path, parser_options: parser_options)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# This test can be removed when the test above (raising on invalid reference) becomes default behavior?
|
59
|
+
it 'allows loading OpenAPI 3 with invalid reference as existing behavior' do
|
60
|
+
s = Committee::Drivers.load_from_file(open_api_3_invalid_reference_path, parser_options:{strict_reference_validation: false})
|
61
|
+
assert_kind_of Committee::Drivers::OpenAPI3::Schema, s
|
62
|
+
end
|
63
|
+
|
51
64
|
it 'errors on an unsupported file extension' do
|
52
65
|
e = assert_raises(StandardError) do
|
53
|
-
Committee::Drivers.load_from_file('test.xml')
|
66
|
+
Committee::Drivers.load_from_file('test.xml', parser_options:{strict_reference_validation: true})
|
54
67
|
end
|
55
68
|
assert_equal "Committee only supports the following file extensions: '.json', '.yaml', '.yml'", e.message
|
56
69
|
end
|
@@ -58,13 +71,13 @@ describe Committee::Drivers do
|
|
58
71
|
|
59
72
|
describe 'load_from_json(schema_path)' do
|
60
73
|
it 'loads OpenAPI2' do
|
61
|
-
s = Committee::Drivers.load_from_json(open_api_2_schema_path)
|
74
|
+
s = Committee::Drivers.load_from_json(open_api_2_schema_path, parser_options:{strict_reference_validation: true})
|
62
75
|
assert_kind_of Committee::Drivers::Schema, s
|
63
76
|
assert_kind_of Committee::Drivers::OpenAPI2::Schema, s
|
64
77
|
end
|
65
78
|
|
66
79
|
it 'loads Hyper-Schema' do
|
67
|
-
s = Committee::Drivers.load_from_json(hyper_schema_schema_path)
|
80
|
+
s = Committee::Drivers.load_from_json(hyper_schema_schema_path, parser_options:{strict_reference_validation: true})
|
68
81
|
assert_kind_of Committee::Drivers::Schema, s
|
69
82
|
assert_kind_of Committee::Drivers::HyperSchema::Schema, s
|
70
83
|
end
|
@@ -72,7 +85,7 @@ describe Committee::Drivers do
|
|
72
85
|
|
73
86
|
describe 'load_from_yaml(schema_path)' do
|
74
87
|
it 'loads OpenAPI3' do
|
75
|
-
s = Committee::Drivers.load_from_yaml(open_api_3_schema_path)
|
88
|
+
s = Committee::Drivers.load_from_yaml(open_api_3_schema_path, parser_options:{strict_reference_validation: true})
|
76
89
|
assert_kind_of Committee::Drivers::Schema, s
|
77
90
|
assert_kind_of Committee::Drivers::OpenAPI3::Schema, s
|
78
91
|
end
|
@@ -80,7 +93,7 @@ describe Committee::Drivers do
|
|
80
93
|
|
81
94
|
describe 'load_from_data(schema_path)' do
|
82
95
|
it 'loads OpenAPI3' do
|
83
|
-
s = Committee::Drivers.load_from_data(open_api_3_data)
|
96
|
+
s = Committee::Drivers.load_from_data(open_api_3_data, parser_options:{strict_reference_validation: false})
|
84
97
|
assert_kind_of Committee::Drivers::Schema, s
|
85
98
|
assert_kind_of Committee::Drivers::OpenAPI3::Schema, s
|
86
99
|
end
|
@@ -102,21 +102,14 @@ describe Committee::Middleware::Base do
|
|
102
102
|
end
|
103
103
|
|
104
104
|
describe 'initialize option' do
|
105
|
-
it "
|
106
|
-
|
107
|
-
|
108
|
-
|
105
|
+
it "accepts OpenAPI3 parser config of strict_reference_validation and raises" do
|
106
|
+
assert_raises(OpenAPIParser::MissingReferenceError) do
|
107
|
+
Committee::Middleware::Base.new(nil, schema_path: open_api_3_invalid_reference_path, strict_reference_validation: true)
|
108
|
+
end
|
109
109
|
end
|
110
110
|
|
111
|
-
it "
|
112
|
-
|
113
|
-
b = Committee::Middleware::Base.new(nil, schema_path: open_api_2_schema_path, parse_response_by_content_type: false)
|
114
|
-
assert_kind_of Committee::Drivers::OpenAPI2::Schema, b.instance_variable_get(:@schema)
|
115
|
-
end
|
116
|
-
|
117
|
-
it "schema_path option with OpenAPI3" do
|
118
|
-
# TODO: delete when 5.0.0 released because default value changed
|
119
|
-
b = Committee::Middleware::Base.new(nil, schema_path: open_api_3_schema_path, parse_response_by_content_type: false)
|
111
|
+
it "does not raise by default even with invalid reference OpenAPI3 specification" do
|
112
|
+
b = Committee::Middleware::Base.new(nil, schema_path: open_api_3_invalid_reference_path, strict_reference_validation: false)
|
120
113
|
assert_kind_of Committee::Drivers::OpenAPI3::Schema, b.instance_variable_get(:@schema)
|
121
114
|
end
|
122
115
|
end
|
@@ -34,12 +34,12 @@ describe Committee::Middleware::RequestValidation do
|
|
34
34
|
params = { "datetime_string" => "2016-04-01T16:00:00.000+09:00" }
|
35
35
|
|
36
36
|
check_parameter = lambda { |env|
|
37
|
-
assert_equal DateTime, env['
|
37
|
+
assert_equal DateTime, env['test.query_hash']["datetime_string"].class
|
38
38
|
assert_equal String, env['rack.request.query_hash']["datetime_string"].class
|
39
39
|
[200, {}, []]
|
40
40
|
}
|
41
41
|
|
42
|
-
@app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_schema, coerce_date_times: true, query_hash_key: "
|
42
|
+
@app = new_rack_app_with_lambda(check_parameter, schema: open_api_3_schema, coerce_date_times: true, query_hash_key: "test.query_hash")
|
43
43
|
|
44
44
|
get "/string_params_coercer", params
|
45
45
|
assert_equal 200, last_response.status
|
@@ -49,7 +49,7 @@ describe Committee::Middleware::RequestValidation do
|
|
49
49
|
params = { "datetime_string" => "2016-04-01T16:00:00.000+09:00" }
|
50
50
|
|
51
51
|
check_parameter = lambda { |env|
|
52
|
-
|
52
|
+
assert_nil env['committee.query_hash']
|
53
53
|
assert_equal DateTime, env['rack.request.query_hash']["datetime_string"].class
|
54
54
|
[200, {}, []]
|
55
55
|
}
|
@@ -65,7 +65,7 @@ describe Committee::Middleware::RequestValidation do
|
|
65
65
|
|
66
66
|
@app = new_rack_app(schema: open_api_3_schema, allow_get_body: true)
|
67
67
|
|
68
|
-
get "/
|
68
|
+
get "/get_endpoint_with_required_parameter", { no_problem: true }, { input: params.to_json }
|
69
69
|
assert_equal 200, last_response.status
|
70
70
|
end
|
71
71
|
|
@@ -74,7 +74,7 @@ describe Committee::Middleware::RequestValidation do
|
|
74
74
|
|
75
75
|
@app = new_rack_app(schema: open_api_3_schema, allow_get_body: false)
|
76
76
|
|
77
|
-
get "/
|
77
|
+
get "/get_endpoint_with_required_parameter", { no_problem: true }, { input: params.to_json }
|
78
78
|
assert_equal 400, last_response.status
|
79
79
|
end
|
80
80
|
|
@@ -170,8 +170,7 @@ describe Committee::Middleware::RequestValidation do
|
|
170
170
|
}
|
171
171
|
|
172
172
|
check_parameter = lambda { |env|
|
173
|
-
|
174
|
-
hash = env["rack.request.query_hash"]
|
173
|
+
hash = env["committee.query_hash"]
|
175
174
|
assert_equal DateTime, hash['nested_array'].first['update_time'].class
|
176
175
|
assert_equal 1, hash['nested_array'].first['per_page']
|
177
176
|
|
@@ -377,8 +376,7 @@ describe Committee::Middleware::RequestValidation do
|
|
377
376
|
|
378
377
|
it "passes through a valid request for OpenAPI3" do
|
379
378
|
check_parameter = lambda { |env|
|
380
|
-
|
381
|
-
assert_equal 3, env['rack.request.query_hash']['limit'] #5.0.x-
|
379
|
+
assert_equal 3, env['committee.query_hash']['limit'] #5.0.x-
|
382
380
|
[200, {}, []]
|
383
381
|
}
|
384
382
|
|
@@ -418,11 +416,11 @@ describe Committee::Middleware::RequestValidation do
|
|
418
416
|
@app = new_rack_app_with_lambda(lambda do |env|
|
419
417
|
assert_equal env['committee.params']['integer'], 42
|
420
418
|
assert_equal env['committee.params'][:integer], 42
|
421
|
-
assert_equal env['
|
419
|
+
assert_equal env['committee.query_hash']['integer'], 42
|
422
420
|
#assert_equal env['rack.request.query_hash'][:integer], 42 # this isn't hash indifferent hash because we use rack.request.query_hash
|
423
421
|
[204, {}, []]
|
424
|
-
end, schema: open_api_3_schema)
|
425
|
-
|
422
|
+
end, schema: open_api_3_schema, parameter_overwite_by_rails_rule: false)
|
423
|
+
|
426
424
|
header "Content-Type", "application/json"
|
427
425
|
post '/overwrite_same_parameter?integer=42'
|
428
426
|
assert_equal 204, last_response.status
|
@@ -434,18 +432,17 @@ describe Committee::Middleware::RequestValidation do
|
|
434
432
|
assert_equal env['committee.params'][:integer], 21
|
435
433
|
assert_equal env['committee.request_body_hash']['integer'], 21
|
436
434
|
assert_equal env['committee.request_body_hash'][:integer], 21
|
437
|
-
assert_equal env['
|
438
|
-
#assert_equal env['rack.request.query_hash'][:integer], 21 # this isn't hash indifferent hash because we use rack.request.query_hash
|
435
|
+
assert_equal env['committee.query_hash']['integer'], 42
|
439
436
|
[204, {}, []]
|
440
|
-
end, schema: open_api_3_schema)
|
441
|
-
|
437
|
+
end, schema: open_api_3_schema, parameter_overwite_by_rails_rule: false)
|
438
|
+
|
442
439
|
params = {integer: 21}
|
443
|
-
|
440
|
+
|
444
441
|
header "Content-Type", "application/json"
|
445
442
|
post '/overwrite_same_parameter?integer=42', JSON.generate(params)
|
446
443
|
assert_equal 204, last_response.status
|
447
444
|
end
|
448
|
-
|
445
|
+
|
449
446
|
it "path parameter precedence over request body" do
|
450
447
|
@app = new_rack_app_with_lambda(lambda do |env|
|
451
448
|
assert_equal env['committee.params']['integer'], 84
|
@@ -454,13 +451,13 @@ describe Committee::Middleware::RequestValidation do
|
|
454
451
|
assert_equal env['committee.path_hash'][:integer], 84
|
455
452
|
assert_equal env['committee.request_body_hash']['integer'], 21
|
456
453
|
assert_equal env['committee.request_body_hash'][:integer], 21
|
457
|
-
assert_equal env['
|
454
|
+
assert_equal env['committee.query_hash']['integer'], 84 # we can't use query_parameter :(
|
458
455
|
#assert_equal env['rack.request.query_hash'][:integer], 21 # this isn't hash indifferent hash because we use rack.request.query_hash
|
459
456
|
[204, {}, []]
|
460
|
-
end, schema: open_api_3_schema)
|
461
|
-
|
457
|
+
end, schema: open_api_3_schema, parameter_overwite_by_rails_rule: false)
|
458
|
+
|
462
459
|
params = {integer: 21}
|
463
|
-
|
460
|
+
|
464
461
|
header "Content-Type", "application/json"
|
465
462
|
post '/overwrite_same_parameter/84?integer=42', JSON.generate(params)
|
466
463
|
assert_equal 204, last_response.status
|
@@ -476,8 +473,8 @@ describe Committee::Middleware::RequestValidation do
|
|
476
473
|
assert_equal env['committee.request_body_hash']['integer'], 21
|
477
474
|
assert_equal env['committee.request_body_hash'][:integer], 21
|
478
475
|
[204, {}, []]
|
479
|
-
end, schema: open_api_3_schema
|
480
|
-
|
476
|
+
end, schema: open_api_3_schema)
|
477
|
+
|
481
478
|
params = {integer: 21}
|
482
479
|
|
483
480
|
header "Content-Type", "application/json"
|
@@ -491,31 +488,31 @@ describe Committee::Middleware::RequestValidation do
|
|
491
488
|
assert_equal env['committee.params'][:integer], 42
|
492
489
|
assert_equal env['committee.request_body_hash']['integer'], 21
|
493
490
|
assert_equal env['committee.request_body_hash'][:integer], 21
|
494
|
-
assert_equal env['
|
491
|
+
assert_equal env['committee.query_hash']['integer'], 42
|
495
492
|
[204, {}, []]
|
496
|
-
end, schema: open_api_3_schema
|
497
|
-
|
493
|
+
end, schema: open_api_3_schema)
|
494
|
+
|
498
495
|
params = {integer: 21}
|
499
|
-
|
496
|
+
|
500
497
|
header "Content-Type", "application/json"
|
501
498
|
post '/overwrite_same_parameter?integer=42', JSON.generate(params)
|
502
499
|
assert_equal 204, last_response.status
|
503
500
|
end
|
504
|
-
|
501
|
+
|
505
502
|
it "path path parameter precedence over query parameter" do
|
506
503
|
@app = new_rack_app_with_lambda(lambda do |env|
|
507
504
|
assert_equal env['committee.params']['integer'], 84
|
508
505
|
assert_equal env['committee.params'][:integer], 84
|
509
506
|
assert_equal env['committee.request_body_hash']['integer'], 21
|
510
507
|
assert_equal env['committee.request_body_hash'][:integer], 21
|
511
|
-
assert_equal env['
|
508
|
+
assert_equal env['committee.query_hash']['integer'], 84 # we can't use query_parameter :(
|
512
509
|
assert_equal env['committee.path_hash']['integer'], 84
|
513
510
|
assert_equal env['committee.path_hash'][:integer], 84
|
514
511
|
[204, {}, []]
|
515
|
-
end, schema: open_api_3_schema
|
516
|
-
|
512
|
+
end, schema: open_api_3_schema)
|
513
|
+
|
517
514
|
params = {integer: 21}
|
518
|
-
|
515
|
+
|
519
516
|
header "Content-Type", "application/json"
|
520
517
|
post '/overwrite_same_parameter/84?integer=42', JSON.generate(params)
|
521
518
|
assert_equal 204, last_response.status
|
@@ -524,11 +521,10 @@ describe Committee::Middleware::RequestValidation do
|
|
524
521
|
|
525
522
|
it "unpacker test" do
|
526
523
|
@app = new_rack_app_with_lambda(lambda do |env|
|
527
|
-
assert_equal env['committee.params']['integer']
|
528
|
-
assert_equal env['committee.params'][:integer]
|
529
|
-
|
530
|
-
assert_equal env['
|
531
|
-
# assert_equal env['rack.request.query_hash'][:integer], 42
|
524
|
+
assert_equal '21', env['committee.params']['integer'] # query parameter has precedence
|
525
|
+
assert_equal '21', env['committee.params'][:integer]
|
526
|
+
assert_equal '21', env['rack.request.query_hash']['integer']
|
527
|
+
assert_equal 42, env['committee.request_body_hash']['integer']
|
532
528
|
[204, {}, []]
|
533
529
|
end, schema: open_api_3_schema, raise: true)
|
534
530
|
|
@@ -541,11 +537,11 @@ describe Committee::Middleware::RequestValidation do
|
|
541
537
|
@app = new_rack_app(schema: open_api_3_schema)
|
542
538
|
|
543
539
|
e = assert_raises(RuntimeError) {
|
544
|
-
|
540
|
+
custom_request('TRACE', "/characters")
|
545
541
|
}
|
546
542
|
|
547
|
-
assert_equal 'Committee OpenAPI3 not support
|
548
|
-
end
|
543
|
+
assert_equal 'Committee OpenAPI3 not support trace method', e.message
|
544
|
+
end
|
549
545
|
|
550
546
|
describe 'check header' do
|
551
547
|
[
|
@@ -562,7 +558,7 @@ describe Committee::Middleware::RequestValidation do
|
|
562
558
|
value = h[:value]
|
563
559
|
expected = h[:expected]
|
564
560
|
describe "when #{check_header}" do
|
565
|
-
%w(get post put patch delete).each do |method|
|
561
|
+
%w(get post put patch delete options).each do |method|
|
566
562
|
describe method do
|
567
563
|
describe description do
|
568
564
|
it (expected[:error].nil? ? 'should pass' : 'should fail') do
|
@@ -306,21 +306,6 @@ describe Committee::Middleware::RequestValidation do
|
|
306
306
|
assert_equal 200, last_response.status
|
307
307
|
end
|
308
308
|
|
309
|
-
it "calls error_handler (has a arg) when request is invalid" do
|
310
|
-
called_err = nil
|
311
|
-
pr = ->(e) { called_err = e }
|
312
|
-
@app = new_rack_app(schema: hyper_schema, error_handler: pr)
|
313
|
-
header "Content-Type", "application/json"
|
314
|
-
params = {
|
315
|
-
"name" => 1
|
316
|
-
}
|
317
|
-
_, err = capture_io do
|
318
|
-
post "/apps", JSON.generate(params)
|
319
|
-
end
|
320
|
-
assert_kind_of Committee::InvalidRequest, called_err
|
321
|
-
assert_match(/\[DEPRECATION\]/i, err)
|
322
|
-
end
|
323
|
-
|
324
309
|
it "calls error_handler (has two args) when request is invalid" do
|
325
310
|
called_err = nil
|
326
311
|
pr = ->(e, _env) { called_err = e }
|
@@ -341,18 +326,6 @@ describe Committee::Middleware::RequestValidation do
|
|
341
326
|
assert_match(/valid json/i, last_response.body)
|
342
327
|
end
|
343
328
|
|
344
|
-
it "calls error_handler (has a arg) when it rescues JSON errors" do
|
345
|
-
called_err = nil
|
346
|
-
pr = ->(e) { called_err = e }
|
347
|
-
@app = new_rack_app(schema: hyper_schema, error_handler: pr)
|
348
|
-
header "Content-Type", "application/json"
|
349
|
-
_, err = capture_io do
|
350
|
-
post "/apps", "{x:y}"
|
351
|
-
end
|
352
|
-
assert_kind_of JSON::ParserError, called_err
|
353
|
-
assert_match(/\[DEPRECATION\]/i, err)
|
354
|
-
end
|
355
|
-
|
356
329
|
it "calls error_handler (has two args) when it rescues JSON errors" do
|
357
330
|
called_err = nil
|
358
331
|
pr = ->(e, _env) { called_err = e }
|
@@ -435,7 +408,7 @@ describe Committee::Middleware::RequestValidation do
|
|
435
408
|
assert_equal 200, last_response.status
|
436
409
|
end
|
437
410
|
|
438
|
-
it "
|
411
|
+
it "coerce form params" do
|
439
412
|
check_parameter = lambda { |env|
|
440
413
|
assert_equal 3, env['committee.params']['age']
|
441
414
|
assert_equal 3, env['committee.request_body_hash']['age']
|
@@ -64,6 +64,12 @@ describe Committee::Middleware::ResponseValidation do
|
|
64
64
|
assert_equal 204, last_response.status
|
65
65
|
end
|
66
66
|
|
67
|
+
it "passes through a 304 (not modified) response" do
|
68
|
+
@app = new_response_rack("", {}, {schema: open_api_3_schema}, {status: 304})
|
69
|
+
post "/validate"
|
70
|
+
assert_equal 304, last_response.status
|
71
|
+
end
|
72
|
+
|
67
73
|
it "passes through a valid response with prefix" do
|
68
74
|
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, schema: open_api_3_schema, prefix: "/v1")
|
69
75
|
get "/v1/characters"
|
@@ -86,7 +92,7 @@ describe Committee::Middleware::ResponseValidation do
|
|
86
92
|
end
|
87
93
|
end
|
88
94
|
|
89
|
-
it "not parameter
|
95
|
+
it "not parameter request" do
|
90
96
|
@app = new_response_rack({integer: '1'}.to_json, {}, schema: open_api_3_schema, raise: true)
|
91
97
|
|
92
98
|
assert_raises(Committee::InvalidResponse) do
|
@@ -136,7 +142,7 @@ describe Committee::Middleware::ResponseValidation do
|
|
136
142
|
header = h[:header]
|
137
143
|
expected = h[:expected]
|
138
144
|
describe "when #{check_header}" do
|
139
|
-
%w(get post put patch delete).each do |method|
|
145
|
+
%w(get post put patch delete options).each do |method|
|
140
146
|
describe method do
|
141
147
|
describe description do
|
142
148
|
if expected[:error].nil?
|
@@ -228,6 +234,42 @@ describe Committee::Middleware::ResponseValidation do
|
|
228
234
|
end
|
229
235
|
end
|
230
236
|
|
237
|
+
it "strict and invalid status" do
|
238
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, {schema: open_api_3_schema, strict: true}, {status: 201})
|
239
|
+
get "/characters"
|
240
|
+
assert_equal 500, last_response.status
|
241
|
+
end
|
242
|
+
|
243
|
+
it "strict and invalid status with raise" do
|
244
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, {schema: open_api_3_schema, strict: true, raise: true}, {status: 201})
|
245
|
+
|
246
|
+
assert_raises(Committee::InvalidResponse) do
|
247
|
+
get "/characters"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
it "strict and invalid content type" do
|
252
|
+
@app = new_response_rack("abc",
|
253
|
+
{},
|
254
|
+
{schema: open_api_3_schema, strict: true},
|
255
|
+
{content_type: 'application/text'}
|
256
|
+
)
|
257
|
+
get "/characters"
|
258
|
+
assert_equal 500, last_response.status
|
259
|
+
end
|
260
|
+
|
261
|
+
it "strict and invalid content type with raise" do
|
262
|
+
@app = new_response_rack("abc",
|
263
|
+
{},
|
264
|
+
{schema: open_api_3_schema, strict: true, raise: true},
|
265
|
+
{content_type: 'application/text'}
|
266
|
+
)
|
267
|
+
|
268
|
+
assert_raises(Committee::InvalidResponse) do
|
269
|
+
get "/characters"
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
231
273
|
private
|
232
274
|
|
233
275
|
def new_response_rack(response, headers = {}, options = {}, rack_options = {})
|
@@ -235,8 +277,9 @@ describe Committee::Middleware::ResponseValidation do
|
|
235
277
|
options[:parse_response_by_content_type] = true if options[:parse_response_by_content_type] == nil
|
236
278
|
|
237
279
|
status = rack_options[:status] || 200
|
280
|
+
content_type = rack_options[:content_type] || "application/json"
|
238
281
|
headers = {
|
239
|
-
"Content-Type" =>
|
282
|
+
"Content-Type" => content_type
|
240
283
|
}.merge(headers)
|
241
284
|
Rack::Builder.new {
|
242
285
|
use Committee::Middleware::ResponseValidation, options
|
@@ -85,6 +85,12 @@ describe Committee::Middleware::ResponseValidation do
|
|
85
85
|
assert_equal 204, last_response.status
|
86
86
|
end
|
87
87
|
|
88
|
+
it "passes through a 304 (not modified) response" do
|
89
|
+
@app = new_rack_app("", {}, app_status: 304, schema: hyper_schema)
|
90
|
+
get "/apps"
|
91
|
+
assert_equal 304, last_response.status
|
92
|
+
end
|
93
|
+
|
88
94
|
it "skip validation when 4xx" do
|
89
95
|
@app = new_rack_app("[{x:y}]", {}, schema: hyper_schema, validate_success_only: true, app_status: 400)
|
90
96
|
get "/apps"
|
@@ -99,17 +105,6 @@ describe Committee::Middleware::ResponseValidation do
|
|
99
105
|
assert_match(/valid json/i, last_response.body)
|
100
106
|
end
|
101
107
|
|
102
|
-
it "calls error_handler (has a arg) when it rescues JSON errors" do
|
103
|
-
called_err = nil
|
104
|
-
pr = ->(e) { called_err = e }
|
105
|
-
@app = new_rack_app("[{x:y}]", {}, schema: hyper_schema, error_handler: pr)
|
106
|
-
_, err = capture_io do
|
107
|
-
get "/apps"
|
108
|
-
end
|
109
|
-
assert_kind_of JSON::ParserError, called_err
|
110
|
-
assert_match(/\[DEPRECATION\]/i, err)
|
111
|
-
end
|
112
|
-
|
113
108
|
it "calls error_handler (has two args) when it rescues JSON errors" do
|
114
109
|
called_err = nil
|
115
110
|
pr = ->(e, _env) { called_err = e }
|
@@ -132,20 +127,6 @@ describe Committee::Middleware::ResponseValidation do
|
|
132
127
|
end
|
133
128
|
end
|
134
129
|
|
135
|
-
it "calls error_handler (has a arg) when it rescues JSON errors" do
|
136
|
-
called_err = nil
|
137
|
-
pr = ->(e) { called_err = e }
|
138
|
-
@app = new_rack_app("[{x:y}]", {}, raise: true, schema: hyper_schema, error_handler: pr)
|
139
|
-
assert_raises(Committee::InvalidResponse) do
|
140
|
-
_, err = capture_io do
|
141
|
-
get "/apps"
|
142
|
-
end
|
143
|
-
|
144
|
-
assert_kind_of JSON::ParserError, called_err
|
145
|
-
assert_match(/\[DEPRECATION\]/i, err)
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
130
|
it "calls error_handler (has two args) when it rescues JSON errors" do
|
150
131
|
called_err = nil
|
151
132
|
pr = ->(e, _env) { called_err = e }
|
@@ -38,6 +38,11 @@ describe Committee::SchemaValidator::HyperSchema::ResponseValidator do
|
|
38
38
|
call
|
39
39
|
end
|
40
40
|
|
41
|
+
it "passes through a 304 Not Modified response" do
|
42
|
+
@status, @headers, @data = 304, {}, nil
|
43
|
+
call
|
44
|
+
end
|
45
|
+
|
41
46
|
it "passes through a valid list response for for rel instances links" do
|
42
47
|
@link = @list_link
|
43
48
|
|
@@ -91,6 +96,11 @@ describe Committee::SchemaValidator::HyperSchema::ResponseValidator do
|
|
91
96
|
call
|
92
97
|
end
|
93
98
|
|
99
|
+
it "allows no Content-Type for 304 Not Modified" do
|
100
|
+
@status, @headers = 304, {}
|
101
|
+
call
|
102
|
+
end
|
103
|
+
|
94
104
|
it "raises errors generated by json_schema" do
|
95
105
|
@data.merge!("name" => "%@!")
|
96
106
|
e = assert_raises(Committee::InvalidResponse) { call }
|
@@ -11,7 +11,7 @@ describe Committee::SchemaValidator::HyperSchema::StringParamsCoercer do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "doesn't coerce params not in the schema" do
|
14
|
-
check_convert("
|
14
|
+
check_convert("owner", "admin", "admin")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "skips values for string param" do
|