committee 5.4.0 → 5.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/committee/bin/committee_stub.rb +1 -6
  3. data/lib/committee/drivers/open_api_2/driver.rb +21 -33
  4. data/lib/committee/drivers/open_api_2/link.rb +8 -1
  5. data/lib/committee/drivers/open_api_2/parameter_schema_builder.rb +1 -2
  6. data/lib/committee/drivers/open_api_2/schema_builder.rb +2 -5
  7. data/lib/committee/drivers.rb +1 -1
  8. data/lib/committee/errors.rb +2 -2
  9. data/lib/committee/middleware/base.rb +2 -2
  10. data/lib/committee/middleware/request_validation.rb +2 -1
  11. data/lib/committee/middleware/stub.rb +1 -1
  12. data/lib/committee/request_unpacker.rb +4 -4
  13. data/lib/committee/schema_validator/hyper_schema/parameter_coercer.rb +41 -41
  14. data/lib/committee/schema_validator/hyper_schema/request_validator.rb +1 -2
  15. data/lib/committee/schema_validator/hyper_schema/response_validator.rb +15 -7
  16. data/lib/committee/schema_validator/hyper_schema/string_params_coercer.rb +60 -60
  17. data/lib/committee/schema_validator/hyper_schema.rb +64 -59
  18. data/lib/committee/schema_validator/open_api_3/request_validator.rb +3 -3
  19. data/lib/committee/schema_validator/open_api_3.rb +10 -5
  20. data/lib/committee/schema_validator/option.rb +1 -6
  21. data/lib/committee/test/schema_coverage.rb +1 -7
  22. data/lib/committee/utils.rb +20 -20
  23. data/lib/committee/version.rb +1 -1
  24. data/test/bin/committee_stub_test.rb +1 -5
  25. data/test/committee_test.rb +0 -1
  26. data/test/drivers/hyper_schema/driver_test.rb +0 -1
  27. data/test/drivers/open_api_2/driver_test.rb +1 -3
  28. data/test/drivers/open_api_2/header_schema_builder_test.rb +1 -9
  29. data/test/drivers/open_api_2/link_test.rb +1 -2
  30. data/test/drivers/open_api_2/parameter_schema_builder_test.rb +6 -45
  31. data/test/drivers/open_api_3/driver_test.rb +5 -5
  32. data/test/drivers_test.rb +19 -28
  33. data/test/middleware/base_test.rb +13 -36
  34. data/test/middleware/request_validation_open_api_3_test.rb +24 -47
  35. data/test/middleware/request_validation_test.rb +19 -53
  36. data/test/middleware/response_validation_open_api_3_test.rb +33 -25
  37. data/test/middleware/response_validation_test.rb +24 -15
  38. data/test/middleware/stub_test.rb +5 -12
  39. data/test/request_unpacker_test.rb +19 -66
  40. data/test/schema_validator/hyper_schema/parameter_coercer_test.rb +3 -3
  41. data/test/schema_validator/hyper_schema/request_validator_test.rb +7 -17
  42. data/test/schema_validator/hyper_schema/response_generator_test.rb +24 -18
  43. data/test/schema_validator/hyper_schema/response_validator_test.rb +3 -7
  44. data/test/schema_validator/hyper_schema/string_params_coercer_test.rb +2 -2
  45. data/test/schema_validator/open_api_3/operation_wrapper_test.rb +16 -17
  46. data/test/schema_validator/open_api_3/request_validator_test.rb +15 -19
  47. data/test/schema_validator/open_api_3/response_validator_test.rb +2 -4
  48. data/test/test/methods_new_version_test.rb +2 -2
  49. data/test/test/methods_test.rb +11 -11
  50. data/test/test/schema_coverage_test.rb +4 -17
  51. data/test/test_helper.rb +6 -13
  52. data/test/validation_error_test.rb +1 -5
  53. metadata +6 -20
@@ -10,12 +10,8 @@ describe Committee::Middleware::Base do
10
10
  end
11
11
 
12
12
  it "accepts just a schema object" do
13
- @app = new_rack_app(
14
- schema: hyper_schema
15
- )
16
- params = {
17
- "name" => "cloudnasium"
18
- }
13
+ @app = new_rack_app(schema: hyper_schema)
14
+ params = { "name" => "cloudnasium" }
19
15
  header "Content-Type", "application/json"
20
16
  post "/apps", JSON.generate(params)
21
17
  assert_equal 200, last_response.status
@@ -23,69 +19,50 @@ describe Committee::Middleware::Base do
23
19
 
