Permalink
Browse files

Ensure that the proper accept header value is set during tests.

Signed-off-by: Yehuda Katz <wycats@gmail.com>
  • Loading branch information...
1 parent b51632d commit 67b2d08c0a64ddec3a0c4e1c0b5d96bd418cceef @josevalim josevalim committed with wycats Jul 29, 2009
Showing with 30 additions and 30 deletions.
  1. +2 −2 actionpack/lib/action_controller/base/mime_responds.rb
  2. +28 −28 actionpack/test/dispatch/request_test.rb
@@ -146,12 +146,12 @@ def clear_respond_to!
#
# Mime::Type.register "image/jpg", :jpg
def respond_to(*mimes, &block)
+ raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
responder = Responder.new
-
block.call(responder) if block_given?
mimes = collect_mimes_from_class_level if mimes.empty?
- mimes.each { |mime| responder.send(mime) }
+ mimes.each { |mime| responder.custom(mime) }
if format = request.negotiate_mime(responder.order)
# TODO It should be just: self.formats = [ :foo ]
@@ -338,16 +338,11 @@ def teardown
end
test "XMLHttpRequest" do
- begin
- ActionController::Base.use_accept_header, old =
- false, ActionController::Base.use_accept_header
-
+ with_accept_header false do
request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
request.expects(:parameters).at_least_once.returns({})
assert request.xhr?
assert_equal Mime::JS, request.format
- ensure
- ActionController::Base.use_accept_header = old
end
end
@@ -396,39 +391,38 @@ def teardown
assert_equal({"bar" => 2}, request.query_parameters)
end
- test "formats" do
- request = stub_request 'HTTP_ACCEPT' => 'text/html'
- request.expects(:parameters).at_least_once.returns({})
- assert_equal [ Mime::HTML ], request.formats
-
- request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
- request.expects(:parameters).at_least_once.returns({})
- assert_equal with_set(Mime::XML, Mime::HTML), request.formats
+ test "formats with accept header" do
+ with_accept_header true do
+ request = stub_request 'HTTP_ACCEPT' => 'text/html'
+ request.expects(:parameters).at_least_once.returns({})
+ assert_equal [ Mime::HTML ], request.formats
- begin
- ActionController::Base.use_accept_header, old =
- false, ActionController::Base.use_accept_header
+ request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
+ request.expects(:parameters).at_least_once.returns({})
+ assert_equal with_set(Mime::XML, Mime::HTML), request.formats
+ end
+ with_accept_header false do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => :txt })
assert_equal with_set(Mime::TEXT), request.formats
- ensure
- ActionController::Base.use_accept_header = old
end
end
test "negotiate_mime" do
- request = stub_request 'HTTP_ACCEPT' => 'text/html'
- request.expects(:parameters).at_least_once.returns({})
+ with_accept_header true do
+ request = stub_request 'HTTP_ACCEPT' => 'text/html'
+ request.expects(:parameters).at_least_once.returns({})
- assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON])
- assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML])
- assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL])
+ assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON])
+ assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML])
+ assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL])
- request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
- request.expects(:parameters).at_least_once.returns({})
- assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV])
- assert_equal Mime::CSV, request.negotiate_mime([Mime::CSV, Mime::YAML])
+ request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8'
+ request.expects(:parameters).at_least_once.returns({})
+ assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV])
+ assert_equal Mime::CSV, request.negotiate_mime([Mime::CSV, Mime::YAML])
+ end
end
protected
@@ -441,4 +435,10 @@ def with_set(*args)
args + Mime::SET
end
+ def with_accept_header(value)
+ ActionController::Base.use_accept_header, old = value, ActionController::Base.use_accept_header
+ yield
+ ensure
+ ActionController::Base.use_accept_header = old
+ end
end

0 comments on commit 67b2d08

Please sign in to comment.