require "#{File.dirname(__FILE__)}/../abstract_unit" require "#{File.dirname(__FILE__)}/fake_controllers" class TestTest < Test::Unit::TestCase class TestController < ActionController::Base def set_flash flash["test"] = ">#{flash["test"]}<" render :text => 'ignore me' end def set_session session['string'] = 'A wonder' session[:symbol] = 'it works' render :text => 'Success' end def render_raw_post raise Test::Unit::AssertionFailedError, "#raw_post is blank" if request.raw_post.blank? render :text => request.raw_post end def test_params render :text => params.inspect end def test_uri render :text => request.request_uri end def test_html_output render :text => <
Name:
HTML 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 redirect_to_symbol redirect_to :generate_url, :id => 5 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 headers['Location'] = 'created resource' head :created 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::Routes.reload ActionController::Routing.use_controllers! %w(content admin/user test_test/test) end def teardown ActionController::Routing::Routes.reload end def test_raw_post_handling params = {:page => {:name => 'page name'}, 'some key' => 123} get :render_raw_post, params.dup raw_post = params.map {|k,v| [CGI::escape(k.to_s), CGI::escape(v.to_s)].join('=')}.sort.join('&') assert_equal raw_post, @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_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_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_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 decendant '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 preceeding a tag with id 'bar' assert_tag :before => { :attributes => { :id => "bar" } } # there is no tag preceeding 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