committee 4.4.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/lib/committee/drivers/open_api_2/driver.rb +1 -1
  3. data/lib/committee/drivers/open_api_2/parameter_schema_builder.rb +1 -1
  4. data/lib/committee/drivers.rb +21 -9
  5. data/lib/committee/middleware/base.rb +5 -4
  6. data/lib/committee/middleware/request_validation.rb +1 -8
  7. data/lib/committee/middleware/response_validation.rb +13 -14
  8. data/lib/committee/request_unpacker.rb +1 -1
  9. data/lib/committee/schema_validator/hyper_schema/response_validator.rb +8 -2
  10. data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +36 -33
  11. data/lib/committee/schema_validator/open_api_3/request_validator.rb +11 -2
  12. data/lib/committee/schema_validator/open_api_3.rb +28 -15
  13. data/lib/committee/schema_validator/option.rb +5 -13
  14. data/lib/committee/schema_validator.rb +1 -1
  15. data/lib/committee/test/methods.rb +2 -7
  16. data/lib/committee/version.rb +5 -0
  17. data/lib/committee.rb +9 -2
  18. data/test/committee_test.rb +28 -2
  19. data/test/drivers/open_api_3/driver_test.rb +1 -1
  20. data/test/drivers_test.rb +20 -7
  21. data/test/middleware/base_test.rb +6 -13
  22. data/test/middleware/request_validation_open_api_3_test.rb +117 -41
  23. data/test/middleware/request_validation_test.rb +1 -28
  24. data/test/middleware/response_validation_open_api_3_test.rb +46 -3
  25. data/test/middleware/response_validation_test.rb +6 -25
  26. data/test/schema_validator/hyper_schema/response_validator_test.rb +10 -0
  27. data/test/schema_validator/hyper_schema/string_params_coercer_test.rb +1 -1
  28. data/test/schema_validator/open_api_3/operation_wrapper_test.rb +55 -11
  29. data/test/schema_validator/open_api_3/request_validator_test.rb +25 -1
  30. data/test/schema_validator/open_api_3/response_validator_test.rb +14 -0
  31. data/test/test/methods_new_version_test.rb +1 -1
  32. data/test/test/methods_test.rb +11 -31
  33. data/test/test_helper.rb +15 -5
  34. metadata +9 -14
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 "schema_path option with hyper-schema" do
106
- # TODO: delete when 5.0.0 released because default value changed
107
- b = Committee::Middleware::Base.new(nil, schema_path: hyper_schema_schema_path, parse_response_by_content_type: false)
108
- assert_kind_of Committee::Drivers::HyperSchema::Schema, b.instance_variable_get(:@schema)
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 "schema_path option with OpenAPI2" do
112
- # TODO: delete when 5.0.0 released because default value changed
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['committee.query_hash']["datetime_string"].class
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: "committee.query_hash")
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
- assert_equal nil, env['committee.query_hash']
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 "/get_endpoint_with_requered_parameter", { no_problem: true }, { input: params.to_json }
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 "/get_endpoint_with_requered_parameter", { no_problem: true }, { input: params.to_json }
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
- # hash = env["committee.query_hash"] # 5.0.x-
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
- # assert_equal 3, env['committee.query_hash']['limit'] #5.0.x-
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
 
@@ -412,43 +410,121 @@ describe Committee::Middleware::RequestValidation do
412
410
  get "/coerce_path_params/1"
413
411
  end
414
412
 
