require 'abstract_unit' require 'controller/fake_controllers' class TestTest < 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.request_uri end def test_query_string render :text => request.query_string 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 private def rescue_action(e) raise e end def generate_url(opts) url_for(opts.merge(:action => "test_uri")) end end def setup @controller = TestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new ActionController::Routing.use_controllers! %w(content admin/user test_test/test) ActionController::Routing::Routes.load_routes! end def teardown ActionController::Routing::Routes.reload end def test_raw_post_handling params = {: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 = { :page => { :name => 'page name' }, 'some key' => 123 } post :render_body, params.dup assert_equal params.to_query, @response.body end def test_process_without_flash process :set_flash assert_equal '><', flash['test'] end def test_process_with_flash process :set_flash, nil, nil, { "test" => "value" } assert_equal '>value<', flash['test'] end def test_process_with_flash_now process :set_flash_now, 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, 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_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_response_after_reset_session process :set_session process :reset_the_session assert_equal Hash.new, @response.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_process_with_request_uri_with_no_params process :test_uri assert_equal "/test_test/test/test_uri", @response.body end def test_process_with_request_uri_with_params process :test_uri, :id => 7 assert_equal "/test_test/test/test_uri/7", @response.body end def test_process_with_request_uri_with_params_with_explicit_uri @request.set_REQUEST_URI "/explicit/uri" process :test_uri, :id => 7 assert_equal "/explicit/uri", @response.body end def test_process_with_query_string process :test_query_string, :q => 'test' assert_equal "q=test", @response.body end def test_process_with_query_string_with_explicit_uri @request.set_REQUEST_URI "/explicit/uri?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, :left => true assert_equal "OK", @response.body process :test_only_one_param, :right => true assert_equal "OK", @response.body 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