diff options
author | José Valim <jose.valim@gmail.com> | 2009-07-29 10:09:21 +0200 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-07-29 12:06:03 -0700 |
commit | 67b2d08c0a64ddec3a0c4e1c0b5d96bd418cceef (patch) | |
tree | 92528304fc98513b3641ce56d7e134f3631dfbc7 /actionpack/test | |
parent | b51632d34d99ee9cae3287c60887b25fdf7a3618 (diff) | |
download | rails-67b2d08c0a64ddec3a0c4e1c0b5d96bd418cceef.tar.gz rails-67b2d08c0a64ddec3a0c4e1c0b5d96bd418cceef.tar.bz2 rails-67b2d08c0a64ddec3a0c4e1c0b5d96bd418cceef.zip |
Ensure that the proper accept header value is set during tests.
Signed-off-by: Yehuda Katz <wycats@gmail.com>
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/dispatch/request_test.rb | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index 948eeeb001..8ebf9aa186 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -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 - - 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 @@ 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 |