Skip to content

Commit

Permalink
Update tests for request memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 8, 2008
1 parent c24a7cd commit ba2d61d
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 121 deletions.
25 changes: 13 additions & 12 deletions actionpack/lib/action_controller/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,17 @@ def ssl?
@env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https'
end

def host_with_port_without_standard_port_handling
def raw_host_with_port
if forwarded = env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
else
env['HTTP_HOST'] || env['SERVER_NAME'] || "#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
end
end
memoize :host_with_port_without_standard_port_handling

# Returns the host for this request, such as example.com.
def host
host_with_port_without_standard_port_handling.sub(/:\d+\Z/, '')
raw_host_with_port.sub(/:\d+$/, '')
end
memoize :host

Expand All @@ -276,7 +275,7 @@ def host_with_port

# Returns the port number of this request as an integer.
def port
if host_with_port_without_standard_port_handling =~ /:(\d+)$/
if raw_host_with_port =~ /:(\d+)$/
$1.to_i
else
standard_port
Expand All @@ -295,7 +294,7 @@ def standard_port
# Returns a port suffix like ":8080" if the port number of this request
# is not the default HTTP port 80 or HTTPS port 443.
def port_string
":#{port}" unless port == standard_port
port == standard_port ? '' : ":#{port}"
end

# Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify
Expand Down Expand Up @@ -333,16 +332,17 @@ def request_uri
(%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
else
# Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
uri = @env['PATH_INFO']
uri = uri.sub(/#{script_filename}\//, '') unless script_filename.nil?
unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty?
uri << '?' << env_qs
uri = @env['PATH_INFO'].to_s

if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
uri = uri.sub(/#{script_filename}\//, '')
end

if uri.nil?
env_qs = @env['QUERY_STRING'].to_s
uri += "?#{env_qs}" unless env_qs.empty?

if uri.blank?
@env.delete('REQUEST_URI')
uri
else
@env['REQUEST_URI'] = uri
end
Expand All @@ -358,6 +358,7 @@ def path
path.sub!(%r/^#{ActionController::Base.relative_url_root}/, '')
path || ''
end
memoize :path

# Read the request body. This is useful for web services that need to
# work with raw requests directly.
Expand Down
24 changes: 19 additions & 5 deletions actionpack/lib/action_controller/test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def set_REQUEST_URI(value)
@env["REQUEST_URI"] = value
@request_uri = nil
@path = nil
request_uri(true)
path(true)
end

def request_uri=(uri)
Expand All @@ -77,17 +79,26 @@ def request_uri=(uri)

def accept=(mime_types)
@env["HTTP_ACCEPT"] = Array(mime_types).collect { |mime_types| mime_types.to_s }.join(",")
accepts(true)
end

def if_modified_since=(last_modified)
@env["HTTP_IF_MODIFIED_SINCE"] = last_modified
end

def if_none_match=(etag)
@env["HTTP_IF_NONE_MATCH"] = etag
end

def remote_addr=(addr)
@env['REMOTE_ADDR'] = addr
end

def request_uri
def request_uri(*args)
@request_uri || super
end

def path
def path(*args)
@path || super
end

Expand Down Expand Up @@ -440,10 +451,13 @@ def find_all_tag(conditions)
end

def method_missing(selector, *args)
return @controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
return super
if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
@controller.send(selector, *args)
else
super
end
end

# Shortcut for <tt>ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type)</tt>:
#
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_should_cache_with_trailing_slash_on_url

uses_mocha("should_cache_ok_at_custom_path") do
def test_should_cache_ok_at_custom_path
@request.expects(:path).returns("/index.html")
@request.stubs(:path).returns("/index.html")
get :ok
assert_response :ok
assert File.exist?("#{FILE_STORE_PATH}/index.html")
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/cgi_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_http_host
assert_equal "rubyonrails.org:8080", @request.host_with_port

@request_hash['HTTP_X_FORWARDED_HOST'] = "www.firsthost.org, www.secondhost.org"
assert_equal "www.secondhost.org", @request.host
assert_equal "www.secondhost.org", @request.host(true)
end

def test_http_host_with_default_port_overrides_server_port
Expand Down
8 changes: 4 additions & 4 deletions actionpack/test/controller/content_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,23 @@ def teardown


def test_render_default_content_types_for_respond_to
@request.env["HTTP_ACCEPT"] = Mime::HTML.to_s
@request.accept = Mime::HTML.to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::HTML, @response.content_type

@request.env["HTTP_ACCEPT"] = Mime::JS.to_s
@request.accept = Mime::JS.to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::JS, @response.content_type
end

def test_render_default_content_types_for_respond_to_with_template
@request.env["HTTP_ACCEPT"] = Mime::XML.to_s
@request.accept = Mime::XML.to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::XML, @response.content_type
end

def test_render_default_content_types_for_respond_to_with_overwrite
@request.env["HTTP_ACCEPT"] = Mime::RSS.to_s
@request.accept = Mime::RSS.to_s
get :render_default_content_types_for_respond_to
assert_equal Mime::XML, @response.content_type
end
Expand Down

0 comments on commit ba2d61d

Please sign in to comment.