1 require File.join(File.dirname(__FILE__), '../../../../config/environment')
2 require 'action_controller/test_process'
8 class LocationAwareController < ActionController::Base #:nodoc: all
12 render :nothing => true
16 class ActionController::TestRequest #:nodoc: all
17 attr_accessor :remote_ip
20 # Re-raise errors caught by the controller.
21 class LocationAwareController #:nodoc: all
22 def rescue_action(e) raise e end;
25 class IpGeocodeLookupTest < Test::Unit::TestCase #:nodoc: all
28 @success = GeoKit::GeoLoc.new
29 @success.provider = "hostip"
30 @success.lat = 41.7696
31 @success.lng = -88.4588
32 @success.city = "Sugar Grove"
34 @success.country_code = "US"
35 @success.success = true
37 @failure = GeoKit::GeoLoc.new
38 @failure.provider = "hostip"
39 @failure.city = "(Private Address)"
40 @failure.success = false
42 @controller = LocationAwareController.new
43 @request = ActionController::TestRequest.new
44 @response = ActionController::TestResponse.new
47 def test_no_location_in_cookie_or_session
48 GeoKit::Geocoders::IpGeocoder.expects(:geocode).with("good ip").returns(@success)
49 @request.remote_ip = "good ip"
54 def test_location_in_cookie
55 @request.remote_ip = "good ip"
56 @request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
61 def test_location_in_session
62 @request.remote_ip = "good ip"
63 @request.session[:geo_location] = @success
64 @request.cookies['geo_location'] = CGI::Cookie.new('geo_location', @success.to_yaml)
69 def test_ip_not_located
70 GeoKit::Geocoders::IpGeocoder.expects(:geocode).with("bad ip").returns(@failure)
71 @request.remote_ip = "bad ip"
73 assert_nil @request.session[:geo_location]
79 assert_response :success
80 assert_equal @success, @request.session[:geo_location]
81 assert_not_nil cookies['geo_location']
82 assert_equal @success, YAML.load(cookies['geo_location'].join)