forked from geokit/geokit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_geo_plugin_geocoder.rb
72 lines (65 loc) · 2.39 KB
/
test_geo_plugin_geocoder.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
require File.join(File.dirname(__FILE__), 'helper')
class GeoPluginGeocoderTest < BaseGeocoderTest #:nodoc: all
IP_SUCCESS = <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<geoPlugin>
<geoplugin_city>Belo Horizonte</geoplugin_city>
<geoplugin_region>Minas Gerais</geoplugin_region>
<geoplugin_areaCode>0</geoplugin_areaCode>
<geoplugin_dmaCode>0</geoplugin_dmaCode>
<geoplugin_countryCode>BR</geoplugin_countryCode>
<geoplugin_countryName>Brazil</geoplugin_countryName>
<geoplugin_continentCode>SA</geoplugin_continentCode>
<geoplugin_latitude>-19.916700</geoplugin_latitude>
<geoplugin_longitude>-43.933300</geoplugin_longitude>
<geoplugin_currencyCode>BRL</geoplugin_currencyCode>
<geoplugin_currencySymbol>R$</geoplugin_currencySymbol>
<geoplugin_currencyConverter>2.2575001717</geoplugin_currencyConverter>
</geoPlugin>
EOF
def setup
super
@ip = '74.125.237.209'
@success.provider = 'geo_plugin'
@base_url = 'http://www.geoplugin.net/xml.gp'
end
def assert_url(expected_url)
assert_equal expected_url, TestHelper.last_url.gsub(/&oauth_[a-z_]+=[a-zA-Z0-9\-. %]+/, '').gsub('%20', '+')
end
def test_geo_plugin_geocode
url = "#{@base_url}?ip=#{@ip}"
res = geocode(@ip, :geo_plugin_geocode)
assert_url url
assert_equal res.city, 'Mountain View'
assert_equal res.state, 'CA'
assert_equal res.country_code, 'US'
end
def test_successful_lookup
success = MockSuccess.new
success.expects(:body).returns(IP_SUCCESS)
url = "#{@base_url}?ip=200.150.38.66"
geocoder_class.expects(:call_geocoder_service).with(url).returns(success)
location = geocode('200.150.38.66')
assert_not_nil location
assert_equal(-19.916700, location.lat)
assert_equal(-43.933300, location.lng)
assert_equal 'Belo Horizonte', location.city
assert_equal 'Minas Gerais', location.state
assert_equal 'BR', location.country_code
assert_equal 'geo_plugin', location.provider
assert location.success?
end
def test_invalid_ip
location = geocode('pixrum')
assert_not_nil location
assert !location.success?
end
def test_service_unavailable
failure = MockFailure.new
url = "#{@base_url}?ip=69.10.10.10"
geocoder_class.expects(:call_geocoder_service).with(url).returns(failure)
location = geocode('69.10.10.10')
assert_not_nil location
assert !location.success?
end
end