24
20
  it "accepts just a OpenAPI3 schema object" do
25
21
  @app = new_rack_app(schema: open_api_3_schema)
26
- params = {
27
- "name" => "cloudnasium"
28
- }
22
+ params = { "name" => "cloudnasium" }
29
23
  header "Content-Type", "application/json"
30
24
  post "/apps", JSON.generate(params)
31
25
  assert_equal 200, last_response.status
32
26
  end
33
27
 
34
28
  it "doesn't accept a schema string" do
35
- @app = new_rack_app(
36
- schema: JSON.dump(hyper_schema_data)
37
- )
38
- params = {
39
- "name" => "cloudnasium"
40
- }
29
+ @app = new_rack_app(schema: JSON.dump(hyper_schema_data))
30
+ params = { "name" => "cloudnasium" }
41
31
  header "Content-Type", "application/json"
42
32
 
43
33
  e = assert_raises(ArgumentError) do
44
34
  post "/apps", JSON.generate(params)
45
35
  end
46
36
 
47
- assert_equal "Committee: schema expected to be an instance " +
48
- "of Committee::Drivers::Schema.", e.message
37
+ assert_equal "Committee: schema expected to be an instance " + "of Committee::Drivers::Schema.", e.message
49
38
  end
50
39
 
51
40
  it "doesn't accept a schema hash" do
52
- @app = new_rack_app(
53
- schema: hyper_schema_data
54
- )
55
- params = {
56
- "name" => "cloudnasium"
57
- }
41
+ @app = new_rack_app(schema: hyper_schema_data)
42
+ params = { "name" => "cloudnasium" }
58
43
  header "Content-Type", "application/json"
59
44
 
60
45
  e = assert_raises(ArgumentError) do
61
46
  post "/apps", JSON.generate(params)
62
47
  end
63
48
 
64
- assert_equal "Committee: schema expected to be an instance " +
65
- "of Committee::Drivers::Schema.", e.message
49
+ assert_equal "Committee: schema expected to be an instance " + "of Committee::Drivers::Schema.", e.message
66
50
  end
67
51
 
68
52
  it "doesn't accept a JsonSchema::Schema object" do
69
- @app = new_rack_app(
70
- schema: JsonSchema.parse!(hyper_schema_data)
71
- )
72
- params = {
73
- "name" => "cloudnasium"
74
- }
53
+ @app = new_rack_app(schema: JsonSchema.parse!(hyper_schema_data))
54
+ params = { "name" => "cloudnasium" }
75
55
  header "Content-Type", "application/json"
76
56
 
77
57
  e = assert_raises(ArgumentError) do
78
58
  post "/apps", JSON.generate(params)
79
59
  end
80
60
 
81
- assert_equal "Committee: schema expected to be an instance " +
82
- "of Committee::Drivers::Schema.", e.message
61
+ assert_equal "Committee: schema expected to be an instance " + "of Committee::Drivers::Schema.", e.message
83
62
  end
84
63
 
85
64
  it "doesn't accept other schema types" do
86
- @app = new_rack_app(
87
- schema: 7,
88
- )
65
+ @app = new_rack_app(schema: 7,)
89
66
  e = assert_raises(ArgumentError) do
90
67
  post "/apps"
91
68
  end
@@ -11,9 +11,7 @@ describe Committee::Middleware::RequestValidation do
11
11
 
12
12
  it "OpenAPI3 pass through a valid request" do
13
13
  @app = new_rack_app(schema: open_api_3_schema)
14
- params = {
15
- "string_post_1" => "cloudnasium"
16
- }
14
+ params = { "string_post_1" => "cloudnasium" }
17
15
  header "Content-Type", "application/json"
18
16
  post "/characters", JSON.generate(params)
19
17
 
@@ -22,12 +20,12 @@ describe Committee::Middleware::RequestValidation do
22
20
 
23
21
  it "not parameter request" do
24
22
  check_parameter_string = lambda { |_|
25
- [200, {integer: 1}, []]
23
+ [200, { integer: 1 }, []]
26
24
  }
27
25
 
28
26
  @app = new_rack_app_with_lambda(check_parameter_string, schema: open_api_3_schema)
29
27
 
30
- put "/validate_no_parameter", {no_schema: 'no'}
28
+ put "/validate_no_parameter", { no_schema: 'no' }
31
29
  end
32
30
 
33
31
  it "passes given a datetime and with coerce_date_times enabled on GET endpoint" do
