Skip to content

Commit ddec9ed

Browse files
authored
Parse JSON when lower-case content-type is given (#440)
* Parse JSON when lower-case content-type is given * Use casecmp? * Fix format based on the lint result * Remove puts for debug in the test
1 parent d35ecf1 commit ddec9ed

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

lib/committee/schema_validator/hyper_schema.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ def response_validate(status, headers, response, _test_method = false)
2828

2929
data = {}
3030
unless full_body.empty?
31-
parse_to_json = !validator_option.parse_response_by_content_type ||
32-
headers.fetch('Content-Type', nil)&.start_with?('application/json')
31+
parse_to_json = if validator_option.parse_response_by_content_type
32+
content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }
33+
headers.fetch(content_type_key, nil)&.start_with?('application/json')
34+
else
35+
true
36+
end
37+
3338
data = JSON.parse(full_body) if parse_to_json
3439
end
3540

@@ -101,11 +106,11 @@ def parameter_coerce!(request, link, coerce_key)
101106
return unless link_exist?
102107

103108
Committee::SchemaValidator::HyperSchema::ParameterCoercer.
104-
new(request.env[coerce_key],
105-
link.schema,
106-
coerce_date_times: validator_option.coerce_date_times,
107-
coerce_recursive: validator_option.coerce_recursive).
108-
call!
109+
new(request.env[coerce_key],
110+
link.schema,
111+
coerce_date_times: validator_option.coerce_date_times,
112+
coerce_recursive: validator_option.coerce_recursive).
113+
call!
109114
end
110115
end
111116
end

lib/committee/schema_validator/open_api_3.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ def response_validate(status, headers, response, test_method = false)
2626
full_body << chunk
2727
end
2828

29-
parse_to_json = !validator_option.parse_response_by_content_type ||
30-
headers.fetch('Content-Type', nil)&.start_with?('application/json')
29+
parse_to_json = if validator_option.parse_response_by_content_type
30+
content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }
31+
headers.fetch(content_type_key, nil)&.start_with?('application/json')
32+
else
33+
true
34+
end
35+
3136
data = if parse_to_json
3237
full_body.empty? ? {} : JSON.parse(full_body)
3338
else

test/middleware/response_validation_open_api_3_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ def app
1717
assert_equal 200, last_response.status
1818
end
1919

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+
2036
it "passes through a invalid json" do
2137
@app = new_response_rack("not_json", {}, schema: open_api_3_schema)
2238

test/middleware/response_validation_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ def app
1515
assert_equal 200, last_response.status
1616
end
1717

18+
it "passes through a valid response with content-type (lower-case)" do
19+
options = { schema: hyper_schema }
20+
status = 200
21+
headers = { 'content-type' => 'application/json' }
22+
response = JSON.generate([ValidApp])
23+
@app = Rack::Builder.new {
24+
use Committee::Middleware::ResponseValidation, options
25+
run lambda { |_|
26+
[status, headers, [response]]
27+
}
28+
}
29+
30+
get "/apps"
31+
assert_equal 200, last_response.status
32+
end
33+
1834
it "doesn't call error_handler (has a arg) when response is valid" do
1935
called = false
2036
pr = ->(_e) { called = true }

0 commit comments

Comments
 (0)