415
- it "corce string and save path hash" do
416
- @app = new_rack_app_with_lambda(lambda do |env|
417
- assert_equal env['committee.params']['integer'], 21
418
- assert_equal env['committee.params'][:integer], 21
419
- assert_equal env['committee.path_hash']['integer'], 21
420
- assert_equal env['committee.path_hash'][:integer], 21
421
- [204, {}, []]
422
- end, schema: open_api_3_schema)
413
+ describe "overwrite same parameter (old rule)" do
414
+ # (high priority) path_hash_key -> request_body_hash -> query_param
415
+ it "set query parameter to committee.params and query hash" do
416
+ @app = new_rack_app_with_lambda(lambda do |env|
417
+ assert_equal env['committee.params']['integer'], 42
418
+ assert_equal env['committee.params'][:integer], 42
419
+ assert_equal env['committee.query_hash']['integer'], 42
420
+ #assert_equal env['rack.request.query_hash'][:integer], 42 # this isn't hash indifferent hash because we use rack.request.query_hash
421
+ [204, {}, []]
422
+ end, schema: open_api_3_schema, parameter_overwite_by_rails_rule: false)
423
+
424
+ header "Content-Type", "application/json"
425
+ post '/overwrite_same_parameter?integer=42'
426
+ assert_equal 204, last_response.status
427
+ end
423
428
 
424
- header "Content-Type", "application/json"
425
- post '/parameter_option_test/21'
426
- assert_equal 204, last_response.status
429
+ it "request body precedence over query parameter" do
430
+ @app = new_rack_app_with_lambda(lambda do |env|
431
+ assert_equal env['committee.params']['integer'], 21
432
+ assert_equal env['committee.params'][:integer], 21
433
+ assert_equal env['committee.request_body_hash']['integer'], 21
434
+ assert_equal env['committee.request_body_hash'][:integer], 21
435
+ assert_equal env['committee.query_hash']['integer'], 42
436
+ [204, {}, []]
437
+ end, schema: open_api_3_schema, parameter_overwite_by_rails_rule: false)
438
+
439
+ params = {integer: 21}
440
+
441
+ header "Content-Type", "application/json"
442
+ post '/overwrite_same_parameter?integer=42', JSON.generate(params)
443
+ assert_equal 204, last_response.status
444
+ end
445
+
446
+ it "path parameter precedence over request body" do
447
+ @app = new_rack_app_with_lambda(lambda do |env|
448
+ assert_equal env['committee.params']['integer'], 84
449
+ assert_equal env['committee.params'][:integer], 84
450
+ assert_equal env['committee.path_hash']['integer'], 84
451
+ assert_equal env['committee.path_hash'][:integer], 84
452
+ assert_equal env['committee.request_body_hash']['integer'], 21
453
+ assert_equal env['committee.request_body_hash'][:integer], 21
454
+ assert_equal env['committee.query_hash']['integer'], 84 # we can't use query_parameter :(
455
+ #assert_equal env['rack.request.query_hash'][:integer], 21 # this isn't hash indifferent hash because we use rack.request.query_hash
456
+ [204, {}, []]
457
+ end, schema: open_api_3_schema, parameter_overwite_by_rails_rule: false)
458
+
459
+ params = {integer: 21}
460
+
461
+ header "Content-Type", "application/json"
462
+ post '/overwrite_same_parameter/84?integer=42', JSON.generate(params)
463
+ assert_equal 204, last_response.status
464
+ end
427
465
  end
428
466
 
429
- it "corce string and save request body hash" do
430
- @app = new_rack_app_with_lambda(lambda do |env|
431
- assert_equal env['committee.params']['integer'], 21 # use path parameter
432
- assert_equal env['committee.params'][:integer], 21
433
- assert_equal env['committee.request_body_hash']['integer'], 42
434
- assert_equal env['committee.request_body_hash'][:integer], 42
435
- [204, {}, []]
436
- end, schema: open_api_3_schema)
467
+ describe "overwrite same parameter (new rule and seme to Rails)" do
468
+ # (high priority) path_hash_key -> query_param -> request_body_hash
469
+ it "set request body to committee.params and query hash" do
470
+ @app = new_rack_app_with_lambda(lambda do |env|
471
+ assert_equal env['committee.params']['integer'], 21
472
+ assert_equal env['committee.params'][:integer], 21
473
+ assert_equal env['committee.request_body_hash']['integer'], 21
474
+ assert_equal env['committee.request_body_hash'][:integer], 21
475
+ [204, {}, []]
476
+ end, schema: open_api_3_schema)
437
477
 
