mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure that the proper accept header value is set during tests.
Signed-off-by: Yehuda Katz <wycats@gmail.com>
This commit is contained in:
parent
b51632d34d
commit
67b2d08c0a
2 changed files with 30 additions and 30 deletions
|
@ -146,12 +146,12 @@ module ActionController #:nodoc:
|
|||
#
|
||||
# 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 @@ class RequestTest < ActiveSupport::TestCase
|
|||
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 @@ class RequestTest < ActiveSupport::TestCase
|
|||
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
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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 @@ protected
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue