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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 169dfa8f079626447ca5086429c0bd025d8180f3ef5b7a7194d1333bdeff8fab
4
- data.tar.gz: 2c8eeece5b411266f482808b7caf08d2de285f64e78e705990a4b7b9606902d3
3
+ metadata.gz: c978ba3ca98ab6a684b1555ebc3e3575ee5fb20ae11c17bc7b8c2baebd71eb86
4
+ data.tar.gz: 0424b819be1a1b0872894d8c0b897e325868347e54d19b443a351f1270d3b097
5
5
  SHA512:
6
- metadata.gz: b891ea4d1498b2fc16f0d241520486736b0b707bf401e96bdab09795ff165c23d86344a2b29cfcf1dbca83d8a773b85a08e59dc09220c5acca2738e485c3994f
7
- data.tar.gz: ca657dfb18edaf157cb01ca48b522882c165a8c06216c84deda9a88a7718fb61da4ea3a3b017e4ac8594e83f97f904d4f0f1ad1947ee9418b977ddf4884e8c52
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
- status, headers, response = @app.call(request.env)
15
+ begin
16
+ status, headers, response = @app.call(request.env)
15
17
 
16
- v = build_schema_validator(request)
17
- v.response_validate(status, headers, response) if v.link_exist? && self.class.validate?(status, validate_success_only)
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
- [status, headers, response]
20
- rescue Committee::InvalidResponse
21
- handle_exception($!, request.env)
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
- raise if @raise
24
- @error_class.new(500, :invalid_response, $!.message).render
25
- rescue JSON::ParserError
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
- raise Committee::InvalidResponse if @raise
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
@@ -64,6 +64,10 @@ module Committee
64
64
  end
65
65
  end
66
66
 
67
+ def request_content_types
68
+ request_operation.operation_object&.request_body&.content&.keys || []
69
+ end
70
+
67
71
  private
68
72
 
69
73
  attr_reader :request_operation
@@ -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
- raise Committee::InvalidRequest, %{"Content-Type" request header must be set to "#{@operation_object}".}
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 validaiton when link does not exist" do
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
- "string_post_1" => "cloudnasium"
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 requsetBody definition, skip content_type check" do
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.1.1
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-04 00:00:00.000000000 Z
13
+ date: 2019-09-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json_schema