andre / geokit-gem

Official Geokit Gem. Geokit gem provides geocoding and distance/heading calculations. Pair with the geokit-rails plugin for full-fledged location-based app functionality.

This URL has Read+Write access

geokit-gem / test / test_ipgeocoder.rb
100644 89 lines (78 sloc) 2.85 kb
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
# encoding: utf-8
require File.join(File.dirname(__FILE__), 'test_base_geocoder')
 
class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
    
  IP_FAILURE=<<-EOF
Country: (Private Address) (XX)
City: (Private Address)
Latitude:
Longitude:
EOF
    
  IP_SUCCESS=<<-EOF
Country: UNITED STATES (US)
City: Sugar Grove, IL
Latitude: 41.7696
Longitude: -88.4588
EOF
    
  IP_UNICODED=<<-EOF
Country: SWEDEN (SE)
City: BorĂ¥s
Latitude: 57.7167
Longitude: 12.9167
EOF
    
    def setup
      super
      @success.provider = "hostip"
    end
  
  def test_successful_lookup
    success = MockSuccess.new
    success.expects(:body).returns(IP_SUCCESS)
    url = 'http://api.hostip.info/get_html.php?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 "hostip", location.provider
    assert location.success?
  end
  
  def test_unicoded_lookup
    success = MockSuccess.new
    success.expects(:body).returns(IP_UNICODED)
    url = 'http://api.hostip.info/get_html.php?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 "hostip", location.provider
    assert location.success?
  end
  
  def test_failed_lookup
    failure = MockSuccess.new
    failure.expects(:body).returns(IP_FAILURE)
    url = 'http://api.hostip.info/get_html.php?ip=10.10.10.10&position=true'
    GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
    location = GeoKit::Geocoders::IpGeocoder.geocode("10.10.10.10")
    assert_not_nil location
    assert !location.success?
  end
  
  def test_invalid_ip
    location = GeoKit::Geocoders::IpGeocoder.geocode("blah")
    assert_not_nil location
    assert !location.success?
  end
  
  def test_service_unavailable
    failure = MockFailure.new
    url = 'http://api.hostip.info/get_html.php?ip=10.10.10.10&position=true'
    GeoKit::Geocoders::IpGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
    location = GeoKit::Geocoders::IpGeocoder.geocode("10.10.10.10")
    assert_not_nil location
    assert !location.success?
  end
end