@@ -150,9 +148,7 @@ describe Committee::Middleware::RequestValidation do
150
148
 
151
149
  it "passes given an invalid datetime string with coerce_date_times enabled" do
152
150
  @app = new_rack_app(schema: open_api_3_schema, coerce_date_times: true)
153
- params = {
154
- "datetime_string" => "invalid_datetime_format"
155
- }
151
+ params = { "datetime_string" => "invalid_datetime_format" }
156
152
  get "/string_params_coercer", params
157
153
 
158
154
  assert_equal 400, last_response.status
@@ -160,14 +156,7 @@ describe Committee::Middleware::RequestValidation do
160
156
  end
161
157
 
162
158
  it "passes a nested object with recursive option" do
163
- params = {
164
- "nested_array" => [
165
- {
166
- "update_time" => "2016-04-01T16:00:00.000+09:00",
167
- "per_page" => "1",
168
- }
169
- ],
170
- }
159
+ params = { "nested_array" => [{ "update_time" => "2016-04-01T16:00:00.000+09:00", "per_page" => "1", }], }
171
160
 
172
161
  check_parameter = lambda { |env|
173
162
  hash = env["committee.query_hash"]
@@ -248,9 +237,7 @@ describe Committee::Middleware::RequestValidation do
248
237
  [200, {}, []]
249
238
  }
250
239
 
251
- @app = new_rack_app_with_lambda(check_parameter,
252
- coerce_date_times: true,
253
- schema: open_api_3_schema)
240
+ @app = new_rack_app_with_lambda(check_parameter, coerce_date_times: true, schema: open_api_3_schema)
254
241
 
255
242
  header "Content-Type", "application/json"
256
243
  post "/string_params_coercer", JSON.generate(params)
@@ -260,9 +247,7 @@ describe Committee::Middleware::RequestValidation do
260
247
  it "OpenAPI3 detects an invalid request" do
261
248
  @app = new_rack_app(schema: open_api_3_schema, strict: true)
262
249
  header "Content-Type", "application/json"
263
- params = {
264
- "string_post_1" => 1
265
- }
250
+ params = { "string_post_1" => 1 }
266
251
  post "/characters", JSON.generate(params)
267
252
  assert_equal 400, last_response.status
268
253
  assert_match(/expected string, but received Integer:/i, last_response.body)
@@ -278,9 +263,7 @@ describe Committee::Middleware::RequestValidation do
278
263
 
279
264
  it "take a prefix" do
280
265
  @app = new_rack_app(prefix: "/v1", schema: open_api_3_schema)
281
- params = {
282
- "string_post_1" => "cloudnasium"
283
- }
266
+ params = { "string_post_1" => "cloudnasium" }
284
267
  header "Content-Type", "application/json"
285
268
  post "/v1/characters", JSON.generate(params)
286
269
  assert_equal 200, last_response.status
@@ -288,9 +271,7 @@ describe Committee::Middleware::RequestValidation do
288
271
 
289
272
  it "take a prefix with invalid data" do
290
273
  @app = new_rack_app(prefix: "/v1", schema: open_api_3_schema)
291
- params = {
292
- "string_post_1" => 1
293
- }
274
+ params = { "string_post_1" => 1 }
294
275
  header "Content-Type", "application/json"
295
276
  post "/v1/characters", JSON.generate(params)
296
277
  assert_equal 400, last_response.status
@@ -299,9 +280,7 @@ describe Committee::Middleware::RequestValidation do
299
280
 
300
281
  it "ignores paths outside the prefix" do
301
282
  @app = new_rack_app(prefix: "/v1", schema: open_api_3_schema)
302
- params = {
303
- "string_post_1" => 1
304
- }
283
+ params = { "string_post_1" => 1 }
305
284
  header "Content-Type", "application/json"
306
285
  post "/characters", JSON.generate(params)
307
286
  assert_equal 200, last_response.status
@@ -309,9 +288,7 @@ describe Committee::Middleware::RequestValidation do
309
288
 
310
289
  it "don't check prefix with no option" do
311
290
  @app = new_rack_app(schema: open_api_3_schema)
312
- params = {
313
- "string_post_1" => 1
314
- }
291
+ params = { "string_post_1" => 1 }
315
292
  header "Content-Type", "application/json"
316
293
  post "/v1/characters", JSON.generate(params)
317
294
  assert_equal 200, last_response.status
@@ -361,14 +338,14 @@ describe Committee::Middleware::RequestValidation do
361
338
  it "optionally coerces query params" do
