From 3b6199b56de2eda0b35fcb13a76438916961ba47 Mon Sep 17 00:00:00 2001 From: nov Date: Fri, 14 Oct 2022 11:45:59 +0900 Subject: [PATCH] fix json response handling --- lib/apple_id/client.rb | 4 ++-- spec/support/webmock_helper.rb | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/apple_id/client.rb b/lib/apple_id/client.rb index 92025ef..c1907b3 100644 --- a/lib/apple_id/client.rb +++ b/lib/apple_id/client.rb @@ -45,12 +45,12 @@ def setup_required_scope(scopes) end def handle_success_response(response) - token_hash = JSON.parse(response.body).with_indifferent_access + token_hash = response.body.with_indifferent_access AccessToken.new token_hash.delete(:access_token), token_hash.merge(client: self) end def handle_error_response(response) - error = JSON.parse(response.body).with_indifferent_access + error = response.body.with_indifferent_access raise Error.new(response.status, error) end end diff --git a/spec/support/webmock_helper.rb b/spec/support/webmock_helper.rb index 5a26b12..3a2180f 100644 --- a/spec/support/webmock_helper.rb +++ b/spec/support/webmock_helper.rb @@ -22,7 +22,13 @@ def request_to(endpoint, method = :get) def response_for(response_file, options = {}) response = {} - response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{options[:format] || :json}")) + format = options[:format] || :json + if format == :json + response[:headers] = { + 'Content-Type': 'application/json' + } + end + response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{format}")) if options[:status] response[:status] = options[:status] end