committee 5.6.3 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/committee/drivers/open_api_2/driver.rb +16 -0
- data/lib/committee/drivers/open_api_2/link.rb +2 -0
- data/lib/committee/drivers/open_api_3/schema.rb +6 -1
- data/lib/committee/drivers.rb +15 -8
- data/lib/committee/middleware/options/response_validation.rb +4 -2
- data/lib/committee/middleware/response_validation.rb +37 -10
- data/lib/committee/middleware/stub.rb +1 -1
- data/lib/committee/request_unpacker.rb +19 -9
- data/lib/committee/schema_locations.rb +96 -0
- data/lib/committee/schema_validator/hyper_schema/request_validator.rb +1 -1
- data/lib/committee/schema_validator/hyper_schema/response_validator.rb +14 -4
- data/lib/committee/schema_validator/hyper_schema.rb +5 -4
- data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +76 -40
- data/lib/committee/schema_validator/open_api_3/parameter_deserializer.rb +66 -20
- data/lib/committee/schema_validator/open_api_3/request_validator.rb +2 -3
- data/lib/committee/schema_validator/open_api_3/response_validator.rb +2 -1
- data/lib/committee/schema_validator/open_api_3/router.rb +1 -3
- data/lib/committee/schema_validator/open_api_3.rb +26 -18
- data/lib/committee/schema_validator/option.rb +2 -1
- data/lib/committee/schema_validator.rb +1 -5
- data/lib/committee/test/except_parameter.rb +4 -4
- data/lib/committee/test/methods.rb +14 -1
- data/lib/committee/test/schema_coverage.rb +11 -2
- data/lib/committee/utils.rb +6 -0
- data/lib/committee/validation_error.rb +1 -1
- data/lib/committee/version.rb +1 -1
- data/lib/committee.rb +1 -0
- data/test/drivers/open_api_2/driver_test.rb +13 -0
- data/test/drivers_test.rb +39 -8
- data/test/middleware/options/response_validation_test.rb +15 -0
- data/test/middleware/request_validation_open_api_3_test.rb +670 -8
- data/test/middleware/request_validation_test.rb +50 -0
- data/test/middleware/response_validation_open_api_3_test.rb +162 -0
- data/test/middleware/response_validation_test.rb +134 -0
- data/test/middleware/stub_test.rb +10 -0
- data/test/request_unpacker_test.rb +34 -0
- data/test/schema_location_test.rb +111 -0
- data/test/schema_validator/hyper_schema/response_validator_test.rb +16 -0
- data/test/schema_validator/open_api_3/operation_wrapper_test.rb +95 -0
- data/test/schema_validator/open_api_3/parameter_deserializer_test.rb +145 -8
- data/test/schema_validator/open_api_3_test.rb +27 -0
- data/test/schema_validator_test.rb +10 -0
- data/test/test/methods_test.rb +200 -0
- data/test/test/schema_coverage_test.rb +34 -0
- data/test/test_helper.rb +23 -0
- data/test/utils_test.rb +28 -0
- data/test/validation_error_test.rb +1 -1
- metadata +22 -4
|
@@ -19,6 +19,37 @@ describe Committee::Middleware::RequestValidation do
|
|
|
19
19
|
assert_equal 200, last_response.status
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
it "accepts a JSON request through Rack::Lint" do
|
|
23
|
+
app = Rack::Lint.new(new_rack_app(schema: hyper_schema))
|
|
24
|
+
env = Rack::MockRequest.env_for("/apps", method: "POST", input: '{"name":"cloudnasium"}', "CONTENT_TYPE" => "application/json")
|
|
25
|
+
|
|
26
|
+
status, _headers, body = app.call(env)
|
|
27
|
+
|
|
28
|
+
assert_equal 200, status
|
|
29
|
+
body.close if body.respond_to?(:close)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "returns a validation error through Rack::Lint" do
|
|
33
|
+
app = Rack::Lint.new(new_rack_app(schema: hyper_schema))
|
|
34
|
+
env = Rack::MockRequest.env_for("/apps", method: "POST", input: "{", "CONTENT_TYPE" => "application/json")
|
|
35
|
+
|
|
36
|
+
status, headers, body = app.call(env)
|
|
37
|
+
|
|
38
|
+
assert_equal 400, status
|
|
39
|
+
assert_equal "application/json", headers["content-type"]
|
|
40
|
+
body.close if body.respond_to?(:close)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "accepts an empty optional body through Rack::Lint" do
|
|
44
|
+
app = Rack::Lint.new(new_rack_app(schema: hyper_schema))
|
|
45
|
+
env = Rack::MockRequest.env_for("/apps", method: "POST", input: "", "CONTENT_TYPE" => "text/plain")
|
|
46
|
+
|
|
47
|
+
status, _headers, body = app.call(env)
|
|
48
|
+
|
|
49
|
+
assert_equal 200, status
|
|
50
|
+
body.close if body.respond_to?(:close)
|
|
51
|
+
end
|
|
52
|
+
|
|
22
53
|
it "doesn't call error_handler (has a arg) when request is valid" do
|
|
23
54
|
called_error = false
|
|
24
55
|
pr = ->(_e) { called_error = true }
|
|
@@ -256,6 +287,25 @@ describe Committee::Middleware::RequestValidation do
|
|
|
256
287
|
assert_equal 200, last_response.status
|
|
257
288
|
end
|
|
258
289
|
|
|
290
|
+
[
|
|
291
|
+
[:hyper_schema, "apps", "name"],
|
|
292
|
+
[:open_api_2_schema, "api/pets", "name"],
|
|
293
|
+
[:open_api_3_schema, "characters", "string_post_1"],
|
|
294
|
+
].each do |schema_method, path, property|
|
|
295
|
+
["/v1/", "/"].product([false, true]).each do |prefix, strict|
|
|
296
|
+
it "validates #{schema_method} requests with prefix #{prefix.inspect} and strict #{strict}" do
|
|
297
|
+
@app = new_rack_app(prefix: prefix, schema: send(schema_method), strict: strict)
|
|
298
|
+
header "Content-Type", "application/json"
|
|
299
|
+
|
|
300
|
+
post "#{prefix}#{path}", JSON.generate({ property => "cloudnasium" })
|
|
301
|
+
assert_equal 200, last_response.status
|
|
302
|
+
|
|
303
|
+
post "#{prefix}#{path}", JSON.generate({ property => 1 })
|
|
304
|
+
assert_equal 400, last_response.status
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
259
309
|
it "ignores paths outside the prefix" do
|
|
260
310
|
@app = new_rack_app(prefix: "/v1", schema: hyper_schema)
|
|
261
311
|
header "Content-Type", "text/html"
|
|
@@ -17,6 +17,60 @@ describe Committee::Middleware::ResponseValidation do
|
|
|
17
17
|
assert_equal 200, last_response.status
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
it "passes through mixed encoding chunks" do
|
|
21
|
+
chunks = ['{"Otonokizaka":["あ'.freeze, "い".b.freeze, 'う"]}'.freeze]
|
|
22
|
+
@app = Rack::Builder.new {
|
|
23
|
+
use Committee::Middleware::ResponseValidation, schema: open_api_3_schema, raise: true
|
|
24
|
+
run ->(_) { [200, { "content-type" => "application/json" }, chunks] }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
status, _headers, response_body = @app.call(Rack::MockRequest.env_for("/characters"))
|
|
28
|
+
|
|
29
|
+
assert_equal 200, status
|
|
30
|
+
response_body.each_with_index { |chunk, index| assert_same chunks[index], chunk }
|
|
31
|
+
assert_equal [Encoding::UTF_8, Encoding::BINARY, Encoding::UTF_8], chunks.map(&:encoding)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "passes through a Rack 3 call-only response body" do
|
|
35
|
+
content = JSON.generate(CHARACTERS_RESPONSE)
|
|
36
|
+
body = Object.new
|
|
37
|
+
body.define_singleton_method(:call) { |stream| stream.write(content) }
|
|
38
|
+
@app = Rack::Builder.new {
|
|
39
|
+
use Committee::Middleware::ResponseValidation, { schema: open_api_3_schema }
|
|
40
|
+
run ->(_) { [200, { "content-type" => "application/json" }, body] }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
status, _headers, response_body = @app.call(Rack::MockRequest.env_for("/characters"))
|
|
44
|
+
output = StringIO.new
|
|
45
|
+
response_body.call(output)
|
|
46
|
+
|
|
47
|
+
assert_equal 200, status
|
|
48
|
+
assert_equal content, output.string
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "preserves a one-shot response body after validation" do
|
|
52
|
+
content = JSON.generate(CHARACTERS_RESPONSE)
|
|
53
|
+
chunks = [content]
|
|
54
|
+
each_calls = 0
|
|
55
|
+
body = Rack::BodyProxy.new(Enumerator.new do |yielder|
|
|
56
|
+
each_calls += 1
|
|
57
|
+
chunks.each { |chunk| yielder << chunk }
|
|
58
|
+
chunks.clear
|
|
59
|
+
end) {}
|
|
60
|
+
@app = Rack::Builder.new {
|
|
61
|
+
use Committee::Middleware::ResponseValidation, { schema: open_api_3_schema }
|
|
62
|
+
run ->(_) { [200, { "Content-Type" => "application/json" }, body] }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
status, _headers, response_body = @app.call(Rack::MockRequest.env_for("/characters"))
|
|
66
|
+
|
|
67
|
+
assert_equal 200, status
|
|
68
|
+
assert_equal content, response_body.each.to_a.join
|
|
69
|
+
assert_equal 1, each_calls
|
|
70
|
+
response_body.close if response_body.respond_to?(:close)
|
|
71
|
+
assert body.closed?
|
|
72
|
+
end
|
|
73
|
+
|
|
20
74
|
it "passes through a valid response with content-type (lower-case)" do
|
|
21
75
|
status = 200
|
|
22
76
|
headers = { "content-type" => "application/json" }
|
|
@@ -303,6 +357,114 @@ describe Committee::Middleware::ResponseValidation do
|
|
|
303
357
|
end
|
|
304
358
|
end
|
|
305
359
|
|
|
360
|
+
describe "strict_response_content_type option" do
|
|
361
|
+
it "passes through with undeclared content type when strict_response_content_type: false (default)" do
|
|
362
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema)
|
|
363
|
+
get "/characters"
|
|
364
|
+
assert_equal 200, last_response.status
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
it "raises with undeclared content type when strict_response_content_type: true" do
|
|
368
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema, strict_response_content_type: true, raise: true)
|
|
369
|
+
assert_raises(Committee::InvalidResponse) do
|
|
370
|
+
get "/characters"
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
it "returns 500 with undeclared content type when strict_response_content_type: true without raise" do
|
|
375
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema, strict_response_content_type: true)
|
|
376
|
+
get "/characters"
|
|
377
|
+
assert_equal 500, last_response.status
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
it "passes through when response has no content schema with strict_response_content_type: true" do
|
|
381
|
+
@app = new_response_rack("", { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema, strict_response_content_type: true)
|
|
382
|
+
get "/no_content_schema"
|
|
383
|
+
assert_equal 200, last_response.status
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
it "passes through with declared content type and strict_response_content_type: true" do
|
|
387
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), { "Content-Type" => "application/json" }, schema: open_api_3_schema, strict_response_content_type: true)
|
|
388
|
+
get "/characters"
|
|
389
|
+
assert_equal 200, last_response.status
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
it "passes through with declared content type and charset param with strict_response_content_type: true" do
|
|
393
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), { "Content-Type" => "application/json; charset=utf-8" }, schema: open_api_3_schema, strict_response_content_type: true)
|
|
394
|
+
get "/characters"
|
|
395
|
+
assert_equal 200, last_response.status
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
it "raises with undeclared content type matched via wildcard status code with strict_response_content_type: true" do
|
|
399
|
+
@app = new_response_rack("{}", { "Content-Type" => "application/vnd.api+json" }, { schema: open_api_3_schema, strict_response_content_type: true, raise: true, validate_success_only: false }, { status: 400 })
|
|
400
|
+
assert_raises(Committee::InvalidResponse) do
|
|
401
|
+
get "/wildcard_response"
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
it "raises with undeclared content type matched via default response with strict_response_content_type: true" do
|
|
406
|
+
@app = new_response_rack("{}", { "Content-Type" => "application/vnd.api+json" }, { schema: open_api_3_schema, strict_response_content_type: true, raise: true, validate_success_only: false }, { status: 500 })
|
|
407
|
+
assert_raises(Committee::InvalidResponse) do
|
|
408
|
+
get "/default_response"
|
|
409
|
+
end
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
it "calls error_handler with strict_response_content_type: true and raise: false" do
|
|
413
|
+
called_err = nil
|
|
414
|
+
pr = ->(e, _env) { called_err = e }
|
|
415
|
+
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema, strict_response_content_type: true, error_handler: pr)
|
|
416
|
+
get "/characters"
|
|
417
|
+
assert_equal 500, last_response.status
|
|
418
|
+
assert_kind_of Committee::InvalidResponse, called_err
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
describe "OpenAPI 3.1" do
|
|
423
|
+
it "accepts a valid response" do
|
|
424
|
+
@app = new_response_rack(JSON.generate(ValidItem), {}, schema: open_api_3_1_schema, raise: true)
|
|
425
|
+
get "/items/1"
|
|
426
|
+
assert_equal 200, last_response.status
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
it "rejects an invalid response" do
|
|
430
|
+
@app = new_response_rack(JSON.generate(ValidItem.merge("name" => 1)), {}, schema: open_api_3_1_schema, raise: true)
|
|
431
|
+
assert_raises(Committee::InvalidResponse) { get "/items/1" }
|
|
432
|
+
end
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
describe "OpenAPI 3.2" do
|
|
436
|
+
it "accepts a valid query response" do
|
|
437
|
+
@app = new_response_rack(JSON.generate([{ "name" => "a" }]), {}, schema: open_api_3_2_schema, raise: true)
|
|
438
|
+
custom_request("QUERY", "/items", {}, { input: JSON.generate("name" => "a"), "CONTENT_TYPE" => "application/json" })
|
|
439
|
+
assert_equal 200, last_response.status
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
it "rejects an invalid query response" do
|
|
443
|
+
@app = new_response_rack(JSON.generate([{ "name" => 1 }]), {}, schema: open_api_3_2_schema, raise: true)
|
|
444
|
+
assert_raises(Committee::InvalidResponse) do
|
|
445
|
+
custom_request("QUERY", "/items", {}, { input: JSON.generate("name" => "a"), "CONTENT_TYPE" => "application/json" })
|
|
446
|
+
end
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
it "accepts a valid response body referenced from components.mediaTypes" do
|
|
450
|
+
@app = new_response_rack(JSON.generate("name" => "a"), {}, schema: open_api_3_2_schema, raise: true)
|
|
451
|
+
post "/shared"
|
|
452
|
+
assert_equal 200, last_response.status
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
it "rejects an invalid response body referenced from components.mediaTypes" do
|
|
456
|
+
@app = new_response_rack(JSON.generate("name" => 1), {}, schema: open_api_3_2_schema, raise: true)
|
|
457
|
+
assert_raises(Committee::InvalidResponse) { post "/shared" }
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# openapi_parser doesn't validate itemSchema yet
|
|
461
|
+
it "skips a streaming response described by itemSchema" do
|
|
462
|
+
@app = new_response_rack("data: {}\n\n", {}, { schema: open_api_3_2_schema, raise: true }, { content_type: "text/event-stream" })
|
|
463
|
+
get "/events"
|
|
464
|
+
assert_equal 200, last_response.status
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
306
468
|
private
|
|
307
469
|
|
|
308
470
|
def new_response_rack(response, headers = {}, options = {}, rack_options = {})
|
|
@@ -15,6 +15,27 @@ describe Committee::Middleware::ResponseValidation do
|
|
|
15
15
|
assert_equal 200, last_response.status
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
[
|
|
19
|
+
[:hyper_schema, "/apps", ValidApp],
|
|
20
|
+
[:open_api_2_schema, "/api/pets", ValidPet],
|
|
21
|
+
].each do |schema_method, path, valid_object|
|
|
22
|
+
it "passes through mixed encoding chunks for #{schema_method}" do
|
|
23
|
+
prefix, suffix = JSON.generate([valid_object.merge("message" => "あいう")]).split("い")
|
|
24
|
+
chunks = [prefix.freeze, "い".b.freeze, suffix.freeze]
|
|
25
|
+
schema = send(schema_method)
|
|
26
|
+
@app = Rack::Builder.new {
|
|
27
|
+
use Committee::Middleware::ResponseValidation, schema: schema, raise: true
|
|
28
|
+
run ->(_) { [200, { "content-type" => "application/json" }, chunks] }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
status, _headers, response_body = @app.call(Rack::MockRequest.env_for(path))
|
|
32
|
+
|
|
33
|
+
assert_equal 200, status
|
|
34
|
+
response_body.each_with_index { |chunk, index| assert_same chunks[index], chunk }
|
|
35
|
+
assert_equal [Encoding::UTF_8, Encoding::BINARY, Encoding::UTF_8], chunks.map(&:encoding)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
18
39
|
it "passes through a valid response with content-type (lower-case)" do
|
|
19
40
|
options = { schema: hyper_schema }
|
|
20
41
|
status = 200
|
|
@@ -127,6 +148,23 @@ describe Committee::Middleware::ResponseValidation do
|
|
|
127
148
|
assert_equal 200, last_response.status
|
|
128
149
|
end
|
|
129
150
|
|
|
151
|
+
[
|
|
152
|
+
[:hyper_schema, "apps"],
|
|
153
|
+
[:open_api_2_schema, "api/pets"],
|
|
154
|
+
[:open_api_3_schema, "characters"],
|
|
155
|
+
].each do |schema_method, path|
|
|
156
|
+
["/v1/", "/"].each do |prefix|
|
|
157
|
+
it "validates #{schema_method} responses with prefix #{prefix.inspect}" do
|
|
158
|
+
@app = new_rack_app("not_json", {}, prefix: prefix, schema: send(schema_method))
|
|
159
|
+
|
|
160
|
+
get "#{prefix}#{path}"
|
|
161
|
+
|
|
162
|
+
assert_equal 500, last_response.status
|
|
163
|
+
assert_match(/valid JSON/i, last_response.body)
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
130
168
|
it "rescues JSON errors" do
|
|
131
169
|
@app = new_rack_app("[{x:y}]", {}, raise: true, schema: hyper_schema)
|
|
132
170
|
assert_raises(Committee::InvalidResponse) do
|
|
@@ -212,6 +250,102 @@ describe Committee::Middleware::ResponseValidation do
|
|
|
212
250
|
get "/events/stream"
|
|
213
251
|
assert_equal 200, last_response.status
|
|
214
252
|
end
|
|
253
|
+
|
|
254
|
+
it 'streams a response with content-type parameters without eagerly reading its body' do
|
|
255
|
+
each_calls = 0
|
|
256
|
+
body = Enumerator.new do |yielder|
|
|
257
|
+
each_calls += 1
|
|
258
|
+
yielder << "data: hello\n\n"
|
|
259
|
+
end
|
|
260
|
+
validated_body = nil
|
|
261
|
+
options = { schema: open_api_3_streaming_response_schema, streaming_content_parsers: { 'text/event-stream' => ->(body) { validated_body = body } }, raise: true }
|
|
262
|
+
headers = { 'content-type' => 'text/event-stream; charset=utf-8' }
|
|
263
|
+
@app = Rack::Builder.new {
|
|
264
|
+
use Committee::Middleware::ResponseValidation, options
|
|
265
|
+
run ->(_) { [200, headers, body] }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
status, response_headers, response_body = @app.call(Rack::MockRequest.env_for("/events/stream"))
|
|
269
|
+
|
|
270
|
+
assert_equal 200, status
|
|
271
|
+
assert_equal 0, each_calls
|
|
272
|
+
assert_equal headers, response_headers
|
|
273
|
+
assert_nil validated_body
|
|
274
|
+
assert_equal ["data: hello\n\n"], response_body.each.to_a
|
|
275
|
+
assert_equal 1, each_calls
|
|
276
|
+
assert_nil validated_body
|
|
277
|
+
response_body.close
|
|
278
|
+
assert_equal "data: hello\n\n", validated_body
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
it 'prefers an exact streaming parser over the normalized media type' do
|
|
282
|
+
validated_body = nil
|
|
283
|
+
content_type = 'text/event-stream; charset=utf-8'
|
|
284
|
+
parsers = { 'text/event-stream' => ->(_) { flunk "the exact parser should take precedence" }, content_type => ->(body) { validated_body = body } }
|
|
285
|
+
options = { schema: open_api_3_streaming_response_schema, streaming_content_parsers: parsers, raise: true }
|
|
286
|
+
@app = Rack::Builder.new {
|
|
287
|
+
use Committee::Middleware::ResponseValidation, options
|
|
288
|
+
run ->(_) { [200, { 'Content-Type' => content_type }, ["hello"]] }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
_status, _headers, response_body = @app.call(Rack::MockRequest.env_for("/events/stream"))
|
|
292
|
+
assert_equal ["hello"], response_body.each.to_a
|
|
293
|
+
response_body.close
|
|
294
|
+
assert_equal "hello", validated_body
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
[
|
|
298
|
+
["non-ASCII characters", ["data: あ", "い".b, "う\n\n"]],
|
|
299
|
+
["a split UTF-8 character", ["data: あ\xE3", "\x81".b, "\x84う\n\n"]],
|
|
300
|
+
].each do |description, chunks|
|
|
301
|
+
it "delivers mixed encoding chunks with #{description} unchanged before validation" do
|
|
302
|
+
chunks.each(&:freeze)
|
|
303
|
+
validated_body = nil
|
|
304
|
+
parser = ->(body) { validated_body = body }
|
|
305
|
+
@app = Rack::Builder.new {
|
|
306
|
+
use Committee::Middleware::ResponseValidation, schema: open_api_3_streaming_response_schema, streaming_content_parsers: { 'text/event-stream' => parser }, raise: true
|
|
307
|
+
run ->(_) { [200, { 'content-type' => 'text/event-stream' }, chunks] }
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
status, _headers, response_body = @app.call(Rack::MockRequest.env_for("/events/stream"))
|
|
311
|
+
delivered_chunks = response_body.each.to_a
|
|
312
|
+
|
|
313
|
+
assert_equal 200, status
|
|
314
|
+
assert_equal chunks.length, delivered_chunks.length
|
|
315
|
+
delivered_chunks.each_with_index { |chunk, i| assert_same chunks[i], chunk }
|
|
316
|
+
assert_nil validated_body
|
|
317
|
+
response_body.close
|
|
318
|
+
assert_equal "data: あいう\n\n", validated_body
|
|
319
|
+
assert_equal Encoding::UTF_8, validated_body.encoding
|
|
320
|
+
assert validated_body.valid_encoding?
|
|
321
|
+
assert_equal [Encoding::UTF_8, Encoding::BINARY, Encoding::UTF_8], chunks.map(&:encoding)
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
it 'validates the chunks sent from a body that cannot be read after close' do
|
|
326
|
+
closed = false
|
|
327
|
+
body = Object.new
|
|
328
|
+
body.define_singleton_method(:each) do |&block|
|
|
329
|
+
raise IOError, "body is closed" if closed
|
|
330
|
+
|
|
331
|
+
["hello"].each(&block)
|
|
332
|
+
end
|
|
333
|
+
body.define_singleton_method(:close) { closed = true }
|
|
334
|
+
validated_body = nil
|
|
335
|
+
options = { schema: open_api_3_streaming_response_schema, streaming_content_parsers: { 'text/event-stream' => ->(body) { validated_body = body } }, raise: true, }
|
|
336
|
+
@app = Rack::Builder.new {
|
|
337
|
+
use Committee::Middleware::ResponseValidation, options
|
|
338
|
+
run ->(_) { [200, { 'content-type' => 'text/event-stream' }, body] }
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
status, _headers, response_body = @app.call(Rack::MockRequest.env_for("/events/stream"))
|
|
342
|
+
|
|
343
|
+
assert_equal 200, status
|
|
344
|
+
assert_equal ["hello"], response_body.each.to_a
|
|
345
|
+
response_body.close
|
|
346
|
+
assert closed
|
|
347
|
+
assert_equal "hello", validated_body
|
|
348
|
+
end
|
|
215
349
|
end
|
|
216
350
|
|
|
217
351
|
describe 'application/x-json-stream; customized streaming event' do
|
|
@@ -17,6 +17,16 @@ describe Committee::Middleware::Stub do
|
|
|
17
17
|
assert_equal ValidApp.keys.sort, data.keys.sort
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
it "returns Rack 3 compliant response headers" do
|
|
21
|
+
app = Rack::Lint.new(new_rack_app(schema: hyper_schema))
|
|
22
|
+
|
|
23
|
+
status, headers, body = app.call(Rack::MockRequest.env_for("/apps/heroku-api"))
|
|
24
|
+
|
|
25
|
+
assert_equal 200, status
|
|
26
|
+
assert_equal "application/json", headers["content-type"]
|
|
27
|
+
body.close if body.respond_to?(:close)
|
|
28
|
+
end
|
|
29
|
+
|
|
20
30
|
it "responds with 201 on create actions" do
|
|
21
31
|
@app = new_rack_app(schema: hyper_schema)
|
|
22
32
|
post "/apps"
|
|
@@ -8,6 +8,7 @@ describe Committee::RequestUnpacker do
|
|
|
8
8
|
request = Rack::Request.new(env)
|
|
9
9
|
unpacker = Committee::RequestUnpacker.new
|
|
10
10
|
assert_equal([{ "x" => "y" }, false], unpacker.unpack_request_params(request))
|
|
11
|
+
assert_equal '{"x":"y"}', env["rack.input"].read
|
|
11
12
|
end
|
|
12
13
|
|
|
13
14
|
it "unpacks JSON on Content-Type: application/vnd.api+json" do
|
|
@@ -58,6 +59,25 @@ describe Committee::RequestUnpacker do
|
|
|
58
59
|
end
|
|
59
60
|
end
|
|
60
61
|
|
|
62
|
+
it "uses raw bodies only when optimistic JSON parsing fails" do
|
|
63
|
+
[['hello', 'hello'], ['{"x":"y"}', { 'x' => 'y' }]].each do |body, expected|
|
|
64
|
+
request = Rack::Request.new({ "CONTENT_TYPE" => "text/plain", "rack.input" => StringIO.new(body) })
|
|
65
|
+
unpacker = Committee::RequestUnpacker.new(allow_raw_body: true, optimistic_json: true)
|
|
66
|
+
|
|
67
|
+
assert_equal [expected, false], unpacker.unpack_request_params(request)
|
|
68
|
+
assert_equal body, request.body.read
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "does not treat JSON or disabled forms as raw bodies" do
|
|
73
|
+
[['application/json', ''], ['application/x-www-form-urlencoded', 'x=y'], ['multipart/form-data', 'x=y']].each do |content_type, body|
|
|
74
|
+
request = Rack::Request.new({ "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new(body) })
|
|
75
|
+
unpacker = Committee::RequestUnpacker.new(allow_raw_body: true)
|
|
76
|
+
|
|
77
|
+
assert_equal [{}, false], unpacker.unpack_request_params(request)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
61
81
|
it "unpacks an empty hash on an empty request body" do
|
|
62
82
|
env = { "CONTENT_TYPE" => "application/json", "rack.input" => StringIO.new(""), }
|
|
63
83
|
request = Rack::Request.new(env)
|
|
@@ -74,6 +94,20 @@ describe Committee::RequestUnpacker do
|
|
|
74
94
|
end
|
|
75
95
|
end
|
|
76
96
|
|
|
97
|
+
it "unpacks form params through Rack::Lint without a rewindable input" do
|
|
98
|
+
app = Rack::Lint.new(lambda { |env|
|
|
99
|
+
params, is_form_params = Committee::RequestUnpacker.new(allow_form_params: true).unpack_request_params(Rack::Request.new(env))
|
|
100
|
+
assert_equal({ "x" => "y" }, params)
|
|
101
|
+
assert is_form_params
|
|
102
|
+
[200, {}, []]
|
|
103
|
+
})
|
|
104
|
+
env = Rack::MockRequest.env_for("/", method: "POST", input: "x=y", "CONTENT_TYPE" => "application/x-www-form-urlencoded")
|
|
105
|
+
|
|
106
|
+
status, _headers, _body = app.call(env)
|
|
107
|
+
|
|
108
|
+
assert_equal 200, status
|
|
109
|
+
end
|
|
110
|
+
|
|
77
111
|
it "unpacks form params with allow_form_params" do
|
|
78
112
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
|
79
113
|
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new("x=y"), }
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
require 'tmpdir'
|
|
5
|
+
|
|
6
|
+
describe 'YAML response error locations' do
|
|
7
|
+
def response_error(schema, path, data, status: 200, method: 'GET', content_type: 'application/json', **options)
|
|
8
|
+
app = ->(_env) { [status, { 'Content-Type' => content_type }, [JSON.generate(data)]] }
|
|
9
|
+
middleware = Committee::Middleware::ResponseValidation.new(app, { schema: schema, raise: true, strict: true }.merge(options))
|
|
10
|
+
assert_raises(Committee::InvalidResponse) { Rack::MockRequest.new(middleware).request(method, path) }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'includes the line of an inline OpenAPI 3 response property' do
|
|
14
|
+
error = response_error(open_api_3_schema, '/validate', { 'string' => 1 }, method: 'POST')
|
|
15
|
+
assert_includes error.message, 'normal.yaml:392'
|
|
16
|
+
assert_kind_of OpenAPIParser::ValidateError, error.original_error
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'includes the line in an external OpenAPI 3 reference' do
|
|
20
|
+
error = response_error(open_api_3_schema, '/ref-sample', { 'sample' => 1 })
|
|
21
|
+
assert_includes error.message, 'referee.yaml:15'
|
|
22
|
+
assert_match(/expected string/, error.message)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'includes the original line of an inline OpenAPI 2 response schema' do
|
|
26
|
+
schema = Committee::Drivers.load_from_file('test/data/openapi2/response_location.yml')
|
|
27
|
+
error = response_error(schema, '/api/widgets', { 'count' => 'wrong' })
|
|
28
|
+
assert_includes error.message, 'response_location.yml:18'
|
|
29
|
+
assert_match(/failed schema/, error.message)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'includes the definition line for an OpenAPI 2 reference' do
|
|
33
|
+
schema = Committee::Drivers.load_from_file('test/data/openapi2/response_location.yml')
|
|
34
|
+
error = response_error(schema, '/api/widgets', { 'message' => 1 }, status: 400, validate_success_only: false)
|
|
35
|
+
assert_includes error.message, 'response_location.yml:28'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'includes the response map line for an undefined OpenAPI 2 status' do
|
|
39
|
+
schema = Committee::Drivers.load_from_file('test/data/openapi2/response_location.yml')
|
|
40
|
+
error = response_error(schema, '/api/widgets', {}, status: 201)
|
|
41
|
+
assert_includes error.message, 'response_location.yml:12'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'includes the produces line for an OpenAPI 2 Content-Type error' do
|
|
45
|
+
schema = Committee::Drivers.load_from_file('test/data/openapi2/response_location.yml')
|
|
46
|
+
error = response_error(schema, '/api/widgets', {}, content_type: 'text/plain')
|
|
47
|
+
assert_includes error.message, 'response_location.yml:7'
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it 'includes the response map line for an undefined OpenAPI 3 status' do
|
|
51
|
+
error = response_error(open_api_3_schema, '/validate', {}, method: 'POST', status: 201)
|
|
52
|
+
assert_includes error.message, 'normal.yaml:384'
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'includes the response definition line for a strict OpenAPI 3 Content-Type error' do
|
|
56
|
+
error = response_error(open_api_3_schema, '/validate', {}, method: 'POST', content_type: 'text/plain', strict_response_content_type: true)
|
|
57
|
+
assert_includes error.message, 'normal.yaml:385'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'preserves the original message when loading from a Hash' do
|
|
61
|
+
schema = Committee::Drivers.load_from_data(open_api_3_data, open_api_3_schema_path, parser_options: { strict_reference_validation: true })
|
|
62
|
+
error = response_error(schema, '/validate', { 'string' => 1 }, method: 'POST')
|
|
63
|
+
assert_equal error.original_error.message, error.message
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'keeps locations after the main YAML file has been removed' do
|
|
67
|
+
Dir.mktmpdir('committee-schema-locations') do |dir|
|
|
68
|
+
path = File.join(dir, 'schema.yml')
|
|
69
|
+
File.write(path, File.read('test/data/openapi2/response_location.yml'))
|
|
70
|
+
schema = Committee::Drivers.load_from_file(path)
|
|
71
|
+
File.unlink(path)
|
|
72
|
+
error = response_error(schema, '/api/widgets', { 'count' => 'wrong' })
|
|
73
|
+
assert_includes error.message, "#{path}:18"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'locates properties inherited through a YAML merge' do
|
|
78
|
+
Dir.mktmpdir('committee-merged-schema') do |dir|
|
|
79
|
+
path = File.join(dir, 'merged.yml')
|
|
80
|
+
yaml = File.read('test/data/openapi2/response_location.yml')
|
|
81
|
+
yaml = yaml.sub(" type: object\n properties:\n count:\n type: integer", " <<: &response\n type: object\n properties:\n count:\n type: integer")
|
|
82
|
+
yaml = yaml.sub(" $ref: '#/definitions/Error'", ' <<: *response')
|
|
83
|
+
File.write(path, yaml)
|
|
84
|
+
schema = Committee::Drivers.load_from_file(path)
|
|
85
|
+
|
|
86
|
+
[200, 400].each do |status|
|
|
87
|
+
error = response_error(schema, '/api/widgets', { 'count' => 'wrong' }, status: status, validate_success_only: false)
|
|
88
|
+
assert_includes error.message, "#{path}:19"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it 'locates an OpenAPI 3.1 property inside a referenced schema' do
|
|
94
|
+
error = response_error(open_api_3_1_schema, '/items/1', ValidItem.merge('rating' => 6))
|
|
95
|
+
assert_includes error.message, '3_1.yaml:127'
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'preserves the validation error when a referenced YAML file is unavailable' do
|
|
99
|
+
Dir.mktmpdir('committee-remote-schema') do |dir|
|
|
100
|
+
path = File.join(dir, 'schema.yml')
|
|
101
|
+
reference_path = File.join(dir, 'referee.yaml')
|
|
102
|
+
File.write(path, File.read(open_api_3_schema_path))
|
|
103
|
+
File.write(reference_path, File.read('test/data/openapi3/referee.yaml'))
|
|
104
|
+
schema = Committee::Drivers.load_from_file(path, parser_options: { strict_reference_validation: true })
|
|
105
|
+
File.unlink(reference_path)
|
|
106
|
+
|
|
107
|
+
error = response_error(schema, '/ref-sample', { 'sample' => 1 })
|
|
108
|
+
assert_equal error.original_error.message, error.message
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -112,6 +112,22 @@ describe Committee::SchemaValidator::HyperSchema::ResponseValidator do
|
|
|
112
112
|
assert_equal message, e.message
|
|
113
113
|
end
|
|
114
114
|
|
|
115
|
+
it "validates OpenAPI 2 responses against the schema for their status" do
|
|
116
|
+
link = Committee::Drivers::OpenAPI2::Link.new
|
|
117
|
+
link.href = "/apps"
|
|
118
|
+
link.media_type = "application/json"
|
|
119
|
+
link.status_success = 200
|
|
120
|
+
success_schema = JsonSchema.parse!({ "type" => "object", "required" => ["id"], "properties" => { "id" => { "type" => "integer" } } })
|
|
121
|
+
error_schema = JsonSchema.parse!({ "type" => "object", "required" => ["error"], "properties" => { "error" => { "type" => "string" } } })
|
|
122
|
+
link.target_schemas = { 200 => success_schema, 400 => error_schema }
|
|
123
|
+
validator = Committee::SchemaValidator::HyperSchema::ResponseValidator.new(link, validate_success_only: false)
|
|
124
|
+
|
|
125
|
+
validator.call(200, @headers, { "id" => 1 })
|
|
126
|
+
validator.call(400, @headers, { "error" => "invalid request" })
|
|
127
|
+
assert_raises(Committee::InvalidResponse) { validator.call(200, @headers, { "error" => "invalid request" }) }
|
|
128
|
+
assert_raises(Committee::InvalidResponse) { validator.call(400, @headers, { "id" => 1, "error" => 1 }) }
|
|
129
|
+
end
|
|
130
|
+
|
|
115
131
|
private
|
|
116
132
|
|
|
117
133
|
def call
|