Skip to content

Commit

Permalink
Fix parsing response if Content-Type contains charset
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed May 21, 2024
1 parent 92a0271 commit b39d620
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def self.parse_response(response)

content_type = response.content_type.presence or return body

case Mime::Type.lookup(content_type)
when JSON_TYPE then JSON.parse(body)
else raise InvalidResponseError.new response: response, message: 'Unknown Content-Type'
if Mime::Type.lookup(content_type).match? JSON_TYPE
JSON.parse(body)
else
raise InvalidResponseError.new response: response, message: 'Unknown Content-Type'
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/adapters/abstract_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ class AbstractAdapterTest < ActiveSupport::TestCase
assert_nil subject.new('https://example.com').authentication
end
end

test 'content-type with charset' do
content_type = 'application/json;charset=UTF-8'
stub_request(:get, 'https://example.com/.well-known/openid-configuration').
to_return(body: '{}', headers: {'Content-Type': content_type})
stub_request(:post, 'https://example.com').to_return(status: 200, body: '{"access_token": "test"}', headers: {'Content-Type': content_type})

assert_nothing_raised do
subject.new('https://example.com').authentication
end
end
end

0 comments on commit b39d620

Please sign in to comment.