438
- params = {integer: 42}
478
+ params = {integer: 21}
439
479
 
440
- header "Content-Type", "application/json"
441
- post '/parameter_option_test/21', JSON.generate(params)
442
- assert_equal 204, last_response.status
480
+ header "Content-Type", "application/json"
481
+ post '/overwrite_same_parameter', JSON.generate(params)
482
+ assert_equal 204, last_response.status
483
+ end
484
+
485
+ it "query parameter precedence over request body" do
486
+ @app = new_rack_app_with_lambda(lambda do |env|
487
+ assert_equal env['committee.params']['integer'], 42
488
+ assert_equal env['committee.params'][:integer], 42
489
+ assert_equal env['committee.request_body_hash']['integer'], 21
490
+ assert_equal env['committee.request_body_hash'][:integer], 21
491
+ assert_equal env['committee.query_hash']['integer'], 42
492
+ [204, {}, []]
493
+ end, schema: open_api_3_schema)
494
+
495
+ params = {integer: 21}
496
+
497
+ header "Content-Type", "application/json"
498
+ post '/overwrite_same_parameter?integer=42', JSON.generate(params)
499
+ assert_equal 204, last_response.status
500
+ end
501
+
502
+ it "path path parameter precedence over query parameter" do
503
+ @app = new_rack_app_with_lambda(lambda do |env|
504
+ assert_equal env['committee.params']['integer'], 84
505
+ assert_equal env['committee.params'][:integer], 84
506
+ assert_equal env['committee.request_body_hash']['integer'], 21
507
+ assert_equal env['committee.request_body_hash'][:integer], 21
508
+ assert_equal env['committee.query_hash']['integer'], 84 # we can't use query_parameter :(
509
+ assert_equal env['committee.path_hash']['integer'], 84
510
+ assert_equal env['committee.path_hash'][:integer], 84
511
+ [204, {}, []]
512
+ end, schema: open_api_3_schema)
513
+
514
+ params = {integer: 21}
515
+
516
+ header "Content-Type", "application/json"
517
+ post '/overwrite_same_parameter/84?integer=42', JSON.generate(params)
518
+ assert_equal 204, last_response.status
519
+ end
443
520
  end
444
521
 
445
522
  it "unpacker test" do
446
523
  @app = new_rack_app_with_lambda(lambda do |env|
447
- assert_equal env['committee.params']['integer'], 42
448
- assert_equal env['committee.params'][:integer], 42
449
- # overwrite by request body...
450
- assert_equal env['rack.request.query_hash']['integer'], 42
451
- # 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']
452
528
  [204, {}, []]
453
529
  end, schema: open_api_3_schema, raise: true)
454
530
 
@@ -461,11 +537,11 @@ describe Committee::Middleware::RequestValidation do
461
537
  @app = new_rack_app(schema: open_api_3_schema)
462
538
 
463
539
  e = assert_raises(RuntimeError) {
464
- head "/characters", {}
540
+ custom_request('TRACE', "/characters")
465
541
  }
466
542
 
467
- assert_equal 'Committee OpenAPI3 not support head method', e.message
468
- end
543
+ assert_equal 'Committee OpenAPI3 not support trace method', e.message
544
+ end
469
545
 
470
546
  describe 'check header' do
471
547
  [
@@ -482,7 +558,7 @@ describe Committee::Middleware::RequestValidation do
482
558
  value = h[:value]
483
559
  expected = h[:expected]
484
560
  describe "when #{check_header}" do
485
- %w(get post put patch delete).each do |method|
561
+ %w(get post put patch delete options).each do |method|
486
562
  describe method do
487
563
  describe description do
488
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 "corce form params" do
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 requset" do
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" => "application/json"
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("onwer", "admin", "admin")
14
+ check_convert("owner", "admin", "admin")
15
15
  end
16
16
 
17
17
  it "skips values for string param" do