Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/hotfix/APPEALS-25955' into hotfi…
Browse files Browse the repository at this point in the history
…x/APPEALS-25321-and-APPEALS-25955
  • Loading branch information
ThorntonMatthew committed Jul 19, 2023
2 parents b058136 + 0f5fb86 commit ec8fcaa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 7 additions & 3 deletions app/controllers/idt/api/v2/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ def format_response(response)
response_body = response.raw_body

begin
parsed_response = JSON.parse(response_body)
parsed_response = if [ActiveSupport::HashWithIndifferentAccess, Hash].include?(response_body.class)
response_body
else
JSON.parse(response_body)
end

# Convert keys from camelCase to snake_case
parsed_response.deep_transform_keys do |key|
key.to_s.underscore.gsub(/e(\d)/, 'e_\1')
end
rescue JSON::ParseError => error
log_error(error + " Distribution ID: #{params[:distribution_id]}")
rescue JSON::ParserError => error
log_error(error)

response_body
end
Expand Down
16 changes: 15 additions & 1 deletion spec/controllers/idt/api/v2/distributions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,26 @@
}
end

subject { get :distribution, params: { distribution_id: vbms_distribution.id } }

it "returns the expected converted response" do
get :distribution, params: { distribution_id: vbms_distribution.id }
subject

expect(response).to have_http_status(200)
expect(JSON.parse(response.body.to_json)).to eq(expected_response.to_json)
end

it "Returns Pacman's response whenever we receive something we cannot parse as JSON and logs the error" do
allow(PacmanService).to receive(:get_distribution_request).and_return(
HTTPI::Response.new(200, {}, "An invalid JSON string")
)

expect_any_instance_of(Idt::Api::V2::DistributionsController).to receive(:log_error)

subject

expect(response.body).to eq "An invalid JSON string"
end
end

context "render_error" do
Expand Down

0 comments on commit ec8fcaa

Please sign in to comment.