Skip to content
Snippets Groups Projects
Commit d807048c authored by Alex Reisner's avatar Alex Reisner
Browse files

Raise RequestDenied on HTTP 401 response.

This is relevant for SmartyStreets, and seems like a good idea in
general (possibly supported by other APIs).
parent 2adc5d29
No related branches found
No related tags found
No related merge requests found
......@@ -214,6 +214,7 @@ module Geocoder
else
check_api_key_configuration!(query)
response = make_api_request(query)
check_response_for_errors!(response)
body = response.body
if cache and valid_response?(response)
cache[key] = body
......@@ -223,6 +224,13 @@ module Geocoder
body
end
def check_response_for_errors!(response)
if response.code.to_i == 401
raise_error(Geocoder::RequestDenied) ||
warn("Geocoding API error: 401 Unauthorized")
end
end
##
# Make an HTTP(S) request to a geocoding API and
# return the response object.
......
......@@ -347,3 +347,11 @@ class GeocoderTestCase < Test::Unit::TestCase
Geocoder.configure(:api_key => key)
end
end
class MockHttpResponse
attr_reader :code, :body
def initialize(options = {})
@code = options[:code].to_s
@body = options[:body]
end
end
......@@ -56,6 +56,15 @@ class LookupTest < GeocoderTestCase
end
end
def test_raises_exception_on_401_response
Geocoder.configure(always_raise: [Geocoder::RequestDenied])
assert_raises Geocoder::RequestDenied do
lookup = Geocoder::Lookup.get(:smarty_streets)
response = MockHttpResponse.new(code: 401)
lookup.send(:check_response_for_errors!, response)
end
end
def test_raises_exception_on_invalid_key
Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
#Geocoder::Lookup.all_services_except_test.each do |l|
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment