Skip to content

Commit

Permalink
Fixed Request#remote_ip in testing #1251 [bitsweat]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1322 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed May 19, 2005
1 parent 473e5bd commit dd8c92e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
8 changes: 6 additions & 2 deletions actionpack/lib/action_controller/test_process.rb
Expand Up @@ -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 || {}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions actionpack/test/controller/request_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit dd8c92e

Please sign in to comment.