362
339
  @app = new_rack_app(coerce_query_params: true, schema: open_api_3_schema)
363
340
  header "Content-Type", "application/json"
364
- get "/string_params_coercer", {"integer_1" => "1"}
341
+ get "/string_params_coercer", { "integer_1" => "1" }
365
342
  assert_equal 200, last_response.status
366
343
  end
367
344
 
368
345
  it "still raises an error if query param coercion is not possible" do
369
346
  @app = new_rack_app(coerce_query_params: false, schema: open_api_3_schema)
370
347
  header "Content-Type", "application/json"
371
- get "/string_params_coercer", {"integer_1" => "1"}
348
+ get "/string_params_coercer", { "integer_1" => "1" }
372
349
 
373
350
  assert_equal 400, last_response.status
374
351
  assert_match(/expected integer, but received String:/i, last_response.body)
@@ -376,7 +353,7 @@ describe Committee::Middleware::RequestValidation do
376
353
 
377
354
  it "passes through a valid request for OpenAPI3" do
378
355
  check_parameter = lambda { |env|
379
- assert_equal 3, env['committee.query_hash']['limit'] #5.0.x-
356
+ assert_equal 3, env['committee.query_hash']['limit'] # 5.0.x-
380
357
  [200, {}, []]
381
358
  }
382
359
 
@@ -417,7 +394,7 @@ describe Committee::Middleware::RequestValidation do
417
394
  assert_equal env['committee.params']['integer'], 42
418
395
  assert_equal env['committee.params'][:integer], 42
419
396
  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
397
+ # assert_equal env['rack.request.query_hash'][:integer], 42 # this isn't hash indifferent hash because we use rack.request.query_hash
421
398
  [204, {}, []]
422
399
  end, schema: open_api_3_schema, parameter_overwrite_by_rails_rule: false)
423
400
 
@@ -436,7 +413,7 @@ describe Committee::Middleware::RequestValidation do
436
413
  [204, {}, []]
437
414
  end, schema: open_api_3_schema, parameter_overwrite_by_rails_rule: false)
438
415
 
439
- params = {integer: 21}
416
+ params = { integer: 21 }
440
417
 
441
418
  header "Content-Type", "application/json"
442
419
  post '/overwrite_same_parameter?integer=42', JSON.generate(params)
@@ -452,11 +429,11 @@ describe Committee::Middleware::RequestValidation do
452
429
  assert_equal env['committee.request_body_hash']['integer'], 21
453
430
  assert_equal env['committee.request_body_hash'][:integer], 21
454
431
  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
432
+ # assert_equal env['rack.request.query_hash'][:integer], 21 # this isn't hash indifferent hash because we use rack.request.query_hash
456
433
  [204, {}, []]
457
434
  end, schema: open_api_3_schema, parameter_overwrite_by_rails_rule: false)
458
435
 
459
- params = {integer: 21}
436
+ params = { integer: 21 }
460
437
 
461
438
  header "Content-Type", "application/json"
462
439
  post '/overwrite_same_parameter/84?integer=42', JSON.generate(params)
@@ -475,7 +452,7 @@ describe Committee::Middleware::RequestValidation do
475
452
  [204, {}, []]
476
453
  end, schema: open_api_3_schema)
477
454
 
478
- params = {integer: 21}
455
+ params = { integer: 21 }
479
456
 
480
457
  header "Content-Type", "application/json"
481
458
  post '/overwrite_same_parameter', JSON.generate(params)
@@ -492,7 +469,7 @@ describe Committee::Middleware::RequestValidation do
492
469
  [204, {}, []]
493
470
  end, schema: open_api_3_schema)
494
471
 
495
- params = {integer: 21}
472
+ params = { integer: 21 }
496
473
 
497
474
  header "Content-Type", "application/json"
498
475
  post '/overwrite_same_parameter?integer=42', JSON.generate(params)
@@ -511,7 +488,7 @@ describe Committee::Middleware::RequestValidation do
511
488
  [204, {}, []]
512
489
  end, schema: open_api_3_schema)
513
490
 
514
- params = {integer: 21}
491
+ params = { integer: 21 }
515
492
 
516
493
  header "Content-Type", "application/json"
517
494
  post '/overwrite_same_parameter/84?integer=42', JSON.generate(params)
@@ -541,7 +518,7 @@ describe Committee::Middleware::RequestValidation do
541
518
  }
542
519
 
543
520
  assert_equal 'Committee OpenAPI3 not support trace method', e.message
544
- end
521
+ end
545
522
 
