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
2 changes: 1 addition & 1 deletion lib/committee/schema_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Committee
module SchemaValidator
class << self
def request_media_type(request)
request.media_type.to_s
Rack::MediaType.type(request.env['CONTENT_TYPE'])
end

# @param [String] prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def validate_get_request_params(path_params, query_params, headers, validator_op

def validate_post_request_params(path_params, query_params, body_params, headers, validator_option)
content_type_key = headers.keys.detect { |k| k.casecmp?('Content-Type') }
content_type = headers[content_type_key].to_s.split(';').first.to_s
content_type = Rack::MediaType.type(headers[content_type_key])

# bad performance because when we coerce value, same check
validate_path_and_query_params(path_params, query_params, headers, validator_option)
Expand Down
23 changes: 23 additions & 0 deletions test/schema_validator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "test_helper"

describe Committee::SchemaValidator do
it "parses simple content types" do
request = Rack::Request.new({ "CONTENT_TYPE" => "application/json" })
media_type = Committee::SchemaValidator.request_media_type(request)
assert_equal 'application/json', media_type
end

it "parses comma-delineated content types" do
request = Rack::Request.new({ "CONTENT_TYPE" => "multipart/form-data, application/json" })
media_type = Committee::SchemaValidator.request_media_type(request)
assert_equal 'multipart/form-data', media_type
end

it "parses semicolon-delineated content types" do
request = Rack::Request.new({ "CONTENT_TYPE" => "multipart/form-data; application/json" })
media_type = Committee::SchemaValidator.request_media_type(request)
assert_equal 'multipart/form-data', media_type
end
end