]> projects.mako.cc - selectricity-live/blob - vendor/plugins/geokit/test/ipgeocoder_test.rb
Close tables
[selectricity-live] / vendor / plugins / geokit / test / ipgeocoder_test.rb
1 require File.join(File.dirname(__FILE__), 'base_geocoder_test')
2
3 class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
4     
5   IP_FAILURE=<<-EOF
6     Country: (Private Address) (XX)
7     City: (Private Address)
8     Latitude: 
9     Longitude:
10     EOF
11     
12   IP_SUCCESS=<<-EOF
13     Country: UNITED STATES (US)
14     City: Sugar Grove, IL
15     Latitude: 41.7696
16     Longitude: -88.4588
17     EOF
18     
19   IP_UNICODED=<<-EOF
20     Country: SWEDEN (SE)
21     City: Borås
22     Latitude: 57.7167
23     Longitude: 12.9167
24     EOF
25     
26     def setup
27       super
28       @success.provider = "hostip"
29     end    
30   
31   def test_successful_lookup
32     success = MockSuccess.new
33     success.expects(:body).returns(IP_SUCCESS)
34     url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
35     GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
36     location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
37     assert_not_nil location
38     assert_equal 41.7696, location.lat
39     assert_equal -88.4588, location.lng
40     assert_equal "Sugar Grove", location.city
41     assert_equal "IL", location.state
42     assert_equal "US", location.country_code
43     assert_equal "hostip", location.provider
44     assert location.success
45   end
46   
47   def test_unicoded_lookup
48     success = MockSuccess.new
49     success.expects(:body).returns(IP_UNICODED)
50     url = 'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true'
51     GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(success)
52     location = GeoKit::Geocoders::IpGeocoder.geocode('12.215.42.19')
53     assert_not_nil location
54     assert_equal 57.7167, location.lat
55     assert_equal 12.9167, location.lng
56     assert_equal "Borås", location.city
57     assert_nil location.state
58     assert_equal "SE", location.country_code
59     assert_equal "hostip", location.provider
60     assert location.success
61   end
62   
63   def test_failed_lookup
64     failure = MockSuccess.new
65     failure.expects(:body).returns(IP_FAILURE)
66     url = 'http://api.hostip.info/get_html.php?ip=0.0.0.0&position=true'
67     GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
68     location = GeoKit::Geocoders::IpGeocoder.geocode("0.0.0.0")
69     assert_not_nil location
70     assert !location.success
71   end
72   
73   def test_invalid_ip
74     location = GeoKit::Geocoders::IpGeocoder.geocode("blah")
75     assert_not_nil location
76     assert !location.success
77   end
78   
79   def test_service_unavailable
80     failure = MockFailure.new
81     url = 'http://api.hostip.info/get_html.php?ip=0.0.0.0&position=true'
82     GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
83     location = GeoKit::Geocoders::IpGeocoder.geocode("0.0.0.0")
84     assert_not_nil location
85     assert !location.success
86   end  
87 end

Benjamin Mako Hill || Want to submit a patch?