Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spaces in a url being encoded as '+' instead of '%20' #1672

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading