Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/committee/schema_validator/open_api_3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def response_validate(status, headers, response, test_method = false, custom_bod
data = if custom_body_parser
custom_body_parser.call(full_body)
elsif parse_to_json
full_body.empty? ? {} : JSON.parse(full_body)
# HEAD responses have no body, but still require header validation.
full_body.empty? && @request.head? ? {} : JSON.parse(full_body)
else
full_body
end
Expand Down
30 changes: 26 additions & 4 deletions test/middleware/response_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,32 @@ def app
end
end

it "optionally validates non-2xx blank responses" do
@app = new_response_rack("", {}, schema: open_api_3_schema, validate_success_only: false)
get "/characters"
['/characters', '/validate_response_array'].each do |path|
it "rejects empty JSON responses for #{path}" do
@app = new_response_rack("", {}, schema: open_api_3_schema, validate_success_only: false)
get path

assert_equal 500, last_response.status
assert_equal "Response wasn't valid JSON.", JSON.parse(last_response.body)['message']
end
end

it "preserves empty HEAD responses and validates their headers" do
document = open_api_3_data
document['paths']['/characters']['head']['responses']['200']['headers'] = { 'X-Status' => { 'required' => true, 'schema' => { 'type' => 'string', 'enum' => ['ok'] } }, }
schema = Committee::Drivers.load_from_data(document, open_api_3_schema_path, parser_options: { strict_reference_validation: true })
response_headers = { 'Content-Type' => 'application/json', 'X-Status' => 'ok' }
@app = Rack::Builder.new {
use Committee::Middleware::ResponseValidation, schema: schema
run ->(_) { [200, response_headers, []] }
}

head '/characters'
assert_equal 200, last_response.status

response_headers['X-Status'] = 'invalid'
head '/characters'
assert_equal 500, last_response.status
end

it "optionally validates non-2xx invalid responses with invalid json" do
Expand Down Expand Up @@ -378,7 +400,7 @@ def app
end

it "passes through when response has no content schema with strict_response_content_type: true" do
@app = new_response_rack("", { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema, strict_response_content_type: true)
@app = new_response_rack("{}", { "Content-Type" => "application/vnd.api+json" }, schema: open_api_3_schema, strict_response_content_type: true)
get "/no_content_schema"
assert_equal 200, last_response.status
end
Expand Down
30 changes: 30 additions & 0 deletions test/schema_validator/open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,34 @@ def validator_for(path, method)
assert_nil validator_for("/no-such-path", "GET").operation_object
end
end

describe "empty JSON responses" do
[
['/characters', '', '{}'],
['/validate_response_array', [], ['[]']],
].each do |path, empty_body, valid_body|
it "rejects an empty JSON body for #{path} while accepting an encoded empty value" do
validator = validator_for(path, 'GET')
headers = { 'Content-Type' => 'application/json' }

assert_silent { validator.response_validate(200, headers, valid_body) }
assert_raises(JSON::ParserError) { validator.response_validate(200, headers, empty_body) }
end
end

it "preserves empty non-JSON response bodies" do
validator = validator_for('/csv', 'GET')

assert_silent { validator.response_validate(200, { 'Content-Type' => 'text/csv' }, '') }
end

it "lets custom parsers handle empty JSON response bodies" do
parsed_body = nil
parser = ->(body) { parsed_body = body; {} }

validator_for('/characters', 'GET').response_validate(200, { 'Content-Type' => 'application/json' }, '', false, parser)

assert_equal '', parsed_body
end
end
end