aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rwxr-xr-xactionpack/lib/action_controller/request.rb13
-rw-r--r--actionpack/test/controller/request_test.rb13
3 files changed, 21 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 39bfc0fb06..5602d315f6 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added GET-masquarading for HEAD, so request.method will return :get even for HEADs. This will help anyone relying on case request.method to automatically work with HEAD and map.resources will also allow HEADs to all GET actions. Rails automatically throws away the response content in a reply to HEAD, so you don't even need to worry about that. If you, for whatever reason, still need to distinguish between GET and HEAD in some edge case, you can use Request#head? and even Request.headers["REQUEST_METHOD"] for get the "real" answer. Closes #6694 [DHH]
+
* Update Routing to complain when :controller is not specified by a route. Closes #6669. [Nicholas Seckar]
* Ensure render_to_string cleans up after itself when an exception is raised. #6658 [rsanheim]
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index 505fe0777f..98d28d24b8 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -13,14 +13,18 @@ module ActionController
@parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access
end
- # Returns the HTTP request method as a lowercase symbol (:get, for example)
+ # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get
+ # since the two are supposedly to be functionaly equivilent for all purposes except that HEAD won't return a response
+ # body (which Rails also takes care of elsewhere).
def method
@request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ?
parameters[:_method].to_s.downcase.to_sym :
@env['REQUEST_METHOD'].downcase.to_sym
+
+ @request_method == :head ? :get : @request_method
end
- # Is this a GET request? Equivalent to request.method == :get
+ # Is this a GET (or HEAD) request? Equivalent to request.method == :get
def get?
method == :get
end
@@ -40,9 +44,10 @@ module ActionController
method == :delete
end
- # Is this a HEAD request? Equivalent to request.method == :head
+ # Is this a HEAD request? HEAD is mapped as :get for request.method, so here we ask the
+ # REQUEST_METHOD header directly. Thus, for head, both get? and head? will return true.
def head?
- method == :head
+ @env['REQUEST_METHOD'].downcase.to_sym == :head
end
# Determine whether the body of a HTTP call is URL-encoded (default)
diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
index 11da23561b..65e88b74d8 100644
--- a/actionpack/test/controller/request_test.rb
+++ b/actionpack/test/controller/request_test.rb
@@ -274,7 +274,7 @@ class RequestTest < Test::Unit::TestCase
end
def test_symbolized_request_methods
- [:head, :get, :post, :put, :delete].each do |method|
+ [:get, :post, :put, :delete].each do |method|
set_request_method_to method
assert_equal method, @request.method
end
@@ -282,7 +282,7 @@ class RequestTest < Test::Unit::TestCase
def test_allow_method_hacking_on_post
set_request_method_to :post
- [:head, :get, :put, :delete].each do |method|
+ [:get, :put, :delete].each do |method|
@request.instance_eval { @parameters = { :_method => method } ; @request_method = nil }
assert_equal method, @request.method
end
@@ -290,11 +290,18 @@ class RequestTest < Test::Unit::TestCase
def test_restrict_method_hacking
@request.instance_eval { @parameters = { :_method => 'put' } }
- [:head, :get, :put, :delete].each do |method|
+ [:get, :put, :delete].each do |method|
set_request_method_to method
assert_equal method, @request.method
end
end
+
+ def test_head_masquarading_as_get
+ set_request_method_to :head
+ assert_equal :get, @request.method
+ assert @request.get?
+ assert @request.head?
+ end
protected
def set_request_method_to(method)