require 'abstract_unit' require 'controller/fake_controllers' require 'active_support/json/decoding' class TestCaseTest < ActionController::TestCase class TestController < ActionController::Base def no_op render :text => 'dummy' end def set_flash flash["test"] = ">#{flash["test"]}<" render :text => 'ignore me' end def set_flash_now flash.now["test_now"] = ">#{flash["test_now"]}<" render :text => 'ignore me' end def set_session session['string'] = 'A wonder' session[:symbol] = 'it works' render :text => 'Success' end def reset_the_session reset_session render :text => 'ignore me' end def render_raw_post raise ActiveSupport::TestCase::Assertion, "#raw_post is blank" if request.raw_post.blank? render :text => request.raw_post end def render_body render :text => request.body.read end def test_params render :text => params.inspect end def test_uri render :text => request.fullpath end def test_format render :text => request.format end def test_query_string render :text => request.query_string end def test_protocol render :text => request.protocol end def test_headers render text: request.headers.env.to_json end def test_html_output render :text => <
Name:
HTML end def test_xml_output response.content_type = "application/xml" render :text => < area is an empty tag in HTML, raising an error if not in xml mode XML end def test_only_one_param render :text => (params[:left] && params[:right]) ? "EEP, Both here!" : "OK" end def test_remote_addr render :text => (request.remote_addr || "not specified") end def test_file_upload render :text => params[:file].size end def test_send_file send_file(File.expand_path(__FILE__)) end def redirect_to_same_controller redirect_to :controller => 'test', :action => 'test_uri', :id => 5 end def redirect_to_different_controller redirect_to :controller => 'fail', :id => 5 end def create head :created, :location => 'created resource' end def delete_cookie cookies.delete("foo") render :nothing => true end def test_assigns @foo = "foo" @foo_hash = {:foo => :bar} render :nothing => true end private def generate_url(opts) url_for(opts.merge(:action => "test_uri")) end end def setup super @controller = TestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @request.env['PATH_INFO'] = nil @routes = ActionDispatch::Routing::RouteSet.new.tap do |r| r.draw do get ':controller(/:action(/:id))' end end end class ViewAssignsController < ActionController::Base def test_assigns @foo = "foo" render :nothing => true end def view_assigns { "bar" => "bar" } end end class DefaultUrlOptionsCachingController < ActionController::Base before_action { @dynamic_opt = 'opt' } def test_url_options_reset render text: url_for(params) end def default_url_options if defined?(@dynamic_opt) super.merge dynamic_opt: @dynamic_opt else super end end end def test_url_options_reset @controller = DefaultUrlOptionsCachingController.new get :test_url_options_reset 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] post :render_raw_post, params.dup assert_equal params.to_query, @response.body end def test_body_stream params = Hash[:page, { :name => 'page name' }, 'some key', 123] post :render_body, params.dup assert_equal params.to_query, @response.body end def test_document_body_and_params_with_post post :test_params, :id => 1 assert_equal("{\"id\"=>\"1\", \"controller\"=>\"test_case_test/test\", \"action\"=>\"test_params\"}", @response.body) end def test_document_body_with_post post :render_body, "document body" assert_equal "document body", @response.body end def test_document_body_with_put put :render_body, "document body" assert_equal "document body", @response.body end def test_head head :test_params assert_equal 200, @response.status end def test_head_params_as_sting assert_raise(NoMethodError) { head :test_params, "document body", :id => 10 } end def test_process_without_flash process :set_flash assert_equal '><', flash['test'] end def test_process_with_flash process :set_flash, "GET", nil, nil, { "test" => "value" } assert_equal '>value<', flash['test'] end def test_process_with_flash_now process :set_flash_now, "GET", nil, nil, { "test_now" => "value_now" } assert_equal '>value_now<', flash['test_now'] end 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 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] end def test_process_merges_session_arg session[:foo] = 'bar' get :no_op, nil, { :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, nil, { :foo => 'bar' } assert_equal 'bar', session[:foo] get :no_op assert_equal 'bar', session[:foo] end def test_process_overwrites_existing_session_arg session[:foo] = 'bar' get :no_op, nil, { :foo => 'baz' } assert_equal 'baz', session[:foo] end def test_session_is_cleared_from_controller_after_reset_session process :set_session process :reset_the_session assert_equal Hash.new, @controller.session.to_hash end def test_session_is_cleared_from_request_after_reset_session process :set_session process :reset_the_session assert_equal Hash.new, @request.session.to_hash end def test_response_and_request_have_nice_accessors process :no_op assert_equal @response, response assert_equal @request, request end def test_process_with_request_uri_with_no_params process :test_uri assert_equal "/test_case_test/test/test_uri", @response.body end def test_process_with_request_uri_with_params 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_with_explicit_uri @request.env['PATH_INFO'] = "/explicit/uri" process :test_uri, "GET", :id => 7 assert_equal "/explicit/uri", @response.body end def test_process_with_query_string process :test_query_string, "GET", :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' process :test_query_string assert_equal "q=test?extra=question", @response.body end def test_multiple_calls process :test_only_one_param, "GET", :left => true assert_equal "OK", @response.body process :test_only_one_param, "GET", :right => true assert_equal "OK", @response.body end def test_assigns process :test_assigns # assigns can be accessed using assigns(key) # or assigns[key], where key is a string or # a symbol assert_equal "foo", assigns(:foo) assert_equal "foo", assigns("foo") assert_equal "foo", assigns[:foo] assert_equal "foo", assigns["foo"] # but the assigned variable should not have its own keys stringified expected_hash = { :foo => :bar } assert_equal expected_hash, assigns(:foo_hash) end def test_view_assigns @controller = ViewAssignsController.new process :test_assigns assert_equal nil, assigns(:foo) assert_equal nil, assigns[:foo] assert_equal "bar", assigns(:bar) assert_equal "bar", assigns[:bar] end def test_assert_tag_tag process :test_html_output # there is a 'form' tag assert_tag :tag => 'form' # there is not an 'hr' tag assert_no_tag :tag => 'hr' end def test_assert_tag_attributes process :test_html_output # there is a tag with an 'id' of 'bar' assert_tag :attributes => { :id => "bar" } # there is no tag with a 'name' of 'baz' assert_no_tag :attributes => { :name => "baz" } end def test_assert_tag_parent process :test_html_output # there is a tag with a parent 'form' tag assert_tag :parent => { :tag => "form" } # there is no tag with a parent of 'input' assert_no_tag :parent => { :tag => "input" } end def test_assert_tag_child process :test_html_output # there is a tag with a child 'input' tag assert_tag :child => { :tag => "input" } # there is no tag with a child 'strong' tag assert_no_tag :child => { :tag => "strong" } end def test_assert_tag_ancestor process :test_html_output # there is a 'li' tag with an ancestor having an id of 'foo' assert_tag :ancestor => { :attributes => { :id => "foo" } }, :tag => "li" # there is no tag of any kind with an ancestor having an href matching 'foo' assert_no_tag :ancestor => { :attributes => { :href => /foo/ } } end def test_assert_tag_descendant process :test_html_output # there is a tag with a descendant 'li' tag assert_tag :descendant => { :tag => "li" } # there is no tag with a descendant 'html' tag assert_no_tag :descendant => { :tag => "html" } end def test_assert_tag_sibling process :test_html_output # there is a tag with a sibling of class 'item' assert_tag :sibling => { :attributes => { :class => "item" } } # there is no tag with a sibling 'ul' tag assert_no_tag :sibling => { :tag => "ul" } end def test_assert_tag_after process :test_html_output # there is a tag following a sibling 'div' tag assert_tag :after => { :tag => "div" } # there is no tag following a sibling tag with id 'bar' assert_no_tag :after => { :attributes => { :id => "bar" } } end def test_assert_tag_before process :test_html_output # there is a tag preceding a tag with id 'bar' assert_tag :before => { :attributes => { :id => "bar" } } # there is no tag preceding a 'form' tag assert_no_tag :before => { :tag => "form" } end def test_assert_tag_children_count process :test_html_output # there is a tag with 2 children assert_tag :children => { :count => 2 } # in particular, there is a