546
523
  describe 'check header' do
547
524
  [
@@ -580,8 +557,8 @@ describe Committee::Middleware::RequestValidation do
580
557
  describe ':accept_request_filter' do
581
558
  [
582
559
  { description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
583
- { description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 400 } },
584
- { description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
560
+ { description: 'when predicate matches, performs validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/c') }, expected: { status: 400 } },
561
+ { description: 'when predicate does not match, skips validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
585
562
  ].each do |h|
586
563
  description = h[:description]
587
564
  accept_request_filter = h[:accept_request_filter]
@@ -55,9 +55,7 @@ describe Committee::Middleware::RequestValidation do
55
55
 
56
56
  it "passes through a valid request" do
57
57
  @app = new_rack_app(schema: hyper_schema)
58
- params = {
59
- "name" => "cloudnasium"
60
- }
58
+ params = { "name" => "cloudnasium" }
61
59
  header "Content-Type", "application/json"
62
60
  post "/apps", JSON.generate(params)
63
61
  assert_equal 200, last_response.status
@@ -67,9 +65,7 @@ describe Committee::Middleware::RequestValidation do
67
65
  called_error = false
68
66
  pr = ->(_e) { called_error = true }
69
67
  @app = new_rack_app(schema: hyper_schema, error_handler: pr)
70
- params = {
71
- "name" => "cloudnasium"
72
- }
68
+ params = { "name" => "cloudnasium" }
73
69
  header "Content-Type", "application/json"
74
70
  post "/apps", JSON.generate(params)
75
71
  assert !called_error
@@ -79,9 +75,7 @@ describe Committee::Middleware::RequestValidation do
79
75
  called_error = false
80
76
  pr = ->(_e, _env) { called_error = true }
81
77
  @app = new_rack_app(schema: hyper_schema, error_handler: pr)
82
- params = {
83
- "name" => "cloudnasium"
84
- }
78
+ params = { "name" => "cloudnasium" }
85
79
  header "Content-Type", "application/json"
86
80
  post "/apps", JSON.generate(params)
87
81
  assert !called_error
@@ -89,10 +83,7 @@ describe Committee::Middleware::RequestValidation do
89
83
 
90
84
  it "passes given a datetime and with coerce_date_times enabled on GET endpoint" do
91
85
  key_name = "update_time"
92
- params = {
93
- key_name => "2016-04-01T16:00:00.000+09:00",
94
- "query" => "cloudnasium"
95
- }
86
+ params = { key_name => "2016-04-01T16:00:00.000+09:00", "query" => "cloudnasium" }
96
87
 
97
88
  check_parameter = lambda { |env|
98
89
  assert_equal DateTime, env['rack.request.query_hash'][key_name].class
@@ -165,10 +156,7 @@ describe Committee::Middleware::RequestValidation do
165
156
 
166
157
  it "passes given a nested datetime and with coerce_recursive=true and coerce_date_times=true on POST endpoint" do
167
158
  key_name = "update_time"
168
- params = {
169
- key_name => "2016-04-01T16:00:00.000+09:00",
170
- "array_property" => ARRAY_PROPERTY
171
- }
159
+ params = { key_name => "2016-04-01T16:00:00.000+09:00", "array_property" => ARRAY_PROPERTY }
172
160
 
173
161
  check_parameter = lambda { |env|
174
162
  hash = env['committee.params']
@@ -191,10 +179,7 @@ describe Committee::Middleware::RequestValidation do
191
179
 
192
180
  it "passes given a nested datetime and with coerce_recursive=true and coerce_date_times=true on POST endpoint" do
193
181
  key_name = "update_time"
194
- params = {
195
- key_name => "2016-04-01T16:00:00.000+09:00",
196
- "array_property" => ARRAY_PROPERTY
197
- }
182
+ params = { key_name => "2016-04-01T16:00:00.000+09:00", "array_property" => ARRAY_PROPERTY }
198
183
 
199
184
  check_parameter = lambda { |env|
200
185
  hash = env['committee.params']
@@ -217,10 +202,7 @@ describe Committee::Middleware::RequestValidation do
217
202
 
218
203
  it "passes given a nested datetime and with coerce_recursive=false and coerce_date_times=true on POST endpoint" do
219
204
  key_name = "update_time"
220
- params = {
221
- key_name => "2016-04-01T16:00:00.000+09:00",
222
- "array_property" => ARRAY_PROPERTY
223
- }
205
+ params = { key_name => "2016-04-01T16:00:00.000+09:00", "array_property" => ARRAY_PROPERTY }
224
206
 
225
207
  check_parameter = lambda { |env|
226
208
  hash = env['committee.params']
@@ -243,10 +225,7 @@ describe Committee::Middleware::RequestValidation do
243
225
 
244
226
  it "passes given a nested datetime and with coerce_recursive=false and coerce_date_times=false on POST endpoint" do
245
227
  key_name = "update_time"
246
- params = {
247
- key_name => "2016-04-01T16:00:00.000+09:00",
248
- "array_property" => ARRAY_PROPERTY
249
- }
228
+ params = { key_name => "2016-04-01T16:00:00.000+09:00", "array_property" => ARRAY_PROPERTY }
250
229
 
251
230
  check_parameter = lambda { |env|
252
231
  hash = env['committee.params']
@@ -269,9 +248,7 @@ describe Committee::Middleware::RequestValidation do
269
248
 
270
249
  it "passes given an invalid datetime string with coerce_date_times enabled" do
271
250
  @app = new_rack_app(coerce_date_times: true, schema: hyper_schema)
272
- params = {
273
- "update_time" => "invalid_datetime_format"
274
- }
251
+ params = { "update_time" => "invalid_datetime_format" }
275
252
  header "Content-Type", "application/json"
276
253
  post "/apps", JSON.generate(params)
277
254
  assert_equal 400, last_response.status
@@ -288,9 +265,7 @@ describe Committee::Middleware::RequestValidation do
288
265
  it "detects an invalid request" do
289
266
  @app = new_rack_app(schema: hyper_schema)
290
267
  header "Content-Type", "application/json"
291
- params = {
292
- "name" => 1
293
- }
268
+ params = { "name" => 1 }
294
269
  post "/apps", JSON.generate(params)
295
270
  assert_equal 400, last_response.status
296
271
  assert_match(/invalid request/i, last_response.body)
@@ -299,9 +274,7 @@ describe Committee::Middleware::RequestValidation do
299
274
  it "ignores errors when ignore_error: true" do
300
275
  @app = new_rack_app(schema: hyper_schema, ignore_error: true)
301
276
  header "Content-Type", "application/json"
302
- params = {
303
- "name" => 1
304
- }
277
+ params = { "name" => 1 }
305
278
  post "/apps", JSON.generate(params)
306
279
  assert_equal 200, last_response.status
307
280
  end
@@ -311,9 +284,7 @@ describe Committee::Middleware::RequestValidation do
311
284
  pr = ->(e, _env) { called_err = e }
312
285
  @app = new_rack_app(schema: hyper_schema, error_handler: pr)
313
286
  header "Content-Type", "application/json"
314
- params = {
315
- "name" => 1
316
- }
287
+ params = { "name" => 1 }
317
288
  post "/apps", JSON.generate(params)
318
289
  assert_kind_of Committee::InvalidRequest, called_err
319
290
  end
@@ -337,9 +308,7 @@ describe Committee::Middleware::RequestValidation do
337
308
 
338
309
  it "takes a prefix" do
339
310
  @app = new_rack_app(prefix: "/v1", schema: hyper_schema)
340
- params = {
341
- "name" => "cloudnasium"
342
- }
311
+ params = { "name" => "cloudnasium" }
343
312
  header "Content-Type", "application/json"
344
313
  post "/v1/apps", JSON.generate(params)
345
314
  assert_equal 200, last_response.status
@@ -374,9 +343,7 @@ describe Committee::Middleware::RequestValidation do
374
343
 
375
344
  it "optionally skip content_type check" do
376
345
  @app = new_rack_app(check_content_type: false, schema: hyper_schema)
377
- params = {
378
- "name" => "cloudnasium"
379
- }
346
+ params = { "name" => "cloudnasium" }
380
347
  header "Content-Type", "text/html"
381
348
  post "/apps", JSON.generate(params)
382
349
  assert_equal 200, last_response.status
@@ -385,14 +352,14 @@ describe Committee::Middleware::RequestValidation do
385
352
  it "optionally coerces query params" do
386
353
  @app = new_rack_app(coerce_query_params: true, schema: hyper_schema)
387
354
  header "Content-Type", "application/json"
388
- get "/search/apps", {"per_page" => "10", "query" => "cloudnasium"}
355
+ get "/search/apps", { "per_page" => "10", "query" => "cloudnasium" }
389
356
  assert_equal 200, last_response.status
390
357
  end
391
358
 
392
359
  it "still raises an error if query param coercion is not possible" do
393
360
  @app = new_rack_app(coerce_query_params: true, schema: hyper_schema)
394
361
  header "Content-Type", "application/json"
395
- get "/search/apps", {"per_page" => "foo", "query" => "cloudnasium"}
362
+ get "/search/apps", { "per_page" => "foo", "query" => "cloudnasium" }
396
363
  assert_equal 400, last_response.status
397
364
  assert_match(/invalid request/i, last_response.body)
398
365
  end
@@ -470,7 +437,7 @@ describe Committee::Middleware::RequestValidation do
470
437
  coerce_recursive: true
471
438
  }
472
439
 
473
- @app = new_rack_app({schema: hyper_schema}.merge(options))
440
+ @app = new_rack_app({ schema: hyper_schema }.merge(options))
474
441
  header "Content-Type", "application/x-www-form-urlencoded"
475
442
  post "/unknown"
476
443
  assert_equal 200, last_response.status
@@ -479,8 +446,8 @@ describe Committee::Middleware::RequestValidation do
479
446
  describe ':accept_request_filter' do
480
447
  [
481
448
  { description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
482
- { description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 400 } },
483
- { description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
449
+ { description: 'when predicate matches, performs validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/a') }, expected: { status: 400 } },
450
+ { description: 'when predicate does not match, skips validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
484
451
  ].each do |h|
485
452
  description = h[:description]
486
453
  accept_request_filter = h[:accept_request_filter]
@@ -503,7 +470,6 @@ describe Committee::Middleware::RequestValidation do
503
470
  }, options)
504
471
  end
505
472
 
506
-
507
473
  def new_rack_app_with_lambda(check_lambda, options = {})
508
474
  Rack::Builder.new {
509
475
  use Committee::Middleware::RequestValidation, options
@@ -5,7 +5,7 @@ require "test_helper"
5
5
  describe Committee::Middleware::ResponseValidation do
6
6
  include Rack::Test::Methods
7
7
 
8
- CHARACTERS_RESPONSE = {"Otonokizaka" => ["Honoka.Kousaka"]}
8
+ CHARACTERS_RESPONSE = { "Otonokizaka" => ["Honoka.Kousaka"] }
9
9
 
10
10
  def app
11
11
  @app
@@ -17,6 +17,22 @@ describe Committee::Middleware::ResponseValidation do
17
17
  assert_equal 200, last_response.status
18
18
  end
19
19
 
20
+ it "passes through a valid response with content-type (lower-case)" do
21
+ status = 200
22
+ headers = { "content-type" => "application/json" }
23
+ response = JSON.generate(CHARACTERS_RESPONSE)
24
+
25
+ @app = Rack::Builder.new {
26
+ use Committee::Middleware::ResponseValidation, { schema: open_api_3_schema }
27
+ run lambda { |_|
28
+ [status, headers, [response]]
29
+ }
30
+ }
31
+
32
+ get "/characters"
33
+ assert_equal 200, last_response.status
34
+ end
35
+
20
36
  it "passes through a invalid json" do
21
37
  @app = new_response_rack("not_json", {}, schema: open_api_3_schema)
22
38
 
@@ -35,7 +51,7 @@ describe Committee::Middleware::ResponseValidation do
35
51
  end
36
52
 
37
53
  it "passes through a invalid json with parse_response_by_content_type option" do
38
- @app = new_response_rack("csv response", { "Content-Type" => "test/csv"}, schema: open_api_3_schema, parse_response_by_content_type: true)
54
+ @app = new_response_rack("csv response", { "Content-Type" => "test/csv" }, schema: open_api_3_schema, parse_response_by_content_type: true)
39
55
 
40
56
  get "/csv"
41
57
 
@@ -59,13 +75,13 @@ describe Committee::Middleware::ResponseValidation do
59
75
  end
60
76
 
61
77
  it "passes through a 204 (no content) response" do
62
- @app = new_response_rack("", {}, {schema: open_api_3_schema}, {status: 204})
78
+ @app = new_response_rack("", {}, { schema: open_api_3_schema }, { status: 204 })
63
79
  post "/validate"
64
80
  assert_equal 204, last_response.status
65
81
  end
66
82
 
67
83
  it "passes through a 304 (not modified) response" do
68
- @app = new_response_rack("", {}, {schema: open_api_3_schema}, {status: 304})
84
+ @app = new_response_rack("", {}, { schema: open_api_3_schema }, { status: 304 })
69
85
  post "/validate"
70
86
  assert_equal 304, last_response.status
71
87
  end
@@ -93,10 +109,10 @@ describe Committee::Middleware::ResponseValidation do
93
109
  end
94
110
 
95
111
  it "not parameter request" do
96
- @app = new_response_rack({integer: '1'}.to_json, {}, schema: open_api_3_schema, raise: true)
112
+ @app = new_response_rack({ integer: '1' }.to_json, {}, schema: open_api_3_schema, raise: true)
97
113
 
98
114
  assert_raises(Committee::InvalidResponse) do
99
- patch "/validate_no_parameter", {no_schema: 'no'}
115
+ patch "/validate_no_parameter", { no_schema: 'no' }
100
116
  end
101
117
  end
102
118
 
@@ -178,7 +194,6 @@ describe Committee::Middleware::ResponseValidation do
178
194
  raise: true,
179
195
  validate_success_only: false)
180
196
 
181
-
182
197
  e = assert_raises(Committee::InvalidResponse) do
183
198
  get "/characters"
184
199
  end
@@ -194,7 +209,6 @@ describe Committee::Middleware::ResponseValidation do
194
209
  raise: true,
195
210
  validate_success_only: true)
196
211
 
197
-
198
212
  get "/characters"
199
213
 
200
214
  assert_equal 400, last_response.status
@@ -204,8 +218,8 @@ describe Committee::Middleware::ResponseValidation do
204
218
  describe ':accept_request_filter' do
205
219
  [
206
220
  { description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 500 } },
207
- { description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 500 } },
208
- { description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
221
+ { description: 'when predicate matches, performs validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/c') }, expected: { status: 500 } },
222
+ { description: 'when predicate does not match, skips validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
209
223
  ].each do |h|
