2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class RequestTest < ActiveSupport::TestCase
|
2007-01-22 14:43:50 +01:00
|
|
|
def setup
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def teardown
|
|
|
|
ActionController::Base.relative_url_root = nil
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_remote_ip
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REMOTE_ADDR' => '1.2.3.4'
|
|
|
|
assert_equal '1.2.3.4', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REMOTE_ADDR' => '1.2.3.4,3.4.5.6'
|
|
|
|
assert_equal '1.2.3.4', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REMOTE_ADDR' => '1.2.3.4',
|
|
|
|
'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
|
|
|
|
assert_equal '1.2.3.4', request.remote_ip
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REMOTE_ADDR' => '127.0.0.1',
|
|
|
|
'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => 'unknown,3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '172.16.0.1,3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '192.168.0.1,3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '10.0.0.1,3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '10.0.0.1, 10.0.0.1, 3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '127.0.0.1,3.4.5.6'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => 'unknown,192.168.0.1'
|
|
|
|
assert_equal 'unknown', request.remote_ip
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4'
|
|
|
|
assert_equal '3.4.5.6', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
|
|
|
|
'HTTP_CLIENT_IP' => '2.2.2.2'
|
2009-03-16 15:55:30 +01:00
|
|
|
e = assert_raise(ActionController::ActionControllerError) {
|
2009-08-04 17:16:03 +02:00
|
|
|
request.remote_ip
|
2008-05-18 06:22:34 +02:00
|
|
|
}
|
|
|
|
assert_match /IP spoofing attack/, e.message
|
2009-08-04 17:16:03 +02:00
|
|
|
assert_match /HTTP_X_FORWARDED_FOR="1.1.1.1"/, e.message
|
|
|
|
assert_match /HTTP_CLIENT_IP="2.2.2.2"/, e.message
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
# turn IP Spoofing detection off.
|
|
|
|
# This is useful for sites that are aimed at non-IP clients. The typical
|
|
|
|
# example is WAP. Since the cellular network is not IP based, it's a
|
|
|
|
# leap of faith to assume that their proxies are ever going to set the
|
|
|
|
# HTTP_CLIENT_IP/HTTP_X_FORWARDED_FOR headers properly.
|
|
|
|
ActionController::Base.ip_spoofing_check = false
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
|
|
|
|
'HTTP_CLIENT_IP' => '2.2.2.2'
|
|
|
|
assert_equal '2.2.2.2', request.remote_ip
|
2009-02-04 21:26:08 +01:00
|
|
|
ActionController::Base.ip_spoofing_check = true
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_FORWARDED_FOR' => '8.8.8.8, 9.9.9.9'
|
|
|
|
assert_equal '9.9.9.9', request.remote_ip
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_domains
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => 'www.rubyonrails.org'
|
|
|
|
assert_equal "rubyonrails.org", request.domain
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
|
|
|
|
assert_equal "rubyonrails.co.uk", request.domain(2)
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "192.168.1.200"
|
|
|
|
assert_nil request.domain
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "foo.192.168.1.200"
|
|
|
|
assert_nil request.domain
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
|
|
|
|
assert_equal "200.com", request.domain
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_subdomains
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "www.rubyonrails.org"
|
|
|
|
assert_equal %w( www ), request.subdomains
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
|
|
|
|
assert_equal %w( www ), request.subdomains(2)
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk"
|
|
|
|
assert_equal %w( dev www ), request.subdomains(2)
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "foobar.foobar.com"
|
|
|
|
assert_equal %w( foobar ), request.subdomains
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "192.168.1.200"
|
|
|
|
assert_equal [], request.subdomains
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "foo.192.168.1.200"
|
|
|
|
assert_equal [], request.subdomains
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
|
|
|
|
assert_equal %w( 192 168 1 ), request.subdomains
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => nil
|
|
|
|
assert_equal [], request.subdomains
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_port_string
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => 'www.example.org:80'
|
|
|
|
assert_equal "", request.port_string
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => 'www.example.org:8080'
|
|
|
|
assert_equal ":8080", request.port_string
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_request_uri
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request '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
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request '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
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/path/of/some/uri"
|
|
|
|
assert_equal "/path/of/some/uri", request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/"
|
|
|
|
assert_equal "/", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/?m=b"
|
|
|
|
assert_equal "/?m=b", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/", 'SCRIPT_NAME' => '/dispatch.cgi'
|
|
|
|
assert_equal "/", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = "/hieraki"
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/hieraki/", 'SCRIPT_NAME' => "/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/hieraki/", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = "/collaboration/hieraki"
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/collaboration/hieraki/books/edit/2",
|
|
|
|
'SCRIPT_NAME' => "/collaboration/hieraki/dispatch.cgi"
|
|
|
|
assert_equal "/collaboration/hieraki/books/edit/2", request.request_uri
|
|
|
|
assert_equal "/books/edit/2", request.path
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# The following tests are for when REQUEST_URI is not supplied (as in IIS)
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
|
|
|
|
'SCRIPT_NAME' => nil,
|
|
|
|
'REQUEST_URI' => nil
|
|
|
|
assert_equal "/path/of/some/uri?mapped=1", request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = '/path'
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
|
|
|
|
'SCRIPT_NAME' => "/path/dispatch.rb",
|
|
|
|
'REQUEST_URI' => nil
|
|
|
|
assert_equal "/path/of/some/uri?mapped=1", request.request_uri
|
|
|
|
assert_equal "/of/some/uri", request.path
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => "/path/of/some/uri",
|
|
|
|
'SCRIPT_NAME' => nil,
|
|
|
|
'REQUEST_URI' => nil
|
|
|
|
assert_equal "/path/of/some/uri", request.request_uri
|
|
|
|
assert_equal "/path/of/some/uri", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => '/', 'REQUEST_URI' => nil
|
|
|
|
assert_equal "/", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => '/?m=b', 'REQUEST_URI' => nil
|
|
|
|
assert_equal "/?m=b", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => "/",
|
|
|
|
'SCRIPT_NAME' => "/dispatch.cgi",
|
|
|
|
'REQUEST_URI' => nil
|
|
|
|
assert_equal "/", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = '/hieraki'
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'PATH_INFO' => "/hieraki/",
|
|
|
|
'SCRIPT_NAME' => "/hieraki/dispatch.cgi",
|
|
|
|
'REQUEST_URI' => nil
|
|
|
|
assert_equal "/hieraki/", request.request_uri
|
|
|
|
assert_equal "/", request.path
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = '/hieraki'
|
2009-08-04 17:16:03 +02:00
|
|
|
assert_equal "/dispatch.cgi", request.path
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = '/foo'
|
2009-08-04 17:16:03 +02:00
|
|
|
assert_equal "/hieraki/dispatch.cgi", request.path
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
# This test ensures that Rails uses REQUEST_URI over PATH_INFO
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.relative_url_root = nil
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_URI' => "/some/path",
|
|
|
|
'PATH_INFO' => "/another/path",
|
|
|
|
'SCRIPT_NAME' => "/dispatch.cgi"
|
|
|
|
assert_equal "/some/path", request.request_uri
|
|
|
|
assert_equal "/some/path", request.path
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_host_with_default_port
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => 'rubyonrails.org:80'
|
|
|
|
assert_equal "rubyonrails.org", request.host_with_port
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_host_with_non_default_port
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_HOST' => 'rubyonrails.org:81'
|
|
|
|
assert_equal "rubyonrails.org:81", request.host_with_port
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_server_software
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
assert_equal nil, request.server_software
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'SERVER_SOFTWARE' => 'Apache3.422'
|
|
|
|
assert_equal 'apache', request.server_software
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'SERVER_SOFTWARE' => 'lighttpd(1.1.4)'
|
|
|
|
assert_equal 'lighttpd', request.server_software
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_xml_http_request
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
|
|
|
|
assert !request.xml_http_request?
|
|
|
|
assert !request.xhr?
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_REQUESTED_WITH' => 'DefinitelyNotAjax1.0'
|
|
|
|
assert !request.xml_http_request?
|
|
|
|
assert !request.xhr?
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
|
|
|
|
assert request.xml_http_request?
|
|
|
|
assert request.xhr?
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reports_ssl
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
assert !request.ssl?
|
|
|
|
|
|
|
|
request = stub_request 'HTTPS' => 'on'
|
|
|
|
assert request.ssl?
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reports_ssl_when_proxied_via_lighttpd
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
assert !request.ssl?
|
|
|
|
|
|
|
|
request = stub_request 'HTTP_X_FORWARDED_PROTO' => 'https'
|
|
|
|
assert request.ssl?
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-02-09 09:04:31 +01:00
|
|
|
|
|
|
|
def test_symbolized_request_methods
|
|
|
|
[:get, :post, :put, :delete].each do |method|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_METHOD' => method.to_s.upcase
|
|
|
|
assert_equal method, request.method
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_invalid_http_method_raises_exception
|
2009-03-16 15:55:30 +01:00
|
|
|
assert_raise(ActionController::UnknownHttpMethod) do
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_METHOD' => 'RANDOM_METHOD'
|
|
|
|
request.request_method
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-09 09:04:31 +01:00
|
|
|
def test_allow_method_hacking_on_post
|
2007-12-21 08:48:59 +01:00
|
|
|
[:get, :head, :options, :put, :post, :delete].each do |method|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_METHOD' => method.to_s.upcase
|
|
|
|
assert_equal(method == :head ? :get : method, request.method)
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_restrict_method_hacking
|
|
|
|
[:get, :put, :delete].each do |method|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_METHOD' => method.to_s.upcase,
|
|
|
|
'action_controller.request.request_parameters' => { :_method => 'put' }
|
|
|
|
assert_equal method, request.method
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def test_head_masquerading_as_get
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'REQUEST_METHOD' => 'HEAD'
|
|
|
|
assert_equal :get, request.method
|
|
|
|
assert request.get?
|
|
|
|
assert request.head?
|
2007-02-09 09:04:31 +01:00
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_xml_format
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
request.expects(:parameters).at_least_once.returns({ :format => 'xml' })
|
|
|
|
assert_equal Mime::XML, request.format
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_xhtml_format
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
request.expects(:parameters).at_least_once.returns({ :format => 'xhtml' })
|
|
|
|
assert_equal Mime::HTML, request.format
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_txt_format
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
request.expects(:parameters).at_least_once.returns({ :format => 'txt' })
|
|
|
|
assert_equal Mime::TEXT, request.format
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
def test_xml_http_request
|
2008-10-27 07:47:01 +01:00
|
|
|
ActionController::Base.use_accept_header, old =
|
|
|
|
false, ActionController::Base.use_accept_header
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
|
|
|
|
request.expects(:parameters).at_least_once.returns({})
|
|
|
|
assert request.xhr?
|
|
|
|
assert_equal Mime::JS, request.format
|
2008-10-27 07:47:01 +01:00
|
|
|
ensure
|
|
|
|
ActionController::Base.use_accept_header = old
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_type
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'CONTENT_TYPE' => 'text/html'
|
|
|
|
assert_equal Mime::HTML, request.content_type
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
def test_can_override_format_with_parameter
|
|
|
|
request = stub_request
|
|
|
|
request.expects(:parameters).at_least_once.returns({ :format => :txt })
|
|
|
|
assert !request.format.xml?
|
|
|
|
|
|
|
|
request = stub_request
|
|
|
|
request.expects(:parameters).at_least_once.returns({ :format => :xml })
|
|
|
|
assert request.format.xml?
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_content_no_type
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
assert_equal nil, request.content_type
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_type_xml
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'CONTENT_TYPE' => 'application/xml'
|
|
|
|
assert_equal Mime::XML, request.content_type
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_type_with_charset
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
|
|
|
|
assert_equal Mime::XML, request.content_type
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_user_agent
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request 'HTTP_USER_AGENT' => 'TestAgent'
|
|
|
|
assert_equal 'TestAgent', request.user_agent
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_parameters
|
2009-08-04 17:16:03 +02:00
|
|
|
request = stub_request
|
|
|
|
request.stubs(:request_parameters).returns({ "foo" => 1 })
|
|
|
|
request.stubs(:query_parameters).returns({ "bar" => 2 })
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
assert_equal({"foo" => 1, "bar" => 2}, request.parameters)
|
|
|
|
assert_equal({"foo" => 1}, request.request_parameters)
|
|
|
|
assert_equal({"bar" => 2}, request.query_parameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def stub_request(env={})
|
|
|
|
ActionController::Request.new(env)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|