From ba2d61dd8160237b2141ae24cf20db9b5301eb9d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 8 Aug 2008 02:31:12 -0700 Subject: Update tests for request memoization --- actionpack/lib/action_controller/request.rb | 25 ++++++++++++------------ actionpack/lib/action_controller/test_process.rb | 24 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 17 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 3c1521d8b1..90bced14e6 100644 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -252,18 +252,17 @@ EOM @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 @@ -276,7 +275,7 @@ EOM # 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 @@ -295,7 +294,7 @@ EOM # 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 @@ -333,16 +332,17 @@ EOM (%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 @@ -358,6 +358,7 @@ EOM 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. diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 42e79fe819..636e1c38b3 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -68,6 +68,8 @@ module ActionController #:nodoc: @env["REQUEST_URI"] = value @request_uri = nil @path = nil + request_uri(true) + path(true) end def request_uri=(uri) @@ -77,17 +79,26 @@ module ActionController #:nodoc: 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 @@ -440,10 +451,13 @@ module ActionController #:nodoc: 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 ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type): # # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png') -- cgit v1.2.3