forked from geokit/geokit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ipgeocoder.rb
134 lines (121 loc) · 4.06 KB
/
test_ipgeocoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# encoding: utf-8
require File.join(File.dirname(__FILE__), 'helper')
class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
IP_FAILURE = <<-EOF
Country: SWITZERLAND (CH)
City: (Unknown City)
Latitude:
Longitude:
EOF
IP_SUCCESS = <<-EOF
Country: UNITED STATES (US)
City: Sugar Grove, IL
Latitude: 41.7696
Longitude: -88.4588
EOF
IP_LATIN = <<-EOF
Country: BRAZIL (BR)
City: S\xE3o Jos\xE9 do Rio Pr\xEAto\n
Latitude: -20.8
Longitude: -49.3833
EOF
IP_UNICODED = <<-EOF
Country: SWEDEN (SE)
City: Borås
Latitude: 57.7167
Longitude: 12.9167
EOF
PRIVATE_IPS_TO_TEST = [
'10.10.10.10',
'172.16.1.3',
'172.22.3.42',
'172.30.254.164',
'192.168.1.1',
'0.0.0.0',
'127.0.0.1',
'240.3.4.5',
'225.1.6.55',
].freeze
def setup
super
@success.provider = 'ip'
@base_url = 'http://api.hostip.info/get_html.php'
end
def test_successful_lookup
success = MockSuccess.new
success.expects(:body).returns(IP_SUCCESS)
url = "#{@base_url}?ip=12.215.42.19&position=true"
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
location = Geokit::Geocoders::IpGeocoder.geocode('12.215.42.19')
assert_not_nil location
assert_equal 41.7696, location.lat
assert_equal(-88.4588, location.lng)
assert_equal 'Sugar Grove', location.city
assert_equal 'IL', location.state
assert_equal 'US', location.country_code
assert_equal 'ip', location.provider
assert location.success?
end
def test_unicoded_lookup
success = MockSuccess.new
success.expects(:body).returns(IP_UNICODED)
url = "#{@base_url}?ip=12.215.42.19&position=true"
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
location = Geokit::Geocoders::IpGeocoder.geocode('12.215.42.19')
assert_not_nil location
assert_equal 57.7167, location.lat
assert_equal 12.9167, location.lng
assert_equal "Bor\303\245s", location.city
assert_nil location.state
assert_equal 'SE', location.country_code
assert_equal 'ip', location.provider
assert location.success?
end
def test_non_unicoded_lookup
success = MockSuccess.new
success.stubs(:body).returns(IP_LATIN)
success.stubs(:[]).with('content-type').returns('text/plain; charset=iso-8859-1')
url = "#{@base_url}?ip=201.23.177.144&position=true"
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
location = Geokit::Geocoders::IpGeocoder.geocode('201.23.177.144')
assert_not_nil location
assert_equal(-20.8, location.lat)
assert_equal(-49.3833, location.lng)
assert_equal "São José Do Rio Prêto", location.city
assert_nil location.state
assert_equal 'BR', location.country_code
assert_equal 'ip', location.provider
assert location.success?
end
def test_failed_lookup
failure = MockSuccess.new
failure.expects(:body).returns(IP_FAILURE)
url = "#{@base_url}?ip=128.178.0.0&position=true"
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
location = Geokit::Geocoders::IpGeocoder.geocode('128.178.0.0')
assert_not_nil location
assert !location.success?
end
def test_private_ips
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).never
PRIVATE_IPS_TO_TEST.each do |ip|
location = Geokit::Geocoders::IpGeocoder.geocode(ip)
assert_not_nil location
assert !location.success?
end
end
def test_invalid_ip
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).never
location = Geokit::Geocoders::IpGeocoder.geocode('blah')
assert_not_nil location
assert !location.success?
end
def test_service_unavailable
failure = MockFailure.new
url = "#{@base_url}?ip=12.215.42.19&position=true"
Geokit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
location = Geokit::Geocoders::IpGeocoder.geocode('12.215.42.19')
assert_not_nil location
assert !location.success?
end
end