diff options
Diffstat (limited to 'actionpack')
-rwxr-xr-x | actionpack/lib/action_controller/base.rb | 26 | ||||
-rw-r--r-- | actionpack/test/controller/new_render_test.rb | 13 |
2 files changed, 33 insertions, 6 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index bd2256f44d..94e2d6a25f 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -837,15 +837,31 @@ module ActionController #:nodoc: # This allows you to easily return a response that consists only of # significant headers: # - # head :status => :created, :location => person_path(@person) + # head :created, :location => person_path(@person) # # It can also be used to return exceptional conditions: # - # return head(:status => :method_not_allowed) unless request.post? - # return head(:status => :bad_request) unless valid_request? + # return head(:method_not_allowed) unless request.post? + # return head(:bad_request) unless valid_request? # render - def head(options = {}) - status = interpret_status(options.delete(:status) || :ok) + def head(*args) + if args.length > 2 + raise ArgumentError, "too many arguments to head" + elsif args.empty? + raise ArgumentError, "too few arguments to head" + elsif args.length == 2 + status = args.shift + options = args.shift + elsif args.first.is_a?(Hash) + options = args.first + else + status = args.first + options = {} + end + + raise ArgumentError, "head requires an options hash" if !options.is_a?(Hash) + + status = interpret_status(status || options.delete(:status) || :ok) options.each do |key, value| headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 5463df3c60..b2b40b7636 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -222,6 +222,10 @@ class NewRenderTestController < ActionController::Base head :x_custom_header => "something" end + def head_with_status_code_first + head :forbidden, :x_custom_header => "something" + end + helper NewRenderTestHelper helper do def rjs_helper_method(value) @@ -656,9 +660,16 @@ EOS end end - def head_with_string_status + def test_head_with_string_status get :head_with_string_status, :status => "404 Eat Dirt" assert_equal 404, @response.response_code assert_equal "Eat Dirt", @response.message end + + def test_head_with_status_code_first + get :head_with_status_code_first + assert_equal 403, @response.response_code + assert_equal "Forbidden", @response.message + assert_equal "something", @response.headers["X-Custom-Header"] + end end |