210
224
  description = h[:description]
211
225
  accept_request_filter = h[:accept_request_filter]
@@ -223,9 +237,9 @@ describe Committee::Middleware::ResponseValidation do
223
237
 
224
238
  it 'does not suppress application error' do
225
239
  @app = Rack::Builder.new {
226
- use Committee::Middleware::ResponseValidation, {schema: open_api_3_schema, raise: true}
240
+ use Committee::Middleware::ResponseValidation, { schema: open_api_3_schema, raise: true }
227
241
  run lambda { |_|
228
- JSON.load('-') # invalid json
242
+ JSON.load('-') # invalid json
229
243
  }
230
244
  }
231
245
 
@@ -234,14 +248,14 @@ describe Committee::Middleware::ResponseValidation do
234
248
  end
235
249
  end
236
250
 
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})
251
+ it "strict and invalid status" do
252
+ @app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, { schema: open_api_3_schema, strict: true }, { status: 201 })
239
253
  get "/characters"
240
254
  assert_equal 500, last_response.status
241
255
  end
242
256
 
243
257
  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})
258
+ @app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, { schema: open_api_3_schema, strict: true, raise: true }, { status: 201 })
245
259
 
246
260
  assert_raises(Committee::InvalidResponse) do
247
261
  get "/characters"
