Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed Request#remote_ip in testing #1251 [bitsweat]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1322 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit dd8c92ea5be3faa9436363d8a37d5debec2027c4 1 parent 473e5bd
@dhh dhh authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed Request#remote_ip in testing #1251 [bitsweat]
+
* Fixed that compute_public_path should recognize external URLs, so image_tag("http://www.example.com/images/icon.gif") is not prefixed with the relative url path #1254 [victor-ronr-trac@carotena.net]
* Added support for descending year values in DateHelper#select_year, like select_year(Date.today, :start_year => 2005, :end_year => 1900), which would count down from 2005 to 1900 instead of the other way #1274 [nwoods@mail.com]
View
8 actionpack/lib/action_controller/test_process.rb
@@ -32,7 +32,7 @@ def process_test(request) #:nodoc:
class TestRequest < AbstractRequest #:nodoc:
attr_accessor :cookies
attr_accessor :query_parameters, :request_parameters, :path, :session, :env
- attr_accessor :host, :remote_addr
+ attr_accessor :host
def initialize(query_parameters = nil, request_parameters = nil, session = nil)
@query_parameters = query_parameters || {}
@@ -71,6 +71,10 @@ def request_uri=(uri)
@path = uri.split("?").first
end
+ def remote_addr=(addr)
+ @env['REMOTE_ADDR'] = addr
+ end
+
def request_uri
@request_uri || super()
end
@@ -88,7 +92,7 @@ def initialize_containers
def initialize_default_values
@host = "test.host"
@request_uri = "/"
- @remote_addr, @remote_ip = "127.0.0.1"
+ self.remote_addr = "127.0.0.1"
@env["SERVER_PORT"] = 80
end
end
View
33 actionpack/test/controller/request_test.rb
@@ -5,6 +5,39 @@ def setup
@request = ActionController::TestRequest.new
end
+ def test_remote_ip
+ assert_equal '127.0.0.1', @request.remote_ip
+
+ @request.remote_addr = '1.2.3.4'
+ assert_equal '1.2.3.4', @request.remote_ip
+
+ @request.env['HTTP_CLIENT_IP'] = '2.3.4.5'
+ assert_equal '2.3.4.5', @request.remote_ip
+ @request.env.delete 'HTTP_CLIENT_IP'
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = '3.4.5.6'
+ assert_equal '3.4.5.6', @request.remote_ip
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,3.4.5.6'
+ assert_equal '3.4.5.6', @request.remote_ip
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = '172.16.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', @request.remote_ip
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = '192.168.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', @request.remote_ip
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = '10.0.0.1,3.4.5.6'
+ assert_equal '3.4.5.6', @request.remote_ip
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,3.4.5.6'
+ assert_equal '127.0.0.1', @request.remote_ip
+
+ @request.env['HTTP_X_FORWARDED_FOR'] = 'unknown,192.168.0.1'
+ assert_equal '1.2.3.4', @request.remote_ip
+ @request.env.delete 'HTTP_X_FORWARDED_FOR'
+ end
+
def test_domains
@request.host = "www.rubyonrails.org"
assert_equal "rubyonrails.org", @request.domain
Please sign in to comment.
Something went wrong with that request. Please try again.