Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

modify head so that you can specify a bare status code as the first p…

…arameter

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5200 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit c0eccc9aef21c3eac9d89d3af93b82ce906b04dd 1 parent b2ede64
@jamis jamis authored
View
26 actionpack/lib/action_controller/base.rb
@@ -837,15 +837,31 @@ def render_without_layout(template_name = default_template_name, status = nil) #
# 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
View
13 actionpack/test/controller/new_render_test.rb
@@ -222,6 +222,10 @@ def head_with_custom_header
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 @@ def test_head_with_integer_status
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
Please sign in to comment.
Something went wrong with that request. Please try again.