2004-12-18 17:19:15 +00:00
|
|
|
require File.dirname(__FILE__) + '/../abstract_unit'
|
|
|
|
|
|
|
|
class RequestTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
end
|
|
|
|
|
2005-05-19 18:24:52 +00:00
|
|
|
def test_remote_ip
|
2005-06-14 10:31:31 +00:00
|
|
|
assert_equal '0.0.0.0', @request.remote_ip
|
2005-05-19 18:24:52 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2004-12-18 17:19:15 +00:00
|
|
|
def test_domains
|
|
|
|
@request.host = "www.rubyonrails.org"
|
|
|
|
assert_equal "rubyonrails.org", @request.domain
|
|
|
|
|
|
|
|
@request.host = "www.rubyonrails.co.uk"
|
|
|
|
assert_equal "rubyonrails.co.uk", @request.domain(2)
|
2005-09-02 09:10:31 +00:00
|
|
|
|
|
|
|
@request.host = "192.168.1.200"
|
|
|
|
assert_nil @request.domain
|
2005-09-20 06:44:53 +00:00
|
|
|
|
|
|
|
@request.host = nil
|
|
|
|
assert_nil @request.domain
|
2004-12-18 17:19:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_subdomains
|
|
|
|
@request.host = "www.rubyonrails.org"
|
|
|
|
assert_equal %w( www ), @request.subdomains
|
|
|
|
|
|
|
|
@request.host = "www.rubyonrails.co.uk"
|
|
|
|
assert_equal %w( www ), @request.subdomains(2)
|
|
|
|
|
|
|
|
@request.host = "dev.www.rubyonrails.co.uk"
|
|
|
|
assert_equal %w( dev www ), @request.subdomains(2)
|
2005-07-12 16:16:13 +00:00
|
|
|
|
|
|
|
@request.host = "foobar.foobar.com"
|
|
|
|
assert_equal %w( foobar ), @request.subdomains
|
2005-09-20 06:44:53 +00:00
|
|
|
|
|
|
|
@request.host = nil
|
|
|
|
assert_equal [], @request.subdomains
|
2004-12-18 17:19:15 +00:00
|
|
|
end
|
2004-12-18 18:01:28 +00:00
|
|
|
|
|
|
|
def test_port_string
|
|
|
|
@request.port = 80
|
|
|
|
assert_equal "", @request.port_string
|
|
|
|
|
|
|
|
@request.port = 8080
|
|
|
|
assert_equal ":8080", @request.port_string
|
|
|
|
end
|
2005-02-15 01:45:35 +00:00
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def test_relative_url_root
|
2005-09-15 05:32:16 +00:00
|
|
|
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
2005-06-18 05:17:18 +00:00
|
|
|
@request.env['SERVER_SOFTWARE'] = 'lighttpd/1.2.3'
|
2005-09-15 05:32:16 +00:00
|
|
|
assert_equal '', @request.relative_url_root, "relative_url_root should be disabled on lighttpd"
|
2005-06-18 05:17:18 +00:00
|
|
|
|
|
|
|
@request.env['SERVER_SOFTWARE'] = 'apache/1.2.3 some random text'
|
|
|
|
|
2005-03-20 22:02:22 +00:00
|
|
|
@request.env['SCRIPT_NAME'] = nil
|
|
|
|
assert_equal "", @request.relative_url_root
|
|
|
|
|
|
|
|
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
|
|
|
assert_equal "", @request.relative_url_root
|
|
|
|
|
|
|
|
@request.env['SCRIPT_NAME'] = "/myapp.rb"
|
|
|
|
assert_equal "", @request.relative_url_root
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-03-20 22:02:22 +00:00
|
|
|
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/hieraki", @request.relative_url_root
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-03-20 22:02:22 +00:00
|
|
|
@request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/collaboration/hieraki", @request.relative_url_root
|
2005-12-08 04:40:24 +00:00
|
|
|
|
|
|
|
# apache/scgi case
|
|
|
|
@request.relative_url_root = nil
|
|
|
|
@request.env['SCRIPT_NAME'] = "/collaboration/hieraki"
|
|
|
|
assert_equal "/collaboration/hieraki", @request.relative_url_root
|
2006-03-18 16:45:40 +00:00
|
|
|
|
|
|
|
@request.relative_url_root = nil
|
|
|
|
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
|
|
|
@request.env['SERVER_SOFTWARE'] = 'lighttpd/1.2.3'
|
|
|
|
@request.env['RAILS_RELATIVE_URL_ROOT'] = "/hieraki"
|
|
|
|
assert_equal "/hieraki", @request.relative_url_root
|
|
|
|
|
|
|
|
# @env overrides path guess
|
|
|
|
@request.relative_url_root = nil
|
|
|
|
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
|
|
|
@request.env['SERVER_SOFTWARE'] = 'apache/1.2.3 some random text'
|
|
|
|
@request.env['RAILS_RELATIVE_URL_ROOT'] = "/real_url"
|
|
|
|
assert_equal "/real_url", @request.relative_url_root
|
2005-03-20 22:02:22 +00:00
|
|
|
end
|
|
|
|
|
2005-02-15 01:45:35 +00:00
|
|
|
def test_request_uri
|
2005-06-18 05:17:18 +00:00
|
|
|
@request.env['SERVER_SOFTWARE'] = 'Apache 42.342.3432'
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-02-15 01:45:35 +00:00
|
|
|
@request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
|
|
|
|
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-02-15 01:45:35 +00:00
|
|
|
@request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"
|
|
|
|
assert_equal "/path/of/some/uri", @request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-02-15 01:45:35 +00:00
|
|
|
@request.set_REQUEST_URI "/path/of/some/uri"
|
|
|
|
assert_equal "/path/of/some/uri", @request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-02-15 01:45:35 +00:00
|
|
|
@request.set_REQUEST_URI "/"
|
|
|
|
assert_equal "/", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-02-15 01:45:35 +00:00
|
|
|
@request.set_REQUEST_URI "/?m=b"
|
|
|
|
assert_equal "/?m=b", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
2005-03-20 22:02:22 +00:00
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-03-20 22:02:22 +00:00
|
|
|
@request.set_REQUEST_URI "/"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
|
|
|
assert_equal "/", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-03-20 22:02:22 +00:00
|
|
|
@request.set_REQUEST_URI "/hieraki/"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/hieraki/", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-03-20 22:02:22 +00:00
|
|
|
@request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri
|
2005-04-18 15:43:07 +00:00
|
|
|
assert_equal "/books/edit/2", @request.path
|
|
|
|
|
|
|
|
# The following tests are for when REQUEST_URI is not supplied (as in IIS)
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.set_REQUEST_URI nil
|
|
|
|
@request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
|
|
|
|
@request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb"
|
|
|
|
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/path/dispatch.rb"
|
|
|
|
assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
|
|
|
|
assert_equal "/of/some/uri", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['PATH_INFO'] = "/path/of/some/uri"
|
|
|
|
@request.env['SCRIPT_NAME'] = nil
|
|
|
|
assert_equal "/path/of/some/uri", @request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['PATH_INFO'] = "/"
|
|
|
|
assert_equal "/", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['PATH_INFO'] = "/?m=b"
|
|
|
|
assert_equal "/?m=b", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['PATH_INFO'] = "/"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
|
|
|
assert_equal "/", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['PATH_INFO'] = "/hieraki/"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/hieraki/", @request.request_uri
|
|
|
|
assert_equal "/", @request.path
|
|
|
|
|
|
|
|
# This test ensures that Rails uses REQUEST_URI over PATH_INFO
|
2005-05-22 07:43:05 +00:00
|
|
|
@request.relative_url_root = nil
|
2005-04-18 15:43:07 +00:00
|
|
|
@request.env['REQUEST_URI'] = "/some/path"
|
|
|
|
@request.env['PATH_INFO'] = "/another/path"
|
|
|
|
@request.env['SCRIPT_NAME'] = "/dispatch.cgi"
|
|
|
|
assert_equal "/some/path", @request.request_uri
|
|
|
|
assert_equal "/some/path", @request.path
|
2005-02-15 01:45:35 +00:00
|
|
|
end
|
2005-03-20 22:02:22 +00:00
|
|
|
|
2004-12-18 18:01:28 +00:00
|
|
|
|
|
|
|
def test_host_with_port
|
|
|
|
@request.host = "rubyonrails.org"
|
|
|
|
@request.port = 80
|
|
|
|
assert_equal "rubyonrails.org", @request.host_with_port
|
|
|
|
|
|
|
|
@request.host = "rubyonrails.org"
|
|
|
|
@request.port = 81
|
|
|
|
assert_equal "rubyonrails.org:81", @request.host_with_port
|
|
|
|
end
|
2005-06-18 05:17:18 +00:00
|
|
|
|
|
|
|
def test_server_software
|
|
|
|
assert_equal nil, @request.server_software
|
|
|
|
|
|
|
|
@request.env['SERVER_SOFTWARE'] = 'Apache3.422'
|
|
|
|
assert_equal 'apache', @request.server_software
|
|
|
|
|
|
|
|
@request.env['SERVER_SOFTWARE'] = 'lighttpd(1.1.4)'
|
|
|
|
assert_equal 'lighttpd', @request.server_software
|
|
|
|
end
|
2005-06-21 07:02:30 +00:00
|
|
|
|
|
|
|
def test_xml_http_request
|
|
|
|
assert !@request.xml_http_request?
|
|
|
|
assert !@request.xhr?
|
|
|
|
|
|
|
|
@request.env['HTTP_X_REQUESTED_WITH'] = "DefinitelyNotAjax1.0"
|
|
|
|
assert !@request.xml_http_request?
|
|
|
|
assert !@request.xhr?
|
|
|
|
|
|
|
|
@request.env['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
|
|
|
|
assert @request.xml_http_request?
|
|
|
|
assert @request.xhr?
|
|
|
|
end
|
2006-01-23 05:32:59 +00:00
|
|
|
|
|
|
|
def test_reports_ssl
|
|
|
|
assert !@request.ssl?
|
|
|
|
@request.env['HTTPS'] = 'on'
|
|
|
|
assert @request.ssl?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reports_ssl_when_proxied_via_lighttpd
|
|
|
|
assert !@request.ssl?
|
|
|
|
@request.env['HTTP_X_FORWARDED_PROTO'] = 'https'
|
|
|
|
assert @request.ssl?
|
|
|
|
end
|
2005-06-21 07:02:30 +00:00
|
|
|
|
2005-02-27 17:11:05 +00:00
|
|
|
end
|