Browse Source

Handle non json address responses (#2696)

* Check if response is http success

* Fix code scanning alert no. 65: Incomplete regular expression for hostnames

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
pull/2680/head^2
kosiakkatrina 2 months ago committed by GitHub
parent
commit
7e5c4f49cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      app/services/address_client.rb
  2. 25
      spec/requests/lettings_logs_controller_spec.rb

6
app/services/address_client.rb

@ -20,7 +20,11 @@ class AddressClient
end
def result
@result ||= JSON.parse(response.body)["results"]&.map { |address| address["DPA"] }
if response.is_a?(Net::HTTPSuccess)
@result ||= JSON.parse(response.body)["results"]&.map { |address| address["DPA"] }
else
@result = nil
end
end
private

25
spec/requests/lettings_logs_controller_spec.rb

@ -1317,6 +1317,31 @@ RSpec.describe LettingsLogsController, type: :request do
end
end
context "when os places api returns non json response" do
let(:lettings_log) do
build(:lettings_log,
:completed,
assigned_to: user,
uprn_known: 0,
uprn: nil,
address_line1_input: "Address line 1",
postcode_full_input: "SW1A 1AA")
end
let(:id) { lettings_log.id }
before do
lettings_log.save!(validate: false)
WebMock.stub_request(:get, /https:\/\/api\.os\.uk\/search\/places\/v1\/find/)
.to_return(status: 503, body: "something went wrong", headers: {})
sign_in user
end
it "renders the property information check answers without error" do
get "/lettings-logs/#{id}/property-information/check-answers"
expect(response).to have_http_status(:ok)
end
end
context "when requesting CSV download" do
let(:headers) { { "Accept" => "text/html" } }
let(:search_term) { "foo" }

Loading…
Cancel
Save