diff options
Diffstat (limited to 'actionpack/test/controller/test_case_test.rb')
-rw-r--r-- | actionpack/test/controller/test_case_test.rb | 576 |
1 files changed, 261 insertions, 315 deletions
diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index b9caddcdb7..33f11e67ef 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -1,40 +1,40 @@ -require 'abstract_unit' -require 'controller/fake_controllers' -require 'active_support/json/decoding' -require 'rails/engine' +require "abstract_unit" +require "controller/fake_controllers" +require "active_support/json/decoding" +require "rails/engine" class TestCaseTest < ActionController::TestCase def self.fixture_path; end; class TestController < ActionController::Base def no_op - render plain: 'dummy' + render plain: "dummy" end def set_flash flash["test"] = ">#{flash["test"]}<" - render plain: 'ignore me' + render plain: "ignore me" end def delete_flash flash.delete("test") - render plain: 'ignore me' + render plain: "ignore me" end def set_flash_now flash.now["test_now"] = ">#{flash["test_now"]}<" - render plain: 'ignore me' + render plain: "ignore me" end def set_session - session['string'] = 'A wonder' - session[:symbol] = 'it works' - render plain: 'Success' + session["string"] = "A wonder" + session[:symbol] = "it works" + render plain: "Success" end def reset_the_session reset_session - render plain: 'ignore me' + render plain: "ignore me" end def render_raw_post @@ -126,20 +126,24 @@ XML end def redirect_to_same_controller - redirect_to controller: 'test', action: 'test_uri', id: 5 + redirect_to controller: "test", action: "test_uri", id: 5 end def redirect_to_different_controller - redirect_to controller: 'fail', id: 5 + redirect_to controller: "fail", id: 5 end def create - head :created, location: 'created resource' + head :created, location: "created resource" + end + + def render_cookie + render plain: cookies["foo"] end def delete_cookie cookies.delete("foo") - render plain: 'ok' + render plain: "ok" end def test_without_body @@ -150,6 +154,10 @@ XML render html: '<body class="foo"></body>'.html_safe end + def boom + raise "boom!" + end + private def generate_url(opts) @@ -160,16 +168,18 @@ XML def setup super @controller = TestController.new - @request.delete_header 'PATH_INFO' + @request.delete_header "PATH_INFO" @routes = ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do - get ':controller(/:action(/:id))' + ActiveSupport::Deprecation.silence do + get ":controller(/:action(/:id))" + end end end end class DefaultUrlOptionsCachingController < ActionController::Base - before_action { @dynamic_opt = 'opt' } + before_action { @dynamic_opt = "opt" } def test_url_options_reset render plain: url_for @@ -187,49 +197,41 @@ XML def test_assert_select_without_body get :test_without_body - assert_select 'body', 0 - assert_select 'div.foo' + assert_select "body", 0 + assert_select "div.foo" end def test_assert_select_with_body get :test_with_body - assert_select 'body.foo' + assert_select "body.foo" end def test_url_options_reset @controller = DefaultUrlOptionsCachingController.new get :test_url_options_reset - assert_nil @request.params['dynamic_opt'] + assert_nil @request.params["dynamic_opt"] assert_match(/dynamic_opt=opt/, @response.body) end def test_raw_post_handling - params = Hash[:page, { name: 'page name' }, 'some key', 123] + params = Hash[:page, { name: "page name" }, "some key", 123] post :render_raw_post, params: params.dup assert_equal params.to_query, @response.body end def test_body_stream - params = Hash[:page, { name: 'page name' }, 'some key', 123] + params = Hash[:page, { name: "page name" }, "some key", 123] post :render_body, params: params.dup assert_equal params.to_query, @response.body end - def test_deprecated_body_stream - params = Hash[:page, { name: 'page name' }, 'some key', 123] - - assert_deprecated { post :render_body, params.dup } - - assert_equal params.to_query, @response.body - end - def test_document_body_and_params_with_post post :test_params, params: { id: 1 } - assert_equal({"id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params"}, ::JSON.parse(@response.body)) + assert_equal({ "id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params" }, ::JSON.parse(@response.body)) end def test_document_body_with_post @@ -237,21 +239,11 @@ XML assert_equal "document body", @response.body end - def test_deprecated_document_body_with_post - assert_deprecated { post :render_body, "document body" } - assert_equal "document body", @response.body - end - def test_document_body_with_put put :render_body, body: "document body" assert_equal "document body", @response.body end - def test_deprecated_document_body_with_put - assert_deprecated { put :render_body, "document body" } - assert_equal "document body", @response.body - end - def test_head head :test_params assert_equal 200, @response.status @@ -259,31 +251,21 @@ XML def test_process_without_flash process :set_flash - assert_equal '><', flash['test'] - end - - def test_deprecated_process_with_flash - assert_deprecated { process :set_flash, "GET", nil, nil, { "test" => "value" } } - assert_equal '>value<', flash['test'] + assert_equal "><", flash["test"] end def test_process_with_flash process :set_flash, method: "GET", flash: { "test" => "value" } - assert_equal '>value<', flash['test'] - end - - def test_deprecated_process_with_flash_now - assert_deprecated { process :set_flash_now, "GET", nil, nil, { "test_now" => "value_now" } } - assert_equal '>value_now<', flash['test_now'] + assert_equal ">value<", flash["test"] end def test_process_with_flash_now process :set_flash_now, method: "GET", flash: { "test_now" => "value_now" } - assert_equal '>value_now<', flash['test_now'] + assert_equal ">value_now<", flash["test_now"] end def test_process_delete_flash @@ -295,64 +277,38 @@ XML def test_process_with_session process :set_session - assert_equal 'A wonder', session['string'], "A value stored in the session should be available by string key" - assert_equal 'A wonder', session[:string], "Test session hash should allow indifferent access" - assert_equal 'it works', session['symbol'], "Test session hash should allow indifferent access" - assert_equal 'it works', session[:symbol], "Test session hash should allow indifferent access" - end - - def test_process_with_session_arg - assert_deprecated { process :no_op, "GET", nil, { 'string' => 'value1', symbol: 'value2' } } - assert_equal 'value1', session['string'] - assert_equal 'value1', session[:string] - assert_equal 'value2', session['symbol'] - assert_equal 'value2', session[:symbol] + assert_equal "A wonder", session["string"], "A value stored in the session should be available by string key" + assert_equal "A wonder", session[:string], "Test session hash should allow indifferent access" + assert_equal "it works", session["symbol"], "Test session hash should allow indifferent access" + assert_equal "it works", session[:symbol], "Test session hash should allow indifferent access" end def test_process_with_session_kwarg - process :no_op, method: "GET", session: { 'string' => 'value1', symbol: 'value2' } - assert_equal 'value1', session['string'] - assert_equal 'value1', session[:string] - assert_equal 'value2', session['symbol'] - assert_equal 'value2', session[:symbol] - end - - def test_deprecated_process_merges_session_arg - session[:foo] = 'bar' - assert_deprecated { - get :no_op, nil, { bar: 'baz' } - } - assert_equal 'bar', session[:foo] - assert_equal 'baz', session[:bar] + process :no_op, method: "GET", session: { "string" => "value1", symbol: "value2" } + assert_equal "value1", session["string"] + assert_equal "value1", session[:string] + assert_equal "value2", session["symbol"] + assert_equal "value2", session[:symbol] end def test_process_merges_session_arg - session[:foo] = 'bar' - get :no_op, session: { bar: 'baz' } - assert_equal 'bar', session[:foo] - assert_equal 'baz', session[:bar] - end - - def test_deprecated_merged_session_arg_is_retained_across_requests - assert_deprecated { - get :no_op, nil, { foo: 'bar' } - } - assert_equal 'bar', session[:foo] - get :no_op - assert_equal 'bar', session[:foo] + session[:foo] = "bar" + get :no_op, session: { bar: "baz" } + assert_equal "bar", session[:foo] + assert_equal "baz", session[:bar] end def test_merged_session_arg_is_retained_across_requests - get :no_op, session: { foo: 'bar' } - assert_equal 'bar', session[:foo] + get :no_op, session: { foo: "bar" } + assert_equal "bar", session[:foo] get :no_op - assert_equal 'bar', session[:foo] + assert_equal "bar", session[:foo] end def test_process_overwrites_existing_session_arg - session[:foo] = 'bar' - get :no_op, session: { foo: 'baz' } - assert_equal 'baz', session[:foo] + session[:foo] = "bar" + get :no_op, session: { foo: "baz" } + assert_equal "baz", session[:foo] end def test_session_is_cleared_from_controller_after_reset_session @@ -383,11 +339,6 @@ XML assert_equal "/test_case_test/test/test_uri", @response.body end - def test_deprecated_process_with_request_uri_with_params - assert_deprecated { process :test_uri, "GET", id: 7 } - assert_equal "/test_case_test/test/test_uri/7", @response.body - end - def test_process_with_request_uri_with_params process :test_uri, method: "GET", @@ -396,14 +347,8 @@ XML assert_equal "/test_case_test/test/test_uri/7", @response.body end - def test_deprecated_process_with_request_uri_with_params_with_explicit_uri - @request.env['PATH_INFO'] = "/explicit/uri" - assert_deprecated { process :test_uri, "GET", id: 7 } - assert_equal "/explicit/uri", @response.body - end - def test_process_with_request_uri_with_params_with_explicit_uri - @request.env['PATH_INFO'] = "/explicit/uri" + @request.env["PATH_INFO"] = "/explicit/uri" process :test_uri, method: "GET", params: { id: 7 } assert_equal "/explicit/uri", @response.body end @@ -411,13 +356,13 @@ XML def test_process_with_query_string process :test_query_string, method: "GET", - params: { q: 'test' } + params: { q: "test" } assert_equal "q=test", @response.body end def test_process_with_query_string_with_explicit_uri - @request.env['PATH_INFO'] = '/explicit/uri' - @request.env['QUERY_STRING'] = 'q=test?extra=question' + @request.env["PATH_INFO"] = "/explicit/uri" + @request.env["QUERY_STRING"] = "q=test?extra=question" process :test_query_string assert_equal "q=test?extra=question", @response.body end @@ -434,7 +379,7 @@ XML begin $stderr = StringIO.new - assert_select 'area' #This will cause a warning if content is processed as HTML + assert_select "area" #This will cause a warning if content is processed as HTML $stderr.rewind && err = $stderr.read ensure $stderr = STDERR @@ -444,21 +389,21 @@ XML end def test_assert_generates - assert_generates 'controller/action/5', controller: 'controller', action: 'action', id: '5' - assert_generates 'controller/action/7', { id: "7" }, { controller: "controller", action: "action" } - assert_generates 'controller/action/5', { controller: "controller", action: "action", id: "5", name: "bob" }, {}, { name: "bob" } - assert_generates 'controller/action/7', { id: "7", name: "bob" }, { controller: "controller", action: "action" }, { name: "bob" } - assert_generates 'controller/action/7', { id: "7" }, { controller: "controller", action: "action", name: "bob" }, {} + assert_generates "controller/action/5", controller: "controller", action: "action", id: "5" + assert_generates "controller/action/7", { id: "7" }, controller: "controller", action: "action" + assert_generates "controller/action/5", { controller: "controller", action: "action", id: "5", name: "bob" }, {}, name: "bob" + assert_generates "controller/action/7", { id: "7", name: "bob" }, { controller: "controller", action: "action" }, name: "bob" + assert_generates "controller/action/7", { id: "7" }, { controller: "controller", action: "action", name: "bob" }, {} end def test_assert_routing - assert_routing 'content', controller: 'content', action: 'index' + assert_routing "content", controller: "content", action: "index" end def test_assert_routing_with_method with_routing do |set| set.draw { resources(:content) } - assert_routing({ method: 'post', path: 'content' }, { controller: 'content', action: 'create' }) + assert_routing({ method: "post", path: "content" }, controller: "content", action: "create") end end @@ -466,125 +411,101 @@ XML with_routing do |set| set.draw do namespace :admin do - get 'user' => 'user#index' + get "user" => "user#index" end end - assert_routing 'admin/user', controller: 'admin/user', action: 'index' + assert_routing "admin/user", controller: "admin/user", action: "index" end end def test_assert_routing_with_glob with_routing do |set| - set.draw { get('*path' => "pages#show") } - assert_routing('/company/about', { controller: 'pages', action: 'show', path: 'company/about' }) + set.draw { get("*path" => "pages#show") } + assert_routing("/company/about", controller: "pages", action: "show", path: "company/about") end end - def test_deprecated_params_passing - assert_deprecated { - get :test_params, page: { name: "Page name", month: '4', year: '2004', day: '6' } - } - parsed_params = ::JSON.parse(@response.body) - assert_equal( - { - 'controller' => 'test_case_test/test', 'action' => 'test_params', - 'page' => { 'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6' } - }, - parsed_params - ) - end - def test_params_passing get :test_params, params: { page: { name: "Page name", - month: '4', - year: '2004', - day: '6' + month: "4", + year: "2004", + day: "6" } } parsed_params = ::JSON.parse(@response.body) assert_equal( { - 'controller' => 'test_case_test/test', 'action' => 'test_params', - 'page' => { 'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6' } + "controller" => "test_case_test/test", "action" => "test_params", + "page" => { "name" => "Page name", "month" => "4", "year" => "2004", "day" => "6" } }, parsed_params ) end def test_query_param_named_action - get :test_query_parameters, params: {action: 'foobar'} + get :test_query_parameters, params: { action: "foobar" } parsed_params = JSON.parse(@response.body) - assert_equal({'action' => 'foobar'}, parsed_params) + assert_equal({ "action" => "foobar" }, parsed_params) end def test_request_param_named_action - post :test_request_parameters, params: {action: 'foobar'} + post :test_request_parameters, params: { action: "foobar" } parsed_params = eval(@response.body) - assert_equal({'action' => 'foobar'}, parsed_params) + assert_equal({ "action" => "foobar" }, parsed_params) end def test_kwarg_params_passing_with_session_and_flash get :test_params, params: { page: { name: "Page name", - month: '4', - year: '2004', - day: '6' + month: "4", + year: "2004", + day: "6" } - }, session: { 'foo' => 'bar' }, flash: { notice: 'created' } + }, session: { "foo" => "bar" }, flash: { notice: "created" } parsed_params = ::JSON.parse(@response.body) assert_equal( - {'controller' => 'test_case_test/test', 'action' => 'test_params', - 'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}}, + { "controller" => "test_case_test/test", "action" => "test_params", + "page" => { "name" => "Page name", "month" => "4", "year" => "2004", "day" => "6" } }, parsed_params ) - assert_equal 'bar', session[:foo] - assert_equal 'created', flash[:notice] + assert_equal "bar", session[:foo] + assert_equal "created", flash[:notice] end - def test_params_passing_with_fixnums + def test_params_passing_with_integer get :test_params, params: { page: { name: "Page name", month: 4, year: 2004, day: 6 } } parsed_params = ::JSON.parse(@response.body) assert_equal( - {'controller' => 'test_case_test/test', 'action' => 'test_params', - 'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}}, + { "controller" => "test_case_test/test", "action" => "test_params", + "page" => { "name" => "Page name", "month" => "4", "year" => "2004", "day" => "6" } }, parsed_params ) end - def test_params_passing_with_fixnums_when_not_html_request - get :test_params, params: { format: 'json', count: 999 } + def test_params_passing_with_integers_when_not_html_request + get :test_params, params: { format: "json", count: 999 } parsed_params = ::JSON.parse(@response.body) assert_equal( - {'controller' => 'test_case_test/test', 'action' => 'test_params', - 'format' => 'json', 'count' => '999' }, + { "controller" => "test_case_test/test", "action" => "test_params", + "format" => "json", "count" => "999" }, parsed_params ) end def test_params_passing_path_parameter_is_string_when_not_html_request - get :test_params, params: { format: 'json', id: 1 } - parsed_params = ::JSON.parse(@response.body) - assert_equal( - {'controller' => 'test_case_test/test', 'action' => 'test_params', - 'format' => 'json', 'id' => '1' }, - parsed_params - ) - end - - def test_deprecated_params_passing_path_parameter_is_string_when_not_html_request - assert_deprecated { get :test_params, format: 'json', id: 1 } + get :test_params, params: { format: "json", id: 1 } parsed_params = ::JSON.parse(@response.body) assert_equal( - {'controller' => 'test_case_test/test', 'action' => 'test_params', - 'format' => 'json', 'id' => '1' }, + { "controller" => "test_case_test/test", "action" => "test_params", + "format" => "json", "id" => "1" }, parsed_params ) end @@ -592,13 +513,13 @@ XML def test_params_passing_with_frozen_values assert_nothing_raised do get :test_params, params: { - frozen: 'icy'.freeze, frozens: ['icy'.freeze].freeze, deepfreeze: { frozen: 'icy'.freeze }.freeze + frozen: "icy".freeze, frozens: ["icy".freeze].freeze, deepfreeze: { frozen: "icy".freeze }.freeze } end parsed_params = ::JSON.parse(@response.body) assert_equal( - {'controller' => 'test_case_test/test', 'action' => 'test_params', - 'frozen' => 'icy', 'frozens' => ['icy'], 'deepfreeze' => { 'frozen' => 'icy' }}, + { "controller" => "test_case_test/test", "action" => "test_params", + "frozen" => "icy", "frozens" => ["icy"], "deepfreeze" => { "frozen" => "icy" } }, parsed_params ) end @@ -610,8 +531,8 @@ XML end test "set additional HTTP headers" do - @request.headers['Referer'] = "http://nohost.com/home" - @request.headers['Content-Type'] = "application/rss+xml" + @request.headers["Referer"] = "http://nohost.com/home" + @request.headers["Content-Type"] = "application/rss+xml" get :test_headers parsed_env = ActiveSupport::JSON.decode(@response.body) assert_equal "http://nohost.com/home", parsed_env["HTTP_REFERER"] @@ -619,36 +540,50 @@ XML end test "set additional env variables" do - @request.headers['HTTP_REFERER'] = "http://example.com/about" - @request.headers['CONTENT_TYPE'] = "application/json" + @request.headers["HTTP_REFERER"] = "http://example.com/about" + @request.headers["CONTENT_TYPE"] = "application/json" get :test_headers parsed_env = ActiveSupport::JSON.decode(@response.body) assert_equal "http://example.com/about", parsed_env["HTTP_REFERER"] assert_equal "application/json", parsed_env["CONTENT_TYPE"] end + def test_using_as_json_sets_request_content_type_to_json + post :render_body, params: { bool_value: true, str_value: "string", num_value: 2 }, as: :json + + assert_equal "application/json", @request.headers["CONTENT_TYPE"] + assert_equal true, @request.request_parameters[:bool_value] + assert_equal "string", @request.request_parameters[:str_value] + assert_equal 2, @request.request_parameters[:num_value] + end + + def test_using_as_json_sets_format_json + post :render_body, params: { bool_value: true, str_value: "string", num_value: 2 }, as: :json + assert_equal "json", @request.format + end + def test_mutating_content_type_headers_for_plain_text_files_sets_the_header - @request.headers['Content-Type'] = 'text/plain' - post :render_body, params: { name: 'foo.txt' } + @request.headers["Content-Type"] = "text/plain" + post :render_body, params: { name: "foo.txt" } - assert_equal 'text/plain', @request.headers['Content-type'] - assert_equal 'foo.txt', @request.request_parameters[:name] - assert_equal 'render_body', @request.path_parameters[:action] + assert_equal "text/plain", @request.headers["Content-type"] + assert_equal "foo.txt", @request.request_parameters[:name] + assert_equal "render_body", @request.path_parameters[:action] end def test_mutating_content_type_headers_for_html_files_sets_the_header - @request.headers['Content-Type'] = 'text/html' - post :render_body, params: { name: 'foo.html' } + @request.headers["Content-Type"] = "text/html" + post :render_body, params: { name: "foo.html" } - assert_equal 'text/html', @request.headers['Content-type'] - assert_equal 'foo.html', @request.request_parameters[:name] - assert_equal 'render_body', @request.path_parameters[:action] + assert_equal "text/html", @request.headers["Content-type"] + assert_equal "foo.html", @request.request_parameters[:name] + assert_equal "render_body", @request.path_parameters[:action] end def test_mutating_content_type_headers_for_non_registered_mime_type_raises_an_error assert_raises(RuntimeError) do - @request.headers['Content-Type'] = 'type/fake' - post :render_body, params: { name: 'foo.fake' } + @request.headers["Content-Type"] = "type/fake" + post :render_body, params: { name: "foo.fake" } end end @@ -659,21 +594,19 @@ XML assert_kind_of String, @request.path_parameters[:id] end - def test_deprecared_id_converted_to_string - assert_deprecated { get :test_params, id: 20, foo: Object.new} - assert_kind_of String, @request.path_parameters[:id] - end - def test_array_path_parameter_handled_properly with_routing do |set| set.draw do - get 'file/*path', to: 'test_case_test/test#test_params' - get ':controller/:action' + get "file/*path", to: "test_case_test/test#test_params" + + ActiveSupport::Deprecation.silence do + get ":controller/:action" + end end - get :test_params, params: { path: ['hello', 'world'] } - assert_equal ['hello', 'world'], @request.path_parameters[:path] - assert_equal 'hello/world', @request.path_parameters[:path].to_param + get :test_params, params: { path: ["hello", "world"] } + assert_equal ["hello", "world"], @request.path_parameters[:path] + assert_equal "hello/world", @request.path_parameters[:path].to_param end end @@ -691,8 +624,8 @@ XML routes_id = @routes.object_id begin - with_routing { raise 'fail' } - fail 'Should not be here.' + with_routing { raise "fail" } + fail "Should not be here." rescue RuntimeError end @@ -711,46 +644,23 @@ XML def test_header_properly_reset_after_remote_http_request get :test_params, xhr: true - assert_nil @request.env['HTTP_X_REQUESTED_WITH'] - assert_nil @request.env['HTTP_ACCEPT'] - end - - def test_deprecated_xhr_with_params - assert_deprecated { xhr :get, :test_params, params: { id: 1 } } - - assert_equal({"id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params"}, ::JSON.parse(@response.body)) + assert_nil @request.env["HTTP_X_REQUESTED_WITH"] + assert_nil @request.env["HTTP_ACCEPT"] end def test_xhr_with_params get :test_params, params: { id: 1 }, xhr: true - assert_equal({"id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params"}, ::JSON.parse(@response.body)) + assert_equal({ "id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params" }, ::JSON.parse(@response.body)) end def test_xhr_with_session get :set_session, xhr: true - assert_equal 'A wonder', session['string'], "A value stored in the session should be available by string key" - assert_equal 'A wonder', session[:string], "Test session hash should allow indifferent access" - assert_equal 'it works', session['symbol'], "Test session hash should allow indifferent access" - assert_equal 'it works', session[:symbol], "Test session hash should allow indifferent access" - end - - def test_deprecated_xhr_with_session - assert_deprecated { xhr :get, :set_session } - - assert_equal 'A wonder', session['string'], "A value stored in the session should be available by string key" - assert_equal 'A wonder', session[:string], "Test session hash should allow indifferent access" - assert_equal 'it works', session['symbol'], "Test session hash should allow indifferent access" - assert_equal 'it works', session[:symbol], "Test session hash should allow indifferent access" - end - - def test_deprecated_params_reset_between_post_requests - assert_deprecated { post :no_op, foo: "bar" } - assert_equal "bar", @request.params[:foo] - - post :no_op - assert @request.params[:foo].blank? + assert_equal "A wonder", session["string"], "A value stored in the session should be available by string key" + assert_equal "A wonder", session[:string], "Test session hash should allow indifferent access" + assert_equal "it works", session["symbol"], "Test session hash should allow indifferent access" + assert_equal "it works", session[:symbol], "Test session hash should allow indifferent access" end def test_params_reset_between_post_requests @@ -791,48 +701,62 @@ XML end def test_request_format - get :test_format, params: { format: 'html' } - assert_equal 'text/html', @response.body + get :test_format, params: { format: "html" } + assert_equal "text/html", @response.body - get :test_format, params: { format: 'json' } - assert_equal 'application/json', @response.body + get :test_format, params: { format: "json" } + assert_equal "application/json", @response.body - get :test_format, params: { format: 'xml' } - assert_equal 'application/xml', @response.body + get :test_format, params: { format: "xml" } + assert_equal "application/xml", @response.body get :test_format - assert_equal 'text/html', @response.body + assert_equal "text/html", @response.body end def test_request_format_kwarg - get :test_format, format: 'html' - assert_equal 'text/html', @response.body + get :test_format, format: "html" + assert_equal "text/html", @response.body - get :test_format, format: 'json' - assert_equal 'application/json', @response.body + get :test_format, format: "json" + assert_equal "application/json", @response.body - get :test_format, format: 'xml' - assert_equal 'application/xml', @response.body + get :test_format, format: "xml" + assert_equal "application/xml", @response.body get :test_format - assert_equal 'text/html', @response.body + assert_equal "text/html", @response.body end def test_request_format_kwarg_overrides_params - get :test_format, format: 'json', params: { format: 'html' } - assert_equal 'application/json', @response.body + get :test_format, format: "json", params: { format: "html" } + assert_equal "application/json", @response.body end def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set - cookies['foo'] = 'bar' + cookies["foo"] = "bar" get :no_op - assert_equal 'bar', cookies['foo'] + assert_equal "bar", cookies["foo"] + end + + def test_cookies_should_be_escaped_properly + cookies["foo"] = "+" + get :render_cookie + assert_equal "+", @response.body end def test_should_detect_if_cookie_is_deleted - cookies['foo'] = 'bar' + cookies["foo"] = "bar" get :delete_cookie - assert_nil cookies['foo'] + assert_nil cookies["foo"] + end + + def test_multiple_mixed_method_process_should_scrub_rack_input + post :test_params, params: { id: 1, foo: "an foo" } + assert_equal({ "id"=>"1", "foo" => "an foo", "controller"=>"test_case_test/test", "action"=>"test_params" }, ::JSON.parse(@response.body)) + + get :test_params, params: { bar: "an bar" } + assert_equal({ "bar"=>"an bar", "controller"=>"test_case_test/test", "action"=>"test_params" }, ::JSON.parse(@response.body)) end %w(controller response request).each do |variable| @@ -851,15 +775,15 @@ XML end end - FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart' + FILES_DIR = File.dirname(__FILE__) + "/../fixtures/multipart" - READ_BINARY = 'rb:binary' - READ_PLAIN = 'r:binary' + READ_BINARY = "rb:binary" + READ_PLAIN = "r:binary" def test_test_uploaded_file - filename = 'mona_lisa.jpg' + filename = "ruby_on_rails.jpg" path = "#{FILES_DIR}/#{filename}" - content_type = 'image/png' + content_type = "image/png" expected = File.read(path) expected.force_encoding(Encoding::BINARY) @@ -877,15 +801,15 @@ XML def test_fixture_path_is_accessed_from_self_instead_of_active_support_test_case TestCaseTest.stub :fixture_path, FILES_DIR do - uploaded_file = fixture_file_upload('/mona_lisa.jpg', 'image/png') - assert_equal File.open("#{FILES_DIR}/mona_lisa.jpg", READ_PLAIN).read, uploaded_file.read + uploaded_file = fixture_file_upload("/ruby_on_rails.jpg", "image/png") + assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read end end def test_test_uploaded_file_with_binary - filename = 'mona_lisa.jpg' + filename = "ruby_on_rails.jpg" path = "#{FILES_DIR}/#{filename}" - content_type = 'image/png' + content_type = "image/png" binary_uploaded_file = Rack::Test::UploadedFile.new(path, content_type, :binary) assert_equal File.open(path, READ_BINARY).read, binary_uploaded_file.read @@ -895,9 +819,9 @@ XML end def test_fixture_file_upload_with_binary - filename = 'mona_lisa.jpg' + filename = "ruby_on_rails.jpg" path = "#{FILES_DIR}/#{filename}" - content_type = 'image/jpg' + content_type = "image/jpg" binary_file_upload = fixture_file_upload(path, content_type, :binary) assert_equal File.open(path, READ_BINARY).read, binary_file_upload.read @@ -907,50 +831,48 @@ XML end def test_fixture_file_upload_should_be_able_access_to_tempfile - file = fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") + file = fixture_file_upload(FILES_DIR + "/ruby_on_rails.jpg", "image/jpg") assert file.respond_to?(:tempfile), "expected tempfile should respond on fixture file object, got nothing" end def test_fixture_file_upload post :test_file_upload, params: { - file: fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") + file: fixture_file_upload(FILES_DIR + "/ruby_on_rails.jpg", "image/jpg") } - assert_equal '159528', @response.body + assert_equal "45142", @response.body end def test_fixture_file_upload_relative_to_fixture_path TestCaseTest.stub :fixture_path, FILES_DIR do - uploaded_file = fixture_file_upload("mona_lisa.jpg", "image/jpg") - assert_equal File.open("#{FILES_DIR}/mona_lisa.jpg", READ_PLAIN).read, uploaded_file.read + uploaded_file = fixture_file_upload("ruby_on_rails.jpg", "image/jpg") + assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read end end - def test_fixture_file_upload_ignores_nil_fixture_path - uploaded_file = fixture_file_upload("#{FILES_DIR}/mona_lisa.jpg", "image/jpg") - assert_equal File.open("#{FILES_DIR}/mona_lisa.jpg", READ_PLAIN).read, uploaded_file.read + def test_fixture_file_upload_ignores_fixture_path_given_full_path + TestCaseTest.stub :fixture_path, File.dirname(__FILE__) do + uploaded_file = fixture_file_upload("#{FILES_DIR}/ruby_on_rails.jpg", "image/jpg") + assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read + end end - def test_deprecated_action_dispatch_uploaded_file_upload - filename = 'mona_lisa.jpg' - path = "#{FILES_DIR}/#{filename}" - assert_deprecated { - post :test_file_upload, file: Rack::Test::UploadedFile.new(path, "image/jpg", true) - } - assert_equal '159528', @response.body + def test_fixture_file_upload_ignores_nil_fixture_path + uploaded_file = fixture_file_upload("#{FILES_DIR}/ruby_on_rails.jpg", "image/jpg") + assert_equal File.open("#{FILES_DIR}/ruby_on_rails.jpg", READ_PLAIN).read, uploaded_file.read end def test_action_dispatch_uploaded_file_upload - filename = 'mona_lisa.jpg' + filename = "ruby_on_rails.jpg" path = "#{FILES_DIR}/#{filename}" post :test_file_upload, params: { file: Rack::Test::UploadedFile.new(path, "image/jpg", true) } - assert_equal '159528', @response.body + assert_equal "45142", @response.body end def test_test_uploaded_file_exception_when_file_doesnt_exist - assert_raise(RuntimeError) { Rack::Test::UploadedFile.new('non_existent_file') } + assert_raise(RuntimeError) { Rack::Test::UploadedFile.new("non_existent_file") } end def test_redirect_url_only_cares_about_location_header @@ -958,21 +880,41 @@ XML assert_response :created # Redirect url doesn't care that it wasn't a :redirect response. - assert_equal 'created resource', @response.redirect_url + assert_equal "created resource", @response.redirect_url assert_equal @response.redirect_url, redirect_to_url # Must be a :redirect response. assert_raise(ActiveSupport::TestCase::Assertion) do - assert_redirected_to 'created resource' + assert_redirected_to "created resource" end end + + def test_exception_in_action_reaches_test + assert_raise(RuntimeError) do + process :boom, method: "GET" + end + end + + def test_request_state_is_cleared_after_exception + assert_raise(RuntimeError) do + process :boom, + method: "GET", + params: { q: "test1" } + end + + process :test_query_string, + method: "GET", + params: { q: "test2" } + + assert_equal "q=test2", @response.body + end end class ResponseDefaultHeadersTest < ActionController::TestCase class TestController < ActionController::Base def remove_header headers.delete params[:header] - head :ok, 'C' => '3' + head :ok, "C" => "3" end # Render a head response, but don't touch default headers @@ -983,7 +925,7 @@ class ResponseDefaultHeadersTest < ActionController::TestCase def before_setup @original = ActionDispatch::Response.default_headers - @defaults = { 'A' => '1', 'B' => '2' } + @defaults = { "A" => "1", "B" => "2" } ActionDispatch::Response.default_headers = @defaults super end @@ -995,10 +937,12 @@ class ResponseDefaultHeadersTest < ActionController::TestCase def setup super @controller = TestController.new - @request.env['PATH_INFO'] = nil + @request.env["PATH_INFO"] = nil @routes = ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do - get ':controller(/:action(/:id))' + ActiveSupport::Deprecation.silence do + get ":controller(/:action(/:id))" + end end end end @@ -1007,18 +951,18 @@ class ResponseDefaultHeadersTest < ActionController::TestCase get :leave_alone # Response headers start out with the defaults - assert_equal @defaults.merge('Content-Type' => 'text/html'), response.headers + assert_equal @defaults.merge("Content-Type" => "text/html"), response.headers end test "response deletes a default header" do - get :remove_header, params: { header: 'A' } + get :remove_header, params: { header: "A" } assert_response :ok # After a request, the response in the test case doesn't have the # defaults merged on top again. - assert_not_includes response.headers, 'A' - assert_includes response.headers, 'B' - assert_includes response.headers, 'C' + assert_not_includes response.headers, "A" + assert_includes response.headers, "B" + assert_includes response.headers, "C" end end @@ -1027,13 +971,13 @@ module EngineControllerTests isolate_namespace EngineControllerTests routes.draw do - get '/' => 'bar#index' + get "/" => "bar#index" end end class BarController < ActionController::Base def index - render plain: 'bar' + render plain: "bar" end end @@ -1042,7 +986,7 @@ module EngineControllerTests def test_engine_controller_route get :index - assert_equal @response.body, 'bar' + assert_equal @response.body, "bar" end end @@ -1055,7 +999,7 @@ module EngineControllerTests def test_engine_controller_route get :index - assert_equal @response.body, 'bar' + assert_equal @response.body, "bar" end end end @@ -1096,7 +1040,7 @@ class CrazySymbolNameTest < ActionController::TestCase end class CrazyStringNameTest < ActionController::TestCase - tests 'content' + tests "content" def test_set_controller_class_using_string assert_equal ContentController, self.class.controller_class @@ -1109,8 +1053,8 @@ class NamedRoutesControllerTest < ActionController::TestCase def test_should_be_able_to_use_named_routes_before_a_request_is_done with_routing do |set| set.draw { resources :contents } - assert_equal 'http://test.host/contents/new', new_content_url - assert_equal 'http://test.host/contents/1', content_url(id: 1) + assert_equal "http://test.host/contents/new", new_content_url + assert_equal "http://test.host/contents/1", content_url(id: 1) end end end @@ -1125,14 +1069,16 @@ class AnonymousControllerTest < ActionController::TestCase @routes = ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do - get ':controller(/:action(/:id))' + ActiveSupport::Deprecation.silence do + get ":controller(/:action(/:id))" + end end end end def test_controller_name get :index - assert_equal 'anonymous', @response.body + assert_equal "anonymous", @response.body end end @@ -1150,19 +1096,19 @@ class RoutingDefaultsTest < ActionController::TestCase @routes = ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do - get '/posts/:id', to: 'anonymous#post', bucket_type: 'post' - get '/projects/:id', to: 'anonymous#project', defaults: { bucket_type: 'project' } + get "/posts/:id", to: "anonymous#post", bucket_type: "post" + get "/projects/:id", to: "anonymous#project", defaults: { bucket_type: "project" } end end end def test_route_option_can_be_passed_via_process - get :post, params: { id: 1, bucket_type: 'post'} - assert_equal '/posts/1', @response.body + get :post, params: { id: 1, bucket_type: "post" } + assert_equal "/posts/1", @response.body end def test_route_default_is_not_required_for_building_request_uri get :project, params: { id: 2 } - assert_equal '/projects/2', @response.body + assert_equal "/projects/2", @response.body end end |