@@ -249,11 +263,7 @@ describe Committee::Middleware::ResponseValidation do
249
263
  end
250
264
 
251
265
  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
- )
266
+ @app = new_response_rack("abc", {}, { schema: open_api_3_schema, strict: true }, { content_type: 'application/text' })
257
267
  get "/characters"
258
268
  assert_equal 500, last_response.status
259
269
  end
@@ -261,8 +271,8 @@ describe Committee::Middleware::ResponseValidation do
261
271
  it "strict and invalid content type with raise" do
262
272
  @app = new_response_rack("abc",
263
273
  {},
264
- {schema: open_api_3_schema, strict: true, raise: true},
265
- {content_type: 'application/text'}
274
+ { schema: open_api_3_schema, strict: true, raise: true },
275
+ { content_type: 'application/text' }
266
276
  )
267
277
 
268
278
  assert_raises(Committee::InvalidResponse) do
@@ -275,9 +285,7 @@ describe Committee::Middleware::ResponseValidation do
275
285
  def new_response_rack(response, headers = {}, options = {}, rack_options = {})
276
286
  status = rack_options[:status] || 200
277
287
  content_type = rack_options[:content_type] || "application/json"
278
- headers = {
279
- "Content-Type" => content_type
280
- }.merge(headers)
288
+ headers = { "Content-Type" => content_type }.merge(headers)
281
289
  Rack::Builder.new {
282
290
  use Committee::Middleware::ResponseValidation, options
283
291
  run lambda { |_|