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 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 => <
hello world
" assert_tag :tag => "p", :content => "hello world" assert_tag :tag => "p", :content => /hello/ assert_no_tag :tag => "p", :content => "hello" 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"}, {} end def test_assert_routing assert_routing 'content', :controller => 'content', :action => 'index' end def test_assert_routing_with_method with_routing do |set| set.draw { |map| map.resources(:content) } assert_routing({ :method => 'post', :path => 'content' }, { :controller => 'content', :action => 'create' }) end end def test_assert_routing_in_module assert_routing 'admin/user', :controller => 'admin/user', :action => 'index' end def test_params_passing get :test_params, :page => {:name => "Page name", :month => '4', :year => '2004', :day => '6'} parsed_params = eval(@response.body) assert_equal( {'controller' => 'test_test/test', 'action' => 'test_params', 'page' => {'name' => "Page name", 'month' => '4', 'year' => '2004', 'day' => '6'}}, parsed_params ) end def test_id_converted_to_string 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 |map| map.connect 'file/*path', :controller => 'test_test/test', :action => 'test_params' map.connect ':controller/:action/:id' end get :test_params, :path => ['hello', 'world'] assert_equal ['hello', 'world'], @request.path_parameters['path'] assert_equal 'hello/world', @request.path_parameters['path'].to_s end end def test_assert_realistic_path_parameters get :test_params, :id => 20, :foo => Object.new # All elements of path_parameters should use string keys @request.path_parameters.keys.each do |key| assert_kind_of String, key end end def test_with_routing_places_routes_back assert ActionController::Routing::Routes routes_id = ActionController::Routing::Routes.object_id begin with_routing { raise 'fail' } fail 'Should not be here.' rescue RuntimeError end assert ActionController::Routing::Routes assert_equal routes_id, ActionController::Routing::Routes.object_id end def test_remote_addr get :test_remote_addr assert_equal "0.0.0.0", @response.body @request.remote_addr = "192.0.0.1" get :test_remote_addr assert_equal "192.0.0.1", @response.body end def test_header_properly_reset_after_remote_http_request xhr :get, :test_params assert_nil @request.env['HTTP_X_REQUESTED_WITH'] end def test_header_properly_reset_after_get_request get :test_params @request.recycle! assert_nil @request.instance_variable_get("@request_method") end %w(controller response request).each do |variable| %w(get post put delete head process).each do |method| define_method("test_#{variable}_missing_for_#{method}_raises_error") do remove_instance_variable "@#{variable}" begin send(method, :test_remote_addr) assert false, "expected RuntimeError, got nothing" rescue RuntimeError => error assert true assert_match %r{@#{variable} is nil}, error.message rescue => error assert false, "expected RuntimeError, got #{error.class}" end end end end FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart' if RUBY_VERSION < '1.9' READ_BINARY = 'rb' READ_PLAIN = 'r' else READ_BINARY = 'rb:binary' READ_PLAIN = 'r:binary' end def test_test_uploaded_file filename = 'mona_lisa.jpg' path = "#{FILES_DIR}/#{filename}" content_type = 'image/png' expected = File.read(path) expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding) file = ActionController::TestUploadedFile.new(path, content_type) assert_equal filename, file.original_filename assert_equal content_type, file.content_type assert_equal file.path, file.local_path assert_equal expected, file.read new_content_type = "new content_type" file.content_type = new_content_type assert_equal new_content_type, file.content_type end def test_test_uploaded_file_with_binary filename = 'mona_lisa.jpg' path = "#{FILES_DIR}/#{filename}" content_type = 'image/png' binary_uploaded_file = ActionController::TestUploadedFile.new(path, content_type, :binary) assert_equal File.open(path, READ_BINARY).read, binary_uploaded_file.read plain_uploaded_file = ActionController::TestUploadedFile.new(path, content_type) assert_equal File.open(path, READ_PLAIN).read, plain_uploaded_file.read end def test_fixture_file_upload_with_binary filename = 'mona_lisa.jpg' path = "#{FILES_DIR}/#{filename}" 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 plain_file_upload = fixture_file_upload(path, content_type) assert_equal File.open(path, READ_PLAIN).read, plain_file_upload.read end def test_fixture_file_upload post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") assert_equal '159528', @response.body end def test_test_uploaded_file_exception_when_file_doesnt_exist assert_raise(RuntimeError) { ActionController::TestUploadedFile.new('non_existent_file') } end def test_redirect_url_only_cares_about_location_header get :create assert_response :created # Redirect url doesn't care that it wasn't a :redirect response. 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' end end def test_binary_content_works_with_send_file get :test_send_file assert_nothing_raised(NoMethodError) { @response.binary_content } end protected def with_foo_routing with_routing do |set| set.draw do |map| map.generate_url 'foo', :controller => 'test' map.connect ':controller/:action/:id' end yield set end end end class CleanBacktraceTest < ActionController::TestCase def test_should_reraise_the_same_object exception = ActiveSupport::TestCase::Assertion.new('message') clean_backtrace { raise exception } rescue Exception => caught assert_equal exception.object_id, caught.object_id assert_equal exception.message, caught.message end def test_should_clean_assertion_lines_from_backtrace path = File.expand_path("#{File.dirname(__FILE__)}/../../lib/action_controller/testing") exception = ActiveSupport::TestCase::Assertion.new('message') exception.set_backtrace ["#{path}/abc", "#{path}/assertions/def"] clean_backtrace { raise exception } rescue Exception => caught assert_equal ["#{path}/abc"], caught.backtrace end def test_should_only_clean_assertion_failure_errors clean_backtrace do raise "can't touch this", [File.expand_path("#{File.dirname(__FILE__)}/../../lib/action_controller/assertions/abc")] end rescue => caught assert !caught.backtrace.empty? end end class InferringClassNameTest < ActionController::TestCase def test_determine_controller_class assert_equal ContentController, determine_class("ContentControllerTest") end def test_determine_controller_class_with_nonsense_name assert_nil determine_class("HelloGoodBye") end def test_determine_controller_class_with_sensible_name_where_no_controller_exists assert_nil determine_class("NoControllerWithThisNameTest") end private def determine_class(name) ActionController::TestCase.determine_default_controller_class(name) end end class CrazyNameTest < ActionController::TestCase tests ContentController def test_controller_class_can_be_set_manually_not_just_inferred assert_equal ContentController, self.class.controller_class end end class NamedRoutesControllerTest < ActionController::TestCase tests ContentController def test_should_be_able_to_use_named_routes_before_a_request_is_done with_routing do |set| set.draw { |map| map.resources :contents } assert_equal 'http://test.host/contents/new', new_content_url assert_equal 'http://test.host/contents/1', content_url(:id => 1) end end end