require 'abstract_unit' require 'controller/fake_controllers' require 'active_support/ordered_hash' 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.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_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 { resources(:content) } assert_routing({ :method => 'post', :path => 'content' }, { :controller => 'content', :action => 'create' }) end end def test_assert_routing_in_module with_routing do |set| set.draw do namespace :admin do match 'user' => 'user#index' end end assert_routing 'admin/user', :controller => 'admin/user', :action => 'index' end end def test_assert_routing_with_glob with_routing do |set| set.draw { match('*path' => "pages#show") } assert_routing('/company/about', { :controller => 'pages', :action => 'show', :path => 'company/about' }) end 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_params_passing_with_fixnums 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_params_passing_with_fixnums_when_not_html_request get :test_params, :format => 'json', :count => 999 parsed_params = eval(@response.body) assert_equal( {'controller' => 'test_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, :format => 'json', :id => 1 parsed_params = eval(@response.body) assert_equal( {'controller' => 'test_test/test', 'action' => 'test_params', 'format' => 'json', 'id' => '1' }, parsed_params ) end def test_params_passing_with_frozen_values assert_nothing_raised do get :test_params, :frozen => 'icy'.freeze, :frozens => ['icy'.freeze].freeze, :deepfreeze => { :frozen => 'icy'.freeze }.freeze end parsed_params = eval(@response.body) assert_equal( {'controller' => 'test_test/test', 'action' => 'test_params', 'frozen' => 'icy', 'frozens' => ['icy'], 'deepfreeze' => { 'frozen' => 'icy' }}, parsed_params ) end def test_params_passing_doesnt_modify_in_place page = {:name => "Page name", :month => 4, :year => 2004, :day => 6} get :test_params, :page => page assert_equal 2004, page[:year] 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 match 'file/*path', :to => 'test_test/test#test_params' match ':controller/:action' 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 @routes routes_id = @routes.object_id begin with_routing { raise 'fail' } fail 'Should not be here.' rescue RuntimeError end assert @routes assert_equal routes_id, @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 def test_params_reset_after_post_request post :no_op, :foo => "bar" assert_equal "bar", @request.params[:foo] @request.recycle! post :no_op assert_blank @request.params[:foo] end def test_symbolized_path_params_reset_after_request get :test_params, :id => "foo" assert_equal "foo", @request.symbolized_path_parameters[:id] @request.recycle! get :test_params assert_nil @request.symbolized_path_parameters[:id] end def test_request_protocol_is_reset_after_request get :test_protocol assert_equal "http://", @response.body @request.env["HTTPS"] = "on" get :test_protocol assert_equal "https://", @response.body @request.env.delete("HTTPS") get :test_protocol assert_equal "http://", @response.body end def test_request_format 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 => 'xml' assert_equal 'application/xml', @response.body get :test_format assert_equal 'text/html', @response.body end def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set cookies['foo'] = 'bar' get :no_op assert_equal 'bar', cookies['foo'] end def test_should_detect_if_cookie_is_deleted cookies['foo'] = 'bar' get :delete_cookie assert_nil cookies['foo'] 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_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 = Rack::Test::UploadedFile.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_fixture_path_is_accessed_from_self_instead_of_active_support_test_case TestTest.stubs(:fixture_path).returns(FILES_DIR) 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 end def test_test_uploaded_file_with_binary filename = 'mona_lisa.jpg' path = "#{FILES_DIR}/#{filename}" 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 plain_uploaded_file = Rack::Test::UploadedFile.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_action_dispatch_uploaded_file_upload filename = 'mona_lisa.jpg' path = "#{FILES_DIR}/#{filename}" post :test_file_upload, :file => ActionDispatch::Http::UploadedFile.new(:filename => path, :type => "image/jpg", :tempfile => File.open(path)) assert_equal '159528', @response.body end def test_test_uploaded_file_exception_when_file_doesnt_exist assert_raise(RuntimeError) { Rack::Test::UploadedFile.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 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 CrazySymbolNameTest < ActionController::TestCase tests :content def test_set_controller_class_using_symbol assert_equal ContentController, self.class.controller_class end end class CrazyStringNameTest < ActionController::TestCase tests 'content' def test_set_controller_class_using_string 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 { 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