Skip to content

Commit

Permalink
Merge pull request #1672 from iBlackShadow/fix-url-encoding
Browse files Browse the repository at this point in the history
Fix spaces in a url being encoded as '+' instead of '%20' in lookups that use URL path instead of query string (Mapbox and Bing).
  • Loading branch information
alexreisner authored Dec 4, 2024
2 parents c1a19f2 + 5c2c6d0 commit ec2f034
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/geocoder/lookups/bing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def required_api_key_parts
private # ---------------------------------------------------------------

def base_query_url(query)
text = CGI.escape(query.sanitized_text.strip)
text = ERB::Util.url_encode(query.sanitized_text.strip)
url = "#{protocol}://dev.virtualearth.net/REST/v1/Locations/"
if query.reverse_geocode?
url + "#{text}?"
Expand Down
6 changes: 3 additions & 3 deletions lib/geocoder/lookups/mapbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def query_url_params(query)
end

def mapbox_search_term(query)
require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
require 'erb' unless defined?(ERB) && defined?(ERB::Util.url_encode)
if query.reverse_geocode?
lat,lon = query.coordinates
"#{CGI.escape lon},#{CGI.escape lat}"
"#{ERB::Util.url_encode lon},#{ERB::Util.url_encode lat}"
else
# truncate at first semicolon so Mapbox doesn't go into batch mode
# (see Github issue #1299)
CGI.escape query.text.to_s.split(';').first.to_s
ERB::Util.url_encode query.text.to_s.split(';').first.to_s
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/unit/lookups/bing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_query_url_escapes_spaces_in_address
"manchester, lancashire",
:region => "uk"
))
assert_match(%r!Locations/uk/\?q=manchester%2C\+lancashire!, url)
assert_match(%r!Locations/uk/\?q=manchester%2C%20lancashire!, url)
end

def test_query_url_strips_trailing_and_leading_spaces
Expand All @@ -69,7 +69,7 @@ def test_query_url_strips_trailing_and_leading_spaces
" manchester, lancashire ",
:region => "uk"
))
assert_match(%r!Locations/uk/\?q=manchester%2C\+lancashire!, url)
assert_match(%r!Locations/uk/\?q=manchester%2C%20lancashire!, url)
end

def test_raises_exception_when_service_unavailable
Expand Down
4 changes: 2 additions & 2 deletions test/unit/lookups/mapbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def setup
def test_url_contains_api_key
Geocoder.configure(mapbox: {api_key: "abc123"})
query = Geocoder::Query.new("Leadville, CO")
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C+CO.json?access_token=abc123", query.url
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C%20CO.json?access_token=abc123", query.url
end

def test_url_contains_params
Geocoder.configure(mapbox: {api_key: "abc123"})
query = Geocoder::Query.new("Leadville, CO", {params: {country: 'CN'}})
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C+CO.json?access_token=abc123&country=CN", query.url
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C%20CO.json?access_token=abc123&country=CN", query.url
end

def test_result_components
Expand Down

0 comments on commit ec2f034

Please sign in to comment.