committee 3.1.1 → 3.2.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/middleware/response_validation.rb +16 -12
- data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +4 -0
- data/lib/committee/schema_validator/open_api_3/request_validator.rb +11 -1
- data/test/middleware/response_validation_open_api_3_test.rb +8 -0
- data/test/middleware/response_validation_test.rb +6 -0
- data/test/schema_validator/open_api_3/operation_wrapper_test.rb +16 -0
- data/test/schema_validator/open_api_3/request_validator_test.rb +26 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c978ba3ca98ab6a684b1555ebc3e3575ee5fb20ae11c17bc7b8c2baebd71eb86
|
4
|
+
data.tar.gz: 0424b819be1a1b0872894d8c0b897e325868347e54d19b443a351f1270d3b097
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 306d7ebf406280f3ad9279543e010046a691bed7b6ba89c61b20663e256e6e7efacd7a4c2a0a2b021d405ab603519c887319ec3963204de09fb9405a66144032
|
7
|
+
data.tar.gz: d0043bd37f971a053843acc791bb9f9216ef8b2aa58946e5ab6a7d0ddcb9f4190deec402fc5424d4a2c14fe074f581576663f69252319d7c7085b9355ec48355
|
@@ -8,25 +8,29 @@ module Committee
|
|
8
8
|
def initialize(app, options = {})
|
9
9
|
super
|
10
10
|
@validate_success_only = @schema.validator_option.validate_success_only
|
11
|
+
@ignore_error = options.fetch(:ignore_error, false)
|
11
12
|
end
|
12
13
|
|
13
14
|
def handle(request)
|
14
|
-
|
15
|
+
begin
|
16
|
+
status, headers, response = @app.call(request.env)
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
v = build_schema_validator(request)
|
19
|
+
v.response_validate(status, headers, response) if v.link_exist? && self.class.validate?(status, validate_success_only)
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
rescue Committee::InvalidResponse
|
22
|
+
handle_exception($!, request.env)
|
23
|
+
|
24
|
+
raise if @raise
|
25
|
+
return @error_class.new(500, :invalid_response, $!.message).render unless @ignore_error
|
26
|
+
rescue JSON::ParserError
|
27
|
+
handle_exception($!, request.env)
|
22
28
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
handle_exception($!, request.env)
|
29
|
+
raise Committee::InvalidResponse if @raise
|
30
|
+
return @error_class.new(500, :invalid_response, "Response wasn't valid JSON.").render unless @ignore_error
|
31
|
+
end
|
27
32
|
|
28
|
-
|
29
|
-
@error_class.new(500, :invalid_response, "Response wasn't valid JSON.").render
|
33
|
+
[status, headers, response]
|
30
34
|
end
|
31
35
|
|
32
36
|
class << self
|
@@ -25,7 +25,17 @@ module Committee
|
|
25
25
|
return true unless request.post? || request.put? || request.patch?
|
26
26
|
return true if @operation_object.valid_request_content_type?(content_type)
|
27
27
|
|
28
|
-
|
28
|
+
message = if valid_content_types.size > 1
|
29
|
+
types = valid_content_types.map {|x| %{"#{x}"} }.join(', ')
|
30
|
+
%{"Content-Type" request header must be set to any of the following: [#{types}].}
|
31
|
+
else
|
32
|
+
%{"Content-Type" request header must be set to "#{valid_content_types.first}".}
|
33
|
+
end
|
34
|
+
raise Committee::InvalidRequest, message
|
35
|
+
end
|
36
|
+
|
37
|
+
def valid_content_types
|
38
|
+
@operation_object&.request_content_types
|
29
39
|
end
|
30
40
|
end
|
31
41
|
end
|
@@ -26,6 +26,14 @@ describe Committee::Middleware::ResponseValidation do
|
|
26
26
|
assert_equal "{\"id\":\"invalid_response\",\"message\":\"Response wasn't valid JSON.\"}", last_response.body
|
27
27
|
end
|
28
28
|
|
29
|
+
it "passes through a invalid json with ignore_error option" do
|
30
|
+
@app = new_response_rack("not_json", {}, schema: open_api_3_schema, ignore_error: true)
|
31
|
+
|
32
|
+
get "/characters"
|
33
|
+
|
34
|
+
assert_equal 200, last_response.status
|
35
|
+
end
|
36
|
+
|
29
37
|
it "passes through not definition" do
|
30
38
|
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, schema: open_api_3_schema)
|
31
39
|
get "/no_data"
|
@@ -38,6 +38,12 @@ describe Committee::Middleware::ResponseValidation do
|
|
38
38
|
assert_match(/{} is not an array/i, last_response.body)
|
39
39
|
end
|
40
40
|
|
41
|
+
it "detects a response invalid due to schema with ignore_error option" do
|
42
|
+
@app = new_rack_app("{}", {}, schema: hyper_schema, ignore_error: true)
|
43
|
+
get "/apps"
|
44
|
+
assert_equal 200, last_response.status
|
45
|
+
end
|
46
|
+
|
41
47
|
it "detects a response invalid due to not being JSON" do
|
42
48
|
@app = new_rack_app("{_}", {}, schema: hyper_schema)
|
43
49
|
get "/apps"
|
@@ -146,5 +146,21 @@ describe Committee::SchemaValidator::OpenAPI3::OperationWrapper do
|
|
146
146
|
assert e.message.start_with?("a class is String but")
|
147
147
|
end
|
148
148
|
end
|
149
|
+
|
150
|
+
describe '#content_types' do
|
151
|
+
it 'returns supported content types' do
|
152
|
+
@path = '/validate_content_types'
|
153
|
+
@method = 'post'
|
154
|
+
|
155
|
+
assert_equal ["application/json", "application/binary"], operation_object.request_content_types
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'returns an empty array when the content of requestBody does not exist' do
|
159
|
+
@path = '/characters'
|
160
|
+
@method = 'get'
|
161
|
+
|
162
|
+
assert_equal [], operation_object.request_content_types
|
163
|
+
end
|
164
|
+
end
|
149
165
|
end
|
150
166
|
end
|
@@ -10,7 +10,7 @@ describe Committee::SchemaValidator::OpenAPI3::RequestValidator do
|
|
10
10
|
@app
|
11
11
|
end
|
12
12
|
|
13
|
-
it "skip
|
13
|
+
it "skip validation when link does not exist" do
|
14
14
|
@app = new_rack_app(schema: open_api_3_schema)
|
15
15
|
params = {}
|
16
16
|
header "Content-Type", "application/json"
|
@@ -21,11 +21,34 @@ describe Committee::SchemaValidator::OpenAPI3::RequestValidator do
|
|
21
21
|
it "optionally content_type check" do
|
22
22
|
@app = new_rack_app(check_content_type: true, schema: open_api_3_schema)
|
23
23
|
params = {
|
24
|
-
|
24
|
+
"string_post_1" => "cloudnasium"
|
25
25
|
}
|
26
26
|
header "Content-Type", "text/html"
|
27
27
|
post "/characters", JSON.generate(params)
|
28
28
|
assert_equal 400, last_response.status
|
29
|
+
|
30
|
+
body = JSON.parse(last_response.body)
|
31
|
+
message =
|
32
|
+
%{"Content-Type" request header must be set to "application/json".}
|
33
|
+
|
34
|
+
assert_equal "bad_request", body['id']
|
35
|
+
assert_equal message, body['message']
|
36
|
+
end
|
37
|
+
|
38
|
+
it "validates content_type" do
|
39
|
+
@app = new_rack_app(check_content_type: true, schema: open_api_3_schema)
|
40
|
+
params = {
|
41
|
+
"string_post_1" => "cloudnasium"
|
42
|
+
}
|
43
|
+
header "Content-Type", "text/html"
|
44
|
+
post "/validate_content_types", JSON.generate(params)
|
45
|
+
assert_equal 400, last_response.status
|
46
|
+
|
47
|
+
body = JSON.parse(last_response.body)
|
48
|
+
message =
|
49
|
+
%{"Content-Type" request header must be set to any of the following: ["application/json", "application/binary"].}
|
50
|
+
|
51
|
+
assert_equal message, body['message']
|
29
52
|
end
|
30
53
|
|
31
54
|
it "optionally skip content_type check" do
|
@@ -38,7 +61,7 @@ describe Committee::SchemaValidator::OpenAPI3::RequestValidator do
|
|
38
61
|
assert_equal 200, last_response.status
|
39
62
|
end
|
40
63
|
|
41
|
-
it "if not exist
|
64
|
+
it "if not exist requestBody definition, skip content_type check" do
|
42
65
|
@app = new_rack_app(check_content_type: true, schema: open_api_3_schema)
|
43
66
|
params = {
|
44
67
|
"string_post_1" => "cloudnasium"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: committee
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-09-
|
13
|
+
date: 2019-09-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: json_schema
|