Skip to content
Snippets Groups Projects
Commit 6bd4b594 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #370 from robdimarco/master

Patch to fix issue #369.  Handles comma separated values for HTTP_X_FORWARDED_FOR header
parents e399ef99 4e1ae2b1
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ module Geocoder ...@@ -8,7 +8,7 @@ module Geocoder
if env.has_key?('HTTP_X_REAL_IP') if env.has_key?('HTTP_X_REAL_IP')
@location = Geocoder.search(env['HTTP_X_REAL_IP']).first @location = Geocoder.search(env['HTTP_X_REAL_IP']).first
elsif env.has_key?('HTTP_X_FORWARDED_FOR') elsif env.has_key?('HTTP_X_FORWARDED_FOR')
@location = Geocoder.search(env['HTTP_X_FORWARDED_FOR']).first @location = Geocoder.search(env['HTTP_X_FORWARDED_FOR'].split(/\s*,\s*/)[0]).first
else else
@location = Geocoder.search(ip).first @location = Geocoder.search(ip).first
end end
......
# encoding: utf-8
require 'test_helper'
class RequestTest < Test::Unit::TestCase
class MockRequest
include Geocoder::Request
attr_accessor :env, :ip
def initialize(env={}, ip="")
@env = env
@ip = ip
end
end
def test_http_x_real_ip
req = MockRequest.new({"HTTP_X_REAL_IP" => "74.200.247.59"})
assert req.location.is_a?(Geocoder::Result::Freegeoip)
end
def test_http_x_forwarded_for_without_proxy
req = MockRequest.new({"HTTP_X_FORWARDED_FOR" => "74.200.247.59"})
assert req.location.is_a?(Geocoder::Result::Freegeoip)
end
def test_http_x_forwarded_for_with_proxy
req = MockRequest.new({"HTTP_X_FORWARDED_FOR" => "74.200.247.59, 74.200.247.59"})
assert req.location.is_a?(Geocoder::Result::Freegeoip)
end
def test_with_request_ip
req = MockRequest.new({}, "74.200.247.59")
assert req.location.is_a?(Geocoder::Result::Freegeoip)
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment