Skip to content
Snippets Groups Projects
Commit 650add80 authored by Abe Voelker's avatar Abe Voelker
Browse files

Handle IPv6 addresses when removing port numbers in request.location

parent b00d68bf
No related branches found
No related tags found
No related merge requests found
......@@ -81,7 +81,18 @@ module Geocoder
end
def geocoder_remove_port_from_addresses(ip_addresses)
ip_addresses.map { |ip| ip.split(':').first }
ip_addresses.map do |ip|
# IPv4
if ip.count('.') > 0
ip.split(':').first
# IPv6 bracket notation
elsif match = ip.match(/\[(\S+)\]/)
match.captures.first
# IPv6 bare notation
else
ip
end
end
end
def geocoder_reject_non_ipv4_addresses(ip_addresses)
......
......@@ -72,6 +72,12 @@ class RequestTest < GeocoderTestCase
req = MockRequest.new()
assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips)
end
def test_geocoder_remove_port_from_ipv6_addresses_with_port
expected_ips = ['2600:1008:b16e:26da:ecb3:22f7:6be4:2137', '2600:1901:0:2df5::', '2001:db8:1f70::999:de8:7648:6e8', '10.128.0.2']
ips = ['2600:1008:b16e:26da:ecb3:22f7:6be4:2137', '2600:1901:0:2df5::', '[2001:db8:1f70::999:de8:7648:6e8]:100', '10.128.0.2']
req = MockRequest.new()
assert_equal expected_ips, req.send(:geocoder_remove_port_from_addresses, ips)
end
def test_geocoder_remove_port_from_addresses_without_port
expected_ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3']
......
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