From 38e3794251c35bffab20dee8d52ca6e90258483a Mon Sep 17 00:00:00 2001 From: Daria Mayorova Date: Tue, 21 May 2024 11:46:00 +0200 Subject: [PATCH] Fix parsing response if Content-Type contains charset --- app/adapters/abstract_adapter.rb | 7 ++++--- test/adapters/abstract_adapter_test.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/adapters/abstract_adapter.rb b/app/adapters/abstract_adapter.rb index 47c65552..e135b00c 100644 --- a/app/adapters/abstract_adapter.rb +++ b/app/adapters/abstract_adapter.rb @@ -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 diff --git a/test/adapters/abstract_adapter_test.rb b/test/adapters/abstract_adapter_test.rb index e00332a0..a79ca283 100644 --- a/test/adapters/abstract_adapter_test.rb +++ b/test/adapters/abstract_adapter_test.rb @@ -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