Permalink
Browse files

Update tests for request memoization

  • Loading branch information...
jeremy committed Aug 8, 2008
1 parent c24a7cd commit ba2d61dd8160237b2141ae24cf20db9b5301eb9d
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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.
@@ -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)
@@ -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
@@ -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')
@@ -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")
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit ba2d61d

Please sign in to comment.