diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-10-10 17:15:11 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-10-10 17:15:11 +0100 |
commit | 66ee2654ff243f03595a402fa15e1eea1b5b45be (patch) | |
tree | 3f1055e03082f0c767719e8cba5155e4207779e0 /actionpack/test | |
parent | dd2779e1b83b4d867d47dd286ec0c919f5df12a9 (diff) | |
parent | b9ce8216fa849a47ad0b0f99fa510e226a23c12e (diff) | |
download | rails-66ee2654ff243f03595a402fa15e1eea1b5b45be.tar.gz rails-66ee2654ff243f03595a402fa15e1eea1b5b45be.tar.bz2 rails-66ee2654ff243f03595a402fa15e1eea1b5b45be.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'actionpack/test')
58 files changed, 552 insertions, 396 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index aef3dd6165..4820f00aa1 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -51,23 +51,73 @@ I18n.backend.store_translations 'pt-BR', {} ORIGINAL_LOCALES = I18n.available_locales.map {|locale| locale.to_s }.sort FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures') +FIXTURES = Pathname.new(FIXTURE_LOAD_PATH) -class ActionController::IntegrationTest < ActiveSupport::TestCase - @@app = ActionDispatch::MiddlewareStack.new { |middleware| - middleware.use "ActionDispatch::ShowExceptions" - middleware.use "ActionDispatch::Callbacks" - middleware.use "ActionDispatch::ParamsParser" - middleware.use "Rack::Head" - }.build(ActionController::Routing::Routes) -end +module SetupOnce + extend ActiveSupport::Concern -module ActionView - class TestCase - setup do - ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action/:id' + included do + cattr_accessor :setup_once_block + self.setup_once_block = nil + + setup :run_setup_once + end + + module ClassMethods + def setup_once(&block) + self.setup_once_block = block + end + end + + private + def run_setup_once + if self.setup_once_block + self.setup_once_block.call + self.setup_once_block = nil end end +end + +class ActiveSupport::TestCase + include SetupOnce + + # Hold off drawing routes until all the possible controller classes + # have been loaded. + setup_once do + ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action/:id' + end + end +end + +class ActionController::IntegrationTest < ActiveSupport::TestCase + def self.build_app(routes = nil) + ActionDispatch::MiddlewareStack.new { |middleware| + middleware.use "ActionDispatch::StringCoercion" + middleware.use "ActionDispatch::ShowExceptions" + middleware.use "ActionDispatch::Callbacks" + middleware.use "ActionDispatch::ParamsParser" + middleware.use "Rack::Head" + }.build(routes || ActionController::Routing::Routes) + end + + self.app = build_app + + def with_routing(&block) + real_routes = ActionController::Routing::Routes + ActionController::Routing.module_eval { remove_const :Routes } + + temporary_routes = ActionController::Routing::RouteSet.new + self.class.app = self.class.build_app(temporary_routes) + ActionController::Routing.module_eval { const_set :Routes, temporary_routes } + + yield temporary_routes + ensure + if ActionController::Routing.const_defined? :Routes + ActionController::Routing.module_eval { remove_const :Routes } + end + ActionController::Routing.const_set(:Routes, real_routes) if real_routes + self.class.app = self.class.build_app end end @@ -138,18 +188,12 @@ module ActionController super end end - + Base.view_paths = FIXTURE_LOAD_PATH - + class TestCase include TestProcess - setup do - ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action/:id' - end - end - def assert_template(options = {}, message = nil) validate_request! @@ -192,11 +236,3 @@ module ActionController end end end - -class SimpleRouteCase < Rack::TestCase - setup do - ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action/:id' - end - end -end diff --git a/actionpack/test/activerecord/active_record_store_test.rb b/actionpack/test/activerecord/active_record_store_test.rb index 102b9cffdd..c6c079f88c 100644 --- a/actionpack/test/activerecord/active_record_store_test.rb +++ b/actionpack/test/activerecord/active_record_store_test.rb @@ -158,7 +158,6 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest map.connect "/:action", :controller => "active_record_store_test/test" end @app = ActiveRecord::SessionStore.new(set, options.reverse_merge(:key => '_session_id')) - reset! yield end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 1a9f95e5e9..69b0eb5e3e 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -46,14 +46,8 @@ end class PageCachingTest < ActionController::TestCase def setup super - ActionController::Base.perform_caching = true - ActionController::Routing::Routes.draw do |map| - map.main '', :controller => 'posts', :format => nil - map.formatted_posts 'posts.:format', :controller => 'posts' - map.resources :posts - map.connect ':controller/:action/:id' - end + ActionController::Base.perform_caching = true @request = ActionController::TestRequest.new @request.host = 'hostname.com' @@ -74,10 +68,16 @@ class PageCachingTest < ActionController::TestCase end def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route - @params[:format] = 'rss' - assert_equal '/posts.rss', @rewriter.rewrite(@params) - @params[:format] = nil - assert_equal '/', @rewriter.rewrite(@params) + with_routing do |set| + set.draw do |map| + map.main '', :controller => 'posts', :format => nil + map.formatted_posts 'posts.:format', :controller => 'posts' + end + @params[:format] = 'rss' + assert_equal '/posts.rss', @rewriter.rewrite(@params) + @params[:format] = nil + assert_equal '/', @rewriter.rewrite(@params) + end end def test_should_cache_get_with_ok_status diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 0e4ca21143..508364d0b5 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -372,11 +372,8 @@ class IntegrationProcessTest < ActionController::IntegrationTest def with_test_route_set with_routing do |set| set.draw do |map| - map.with_options :controller => "IntegrationProcessTest::Integration" do |c| - c.connect "/:action" - end + map.connect "/:action", :controller => "integration_process_test/integration" end - reset! yield end end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 93a815adae..a79648396c 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -527,12 +527,6 @@ class RespondWithControllerTest < ActionController::TestCase super ActionController::Base.use_accept_header = true @request.host = "www.example.com" - - ActionController::Routing::Routes.draw do |map| - map.resources :customers - map.resources :quiz_stores, :has_many => :customers - map.connect ":controller/:action/:id" - end end def teardown @@ -593,53 +587,59 @@ class RespondWithControllerTest < ActionController::TestCase end def test_using_resource_for_post_with_html - post :using_resource - assert_equal "text/html", @response.content_type - assert_equal 302, @response.status - assert_equal "http://www.example.com/customers/13", @response.location - assert @response.redirect? - - errors = { :name => :invalid } - Customer.any_instance.stubs(:errors).returns(errors) - post :using_resource - assert_equal "text/html", @response.content_type - assert_equal 200, @response.status - assert_equal "New world!\n", @response.body - assert_nil @response.location + with_test_route_set do + post :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers/13", @response.location + assert @response.redirect? + + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + post :using_resource + assert_equal "text/html", @response.content_type + assert_equal 200, @response.status + assert_equal "New world!\n", @response.body + assert_nil @response.location + end end def test_using_resource_for_post_with_xml - @request.accept = "application/xml" - - post :using_resource - assert_equal "application/xml", @response.content_type - assert_equal 201, @response.status - assert_equal "<name>david</name>", @response.body - assert_equal "http://www.example.com/customers/13", @response.location - - errors = { :name => :invalid } - Customer.any_instance.stubs(:errors).returns(errors) - post :using_resource - assert_equal "application/xml", @response.content_type - assert_equal 422, @response.status - assert_equal errors.to_xml, @response.body - assert_nil @response.location + with_test_route_set do + @request.accept = "application/xml" + + post :using_resource + assert_equal "application/xml", @response.content_type + assert_equal 201, @response.status + assert_equal "<name>david</name>", @response.body + assert_equal "http://www.example.com/customers/13", @response.location + + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + post :using_resource + assert_equal "application/xml", @response.content_type + assert_equal 422, @response.status + assert_equal errors.to_xml, @response.body + assert_nil @response.location + end end def test_using_resource_for_put_with_html - put :using_resource - assert_equal "text/html", @response.content_type - assert_equal 302, @response.status - assert_equal "http://www.example.com/customers/13", @response.location - assert @response.redirect? - - errors = { :name => :invalid } - Customer.any_instance.stubs(:errors).returns(errors) - put :using_resource - assert_equal "text/html", @response.content_type - assert_equal 200, @response.status - assert_equal "Edit world!\n", @response.body - assert_nil @response.location + with_test_route_set do + put :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers/13", @response.location + assert @response.redirect? + + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + put :using_resource + assert_equal "text/html", @response.content_type + assert_equal 200, @response.status + assert_equal "Edit world!\n", @response.body + assert_nil @response.location + end end def test_using_resource_for_put_with_xml @@ -660,11 +660,13 @@ class RespondWithControllerTest < ActionController::TestCase end def test_using_resource_for_delete_with_html - Customer.any_instance.stubs(:destroyed?).returns(true) - delete :using_resource - assert_equal "text/html", @response.content_type - assert_equal 302, @response.status - assert_equal "http://www.example.com/customers", @response.location + with_test_route_set do + Customer.any_instance.stubs(:destroyed?).returns(true) + delete :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers", @response.location + end end def test_using_resource_for_delete_with_xml @@ -685,21 +687,23 @@ class RespondWithControllerTest < ActionController::TestCase end def test_using_resource_with_parent_for_post - @request.accept = "application/xml" - - post :using_resource_with_parent - assert_equal "application/xml", @response.content_type - assert_equal 201, @response.status - assert_equal "<name>david</name>", @response.body - assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location - - errors = { :name => :invalid } - Customer.any_instance.stubs(:errors).returns(errors) - post :using_resource - assert_equal "application/xml", @response.content_type - assert_equal 422, @response.status - assert_equal errors.to_xml, @response.body - assert_nil @response.location + with_test_route_set do + @request.accept = "application/xml" + + post :using_resource_with_parent + assert_equal "application/xml", @response.content_type + assert_equal 201, @response.status + assert_equal "<name>david</name>", @response.body + assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location + + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + post :using_resource + assert_equal "application/xml", @response.content_type + assert_equal 422, @response.status + assert_equal errors.to_xml, @response.body + assert_nil @response.location + end end def test_using_resource_with_collection @@ -773,6 +777,18 @@ class RespondWithControllerTest < ActionController::TestCase get :default_overwritten assert_equal 406, @response.status end + + private + def with_test_route_set + with_routing do |set| + set.draw do |map| + map.resources :customers + map.resources :quiz_stores, :has_many => :customers + map.connect ":controller/:action/:id" + end + yield + end + end end class AbstractPostController < ActionController::Base diff --git a/actionpack/test/new_base/base_test.rb b/actionpack/test/controller/new_base/base_test.rb index effde324bc..1f9bf7f0fb 100644 --- a/actionpack/test/new_base/base_test.rb +++ b/actionpack/test/controller/new_base/base_test.rb @@ -26,7 +26,7 @@ module Dispatching class ContainedEmptyController < ActionController::Base ; end end - class BaseTest < SimpleRouteCase + class BaseTest < Rack::TestCase # :api: plugin test "simple dispatching" do get "/dispatching/simple/index" diff --git a/actionpack/test/new_base/content_negotiation_test.rb b/actionpack/test/controller/new_base/content_negotiation_test.rb index c43cb677f8..7b38a82f51 100644 --- a/actionpack/test/new_base/content_negotiation_test.rb +++ b/actionpack/test/controller/new_base/content_negotiation_test.rb @@ -9,7 +9,7 @@ module ContentNegotiation )] end - class TestContentNegotiation < SimpleRouteCase + class TestContentNegotiation < Rack::TestCase test "A */* Accept header will return HTML" do get "/content_negotiation/basic/hello", {}, "HTTP_ACCEPT" => "*/*" assert_body "Hello world */*!" diff --git a/actionpack/test/new_base/content_type_test.rb b/actionpack/test/controller/new_base/content_type_test.rb index 898d0bb9f3..0ff5552b08 100644 --- a/actionpack/test/new_base/content_type_test.rb +++ b/actionpack/test/controller/new_base/content_type_test.rb @@ -44,7 +44,7 @@ module ContentType end end - class ExplicitContentTypeTest < SimpleRouteCase + class ExplicitContentTypeTest < Rack::TestCase test "default response is HTML and UTF8" do get "/content_type/base" @@ -67,7 +67,7 @@ module ContentType end end - class ImpliedContentTypeTest < SimpleRouteCase + class ImpliedContentTypeTest < Rack::TestCase test "sets Content-Type as text/html when rendering *.html.erb" do get "/content_type/implied/i_am_html_erb" @@ -93,7 +93,7 @@ module ContentType end end - class ExplicitCharsetTest < SimpleRouteCase + class ExplicitCharsetTest < Rack::TestCase test "setting the charset of the response directly on the response object" do get "/content_type/charset/set_on_response_obj" diff --git a/actionpack/test/new_base/etag_test.rb b/actionpack/test/controller/new_base/etag_test.rb index d5b7942ab6..51bfb2278a 100644 --- a/actionpack/test/new_base/etag_test.rb +++ b/actionpack/test/controller/new_base/etag_test.rb @@ -16,7 +16,7 @@ module Etags end end - class EtagTest < SimpleRouteCase + class EtagTest < Rack::TestCase describe "Rendering without any special etag options returns an etag that is an MD5 hash of its text" test "an action without a layout" do diff --git a/actionpack/test/new_base/metal_test.rb b/actionpack/test/controller/new_base/metal_test.rb index e1d46b906e..e1d46b906e 100644 --- a/actionpack/test/new_base/metal_test.rb +++ b/actionpack/test/controller/new_base/metal_test.rb diff --git a/actionpack/test/new_base/middleware_test.rb b/actionpack/test/controller/new_base/middleware_test.rb index ada0215b1a..ada0215b1a 100644 --- a/actionpack/test/new_base/middleware_test.rb +++ b/actionpack/test/controller/new_base/middleware_test.rb diff --git a/actionpack/test/new_base/render_action_test.rb b/actionpack/test/controller/new_base/render_action_test.rb index d5896c1ebd..ecd29c4530 100644 --- a/actionpack/test/new_base/render_action_test.rb +++ b/actionpack/test/controller/new_base/render_action_test.rb @@ -45,7 +45,7 @@ module RenderAction end - class RenderActionTest < SimpleRouteCase + class RenderActionTest < Rack::TestCase test "rendering an action using :action => <String>" do get "/render_action/basic/hello_world" @@ -82,7 +82,7 @@ module RenderAction end end - class RenderLayoutTest < SimpleRouteCase + class RenderLayoutTest < Rack::TestCase describe "Both <controller_path>.html.erb and application.html.erb are missing" test "rendering with layout => true" do @@ -150,7 +150,7 @@ module RenderActionWithApplicationLayout end end - class LayoutTest < SimpleRouteCase + class LayoutTest < Rack::TestCase describe "Only application.html.erb is present and <controller_path>.html.erb is missing" test "rendering implicit application.html.erb as layout" do @@ -189,7 +189,7 @@ module RenderActionWithApplicationLayout end end - class TestLayout < SimpleRouteCase + class TestLayout < Rack::TestCase testing BasicController test "builder works with layouts" do @@ -228,7 +228,7 @@ module RenderActionWithControllerLayout end end - class ControllerLayoutTest < SimpleRouteCase + class ControllerLayoutTest < Rack::TestCase describe "Only <controller_path>.html.erb is present and application.html.erb is missing" test "render hello_world and implicitly use <controller_path>.html.erb as a layout." do @@ -286,7 +286,7 @@ module RenderActionWithBothLayouts end end - class ControllerLayoutTest < SimpleRouteCase + class ControllerLayoutTest < Rack::TestCase describe "Both <controller_path>.html.erb and application.html.erb are present" test "rendering implicitly use <controller_path>.html.erb over application.html.erb as a layout" do diff --git a/actionpack/test/new_base/render_file_test.rb b/actionpack/test/controller/new_base/render_file_test.rb index c4098855e6..8b2fdf8f96 100644 --- a/actionpack/test/new_base/render_file_test.rb +++ b/actionpack/test/controller/new_base/render_file_test.rb @@ -1,110 +1,99 @@ require 'abstract_unit' module RenderFile - class BasicController < ActionController::Base - self.view_paths = File.dirname(__FILE__) - + self.view_paths = File.dirname(__FILE__) + def index - render :file => File.join(File.dirname(__FILE__), *%w[.. fixtures test hello_world]) + render :file => File.join(File.dirname(__FILE__), *%w[.. .. fixtures test hello_world]) end - + def with_instance_variables @secret = 'in the sauce' - render :file => File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb') + render :file => File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_ivar.erb') end - + def without_file_key - render File.join(File.dirname(__FILE__), *%w[.. fixtures test hello_world]) + render File.join(File.dirname(__FILE__), *%w[.. .. fixtures test hello_world]) end - + def without_file_key_with_instance_variable @secret = 'in the sauce' - render File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb') + render File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_ivar.erb') end - + def relative_path @secret = 'in the sauce' - render :file => '../fixtures/test/render_file_with_ivar' + render :file => '../../fixtures/test/render_file_with_ivar' end - + def relative_path_with_dot @secret = 'in the sauce' - render :file => '../fixtures/test/dot.directory/render_file_with_ivar' + render :file => '../../fixtures/test/dot.directory/render_file_with_ivar' end - + def pathname @secret = 'in the sauce' - render :file => Pathname.new(File.dirname(__FILE__)).join(*%w[.. fixtures test dot.directory render_file_with_ivar.erb]) + render :file => Pathname.new(File.dirname(__FILE__)).join(*%w[.. .. fixtures test dot.directory render_file_with_ivar.erb]) end - + def with_locals - path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb') + path = File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_locals.erb') render :file => path, :locals => {:secret => 'in the sauce'} end - + def without_file_key_with_locals - path = File.expand_path('../fixtures/test/render_file_with_locals.erb') + path = FIXTURES.join('test/render_file_with_locals.erb').to_s render path, :locals => {:secret => 'in the sauce'} end end - - class TestBasic < SimpleRouteCase + + class TestBasic < Rack::TestCase testing RenderFile::BasicController - - def setup - @old_pwd = Dir.pwd - Dir.chdir(File.dirname(__FILE__)) - end - - def teardown - Dir.chdir(@old_pwd) - end - + test "rendering simple template" do get :index assert_response "Hello world!" end - + test "rendering template with ivar" do get :with_instance_variables assert_response "The secret is in the sauce\n" end - + test "rendering path without specifying the :file key" do get :without_file_key assert_response "Hello world!" end - + test "rendering path without specifying the :file key with ivar" do get :without_file_key_with_instance_variable assert_response "The secret is in the sauce\n" end - + test "rendering a relative path" do get :relative_path assert_response "The secret is in the sauce\n" end - + test "rendering a relative path with dot" do get :relative_path_with_dot assert_response "The secret is in the sauce\n" end - + test "rendering a Pathname" do get :pathname assert_response "The secret is in the sauce\n" end - + test "rendering file with locals" do get :with_locals assert_response "The secret is in the sauce\n" end - + test "rendering path without specifying the :file key with locals" do get :without_file_key_with_locals assert_response "The secret is in the sauce\n" end end - end diff --git a/actionpack/test/new_base/render_implicit_action_test.rb b/actionpack/test/controller/new_base/render_implicit_action_test.rb index 2b78fa7d4f..90cc7933ff 100644 --- a/actionpack/test/new_base/render_implicit_action_test.rb +++ b/actionpack/test/controller/new_base/render_implicit_action_test.rb @@ -10,7 +10,7 @@ module RenderImplicitAction def hello_world() end end - class RenderImplicitActionTest < SimpleRouteCase + class RenderImplicitActionTest < Rack::TestCase test "render a simple action with new explicit call to render" do get "/render_implicit_action/simple/hello_world" diff --git a/actionpack/test/new_base/render_layout_test.rb b/actionpack/test/controller/new_base/render_layout_test.rb index f840a47ecf..6a9668b81a 100644 --- a/actionpack/test/new_base/render_layout_test.rb +++ b/actionpack/test/controller/new_base/render_layout_test.rb @@ -36,7 +36,7 @@ module ControllerLayouts end end - class RenderLayoutTest < SimpleRouteCase + class RenderLayoutTest < Rack::TestCase test "rendering a normal template, but using the implicit layout" do get "/controller_layouts/implicit/index" @@ -58,7 +58,7 @@ module ControllerLayouts end - class LayoutOptionsTest < SimpleRouteCase + class LayoutOptionsTest < Rack::TestCase testing ControllerLayouts::ImplicitController test "rendering with :layout => false leaves out the implicit layout" do @@ -79,7 +79,7 @@ module ControllerLayouts end end - class MismatchFormatTest < SimpleRouteCase + class MismatchFormatTest < Rack::TestCase testing ControllerLayouts::MismatchFormatController test "if JS is selected, an HTML template is not also selected" do diff --git a/actionpack/test/new_base/render_partial_test.rb b/actionpack/test/controller/new_base/render_partial_test.rb index 7c2c20e1c7..8fddcbcd57 100644 --- a/actionpack/test/new_base/render_partial_test.rb +++ b/actionpack/test/controller/new_base/render_partial_test.rb @@ -15,7 +15,7 @@ module RenderPartial end end - class TestPartial < SimpleRouteCase + class TestPartial < Rack::TestCase testing BasicController test "rendering a partial in ActionView doesn't pull the ivars again from the controller" do diff --git a/actionpack/test/new_base/render_rjs_test.rb b/actionpack/test/controller/new_base/render_rjs_test.rb index 7b76c54ab9..8c47b38ab6 100644 --- a/actionpack/test/new_base/render_rjs_test.rb +++ b/actionpack/test/controller/new_base/render_rjs_test.rb @@ -21,7 +21,7 @@ module RenderRjs end end - class TestBasic < SimpleRouteCase + class TestBasic < Rack::TestCase testing BasicController def setup diff --git a/actionpack/test/new_base/render_template_test.rb b/actionpack/test/controller/new_base/render_template_test.rb index 3b24c2d75a..c81b951c0d 100644 --- a/actionpack/test/new_base/render_template_test.rb +++ b/actionpack/test/controller/new_base/render_template_test.rb @@ -39,7 +39,7 @@ module RenderTemplate end end - class TestWithoutLayout < SimpleRouteCase + class TestWithoutLayout < Rack::TestCase testing RenderTemplate::WithoutLayoutController test "rendering a normal template with full path without layout" do @@ -107,7 +107,7 @@ module RenderTemplate end end - class TestWithLayout < SimpleRouteCase + class TestWithLayout < Rack::TestCase describe "Rendering with :template using implicit or explicit layout" test "rendering with implicit layout" do @@ -158,7 +158,7 @@ module RenderTemplate end end - class TestTemplateRenderWithForwardSlash < SimpleRouteCase + class TestTemplateRenderWithForwardSlash < Rack::TestCase test "rendering a normal template with full path starting with a leading slash" do get "/render_template/compatibility/without_layout/with_forward_slash" diff --git a/actionpack/test/new_base/render_test.rb b/actionpack/test/controller/new_base/render_test.rb index 804be79d17..d985d9f9ad 100644 --- a/actionpack/test/new_base/render_test.rb +++ b/actionpack/test/controller/new_base/render_test.rb @@ -35,7 +35,7 @@ module Render end end - class RenderTest < SimpleRouteCase + class RenderTest < Rack::TestCase test "render with blank" do get "/render/blank_render" @@ -50,7 +50,7 @@ module Render end end - class TestOnlyRenderPublicActions < SimpleRouteCase + class TestOnlyRenderPublicActions < Rack::TestCase describe "Only public methods on actual controllers are callable actions" test "raises an exception when a method of Object is called" do @@ -66,7 +66,7 @@ module Render end end - class TestVariousObjectsAvailableInView < SimpleRouteCase + class TestVariousObjectsAvailableInView < Rack::TestCase test "The request object is accessible in the view" do get "/render/blank_render/access_request" assert_body "The request: GET" diff --git a/actionpack/test/new_base/render_text_test.rb b/actionpack/test/controller/new_base/render_text_test.rb index f5839ee16f..0e6f51c998 100644 --- a/actionpack/test/new_base/render_text_test.rb +++ b/actionpack/test/controller/new_base/render_text_test.rb @@ -62,7 +62,7 @@ module RenderText end end - class RenderTextTest < SimpleRouteCase + class RenderTextTest < Rack::TestCase describe "Rendering text using render :text" test "rendering text from a action with default options renders the text with the layout" do diff --git a/actionpack/test/new_base/render_xml_test.rb b/actionpack/test/controller/new_base/render_xml_test.rb index d044738a78..d044738a78 100644 --- a/actionpack/test/new_base/render_xml_test.rb +++ b/actionpack/test/controller/new_base/render_xml_test.rb diff --git a/actionpack/test/controller/output_escaping_test.rb b/actionpack/test/controller/output_escaping_test.rb new file mode 100644 index 0000000000..7332f3f1e3 --- /dev/null +++ b/actionpack/test/controller/output_escaping_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' + +class OutputEscapingTest < ActiveSupport::TestCase + + test "escape_html shouldn't die when passed nil" do + assert ERB::Util.h(nil).blank? + end + + test "escapeHTML should escape strings" do + assert_equal "<>"", ERB::Util.h("<>\"") + end + + test "escapeHTML shouldn't touch explicitly safe strings" do + # TODO this seems easier to compose and reason about, but + # this should be verified + assert_equal "<", ERB::Util.h("<".html_safe!) + end + +end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index abcc8bf384..2db524ca4b 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -1050,7 +1050,7 @@ class RenderTest < ActionController::TestCase def test_action_talk_to_layout get :action_talk_to_layout - assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body + assert_equal "<title>Talking to the layout</title>\n\nAction was here!", @response.body end # :addressed: diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb index 6ad708bba1..689359166f 100644 --- a/actionpack/test/controller/rescue_test.rb +++ b/actionpack/test/controller/rescue_test.rb @@ -347,7 +347,6 @@ class RescueTest < ActionController::IntegrationTest map.connect 'invalid', :controller => "rescue_test/test", :action => 'invalid' map.connect 'b00m', :controller => "rescue_test/test", :action => 'b00m' end - reset! yield end end diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 1aabf71cad..edf243337f 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -669,21 +669,13 @@ class LegacyRouteSetTests < Test::Unit::TestCase %w(GET POST PUT DELETE).each do |request_method| define_method("test_request_method_recognized_with_#{request_method}") do - begin - Object.const_set(:BooksController, Class.new(ActionController::Base)) - - setup_request_method_routes_for(request_method) - - assert_nothing_raised { rs.recognize(@request) } - assert_equal request_method.downcase, @request.path_parameters[:action] - ensure - Object.send(:remove_const, :BooksController) rescue nil - end + setup_request_method_routes_for(request_method) + assert_nothing_raised { rs.recognize(@request) } + assert_equal request_method.downcase, @request.path_parameters[:action] end end def test_recognize_array_of_methods - Object.const_set(:BooksController, Class.new(ActionController::Base)) rs.draw do |r| r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] } r.connect '/match', :controller => 'books', :action => 'not_get_or_post' @@ -701,13 +693,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase @request.request_uri = "/match" assert_nothing_raised { rs.recognize(@request) } assert_equal 'not_get_or_post', @request.path_parameters[:action] - ensure - Object.send(:remove_const, :BooksController) rescue nil end def test_subpath_recognized - Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) - rs.draw do |r| r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit' r.connect '/items/:id/:action', :controller => 'subpath_books' @@ -730,13 +718,9 @@ class LegacyRouteSetTests < Test::Unit::TestCase hash = rs.recognize_path "/posts/7" assert_not_nil hash assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]] - ensure - Object.send(:remove_const, :SubpathBooksController) rescue nil end def test_subpath_generated - Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) - rs.draw do |r| r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit' r.connect '/items/:id/:action', :controller => 'subpath_books' @@ -746,8 +730,6 @@ class LegacyRouteSetTests < Test::Unit::TestCase assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit") assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete") assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview") - ensure - Object.send(:remove_const, :SubpathBooksController) rescue nil end def test_failed_requirements_raises_exception_with_violated_requirements @@ -1122,8 +1104,6 @@ class RouteSetTest < ActiveSupport::TestCase end def test_recognize_with_conditions - Object.const_set(:PeopleController, Class.new) - set.draw do |map| map.with_options(:controller => "people") do |people| people.people "/people", :action => "index", :conditions => { :method => :get } @@ -1183,14 +1163,9 @@ class RouteSetTest < ActiveSupport::TestCase assert_equal [:get, :put, :delete], e.allowed_methods end request.recycle! - - ensure - Object.send(:remove_const, :PeopleController) end def test_recognize_with_alias_in_conditions - Object.const_set(:PeopleController, Class.new) - set.draw do |map| map.people "/people", :controller => 'people', :action => "index", :conditions => { :method => :get } @@ -1208,13 +1183,9 @@ class RouteSetTest < ActiveSupport::TestCase assert_nothing_raised { set.recognize(request) } assert_equal("people", request.path_parameters[:controller]) assert_equal("index", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :PeopleController) end def test_typo_recognition - Object.const_set(:ArticlesController, Class.new) - set.draw do |map| map.connect 'articles/:year/:month/:day/:title', :controller => 'articles', :action => 'permalink', @@ -1229,9 +1200,6 @@ class RouteSetTest < ActiveSupport::TestCase assert_equal("11", request.path_parameters[:month]) assert_equal("05", request.path_parameters[:day]) assert_equal("a-very-interesting-article", request.path_parameters[:title]) - - ensure - Object.send(:remove_const, :ArticlesController) end def test_routing_traversal_does_not_load_extra_classes @@ -1248,8 +1216,6 @@ class RouteSetTest < ActiveSupport::TestCase end def test_recognize_with_conditions_and_format - Object.const_set(:PeopleController, Class.new) - set.draw do |map| map.with_options(:controller => "people") do |people| people.person "/people/:id", :action => "show", :conditions => { :method => :get } @@ -1276,8 +1242,6 @@ class RouteSetTest < ActiveSupport::TestCase assert_equal("show", request.path_parameters[:action]) assert_equal("5", request.path_parameters[:id]) assert_equal("png", request.path_parameters[:_format]) - ensure - Object.send(:remove_const, :PeopleController) end def test_generate_with_default_action @@ -1291,8 +1255,6 @@ class RouteSetTest < ActiveSupport::TestCase end def test_root_map - Object.const_set(:PeopleController, Class.new) - set.draw { |map| map.root :controller => "people" } request.path = "" @@ -1300,13 +1262,9 @@ class RouteSetTest < ActiveSupport::TestCase assert_nothing_raised { set.recognize(request) } assert_equal("people", request.path_parameters[:controller]) assert_equal("index", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :PeopleController) end def test_namespace - Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) - set.draw do |map| map.namespace 'api' do |api| @@ -1320,13 +1278,9 @@ class RouteSetTest < ActiveSupport::TestCase assert_nothing_raised { set.recognize(request) } assert_equal("api/products", request.path_parameters[:controller]) assert_equal("inventory", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :Api) end def test_namespaced_root_map - Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) - set.draw do |map| map.namespace 'api' do |api| @@ -1340,13 +1294,9 @@ class RouteSetTest < ActiveSupport::TestCase assert_nothing_raised { set.recognize(request) } assert_equal("api/products", request.path_parameters[:controller]) assert_equal("index", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :Api) end def test_namespace_with_path_prefix - Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) - set.draw do |map| map.namespace 'api', :path_prefix => 'prefix' do |api| api.route 'inventory', :controller => "products", :action => 'inventory' @@ -1358,13 +1308,9 @@ class RouteSetTest < ActiveSupport::TestCase assert_nothing_raised { set.recognize(request) } assert_equal("api/products", request.path_parameters[:controller]) assert_equal("inventory", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :Api) end def test_namespace_with_blank_path_prefix - Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) - set.draw do |map| map.namespace 'api', :path_prefix => '' do |api| api.route 'inventory', :controller => "products", :action => 'inventory' @@ -1376,8 +1322,6 @@ class RouteSetTest < ActiveSupport::TestCase assert_nothing_raised { set.recognize(request) } assert_equal("api/products", request.path_parameters[:controller]) assert_equal("inventory", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :Api) end def test_generate_changes_controller_module diff --git a/actionpack/test/controller/verification_test.rb b/actionpack/test/controller/verification_test.rb index ee558f3465..1a9eb65f29 100644 --- a/actionpack/test/controller/verification_test.rb +++ b/actionpack/test/controller/verification_test.rb @@ -111,13 +111,6 @@ class VerificationTest < ActionController::TestCase tests TestController - setup do - ActionController::Routing::Routes.draw do |map| - map.foo '/foo', :controller => 'test', :action => 'foo' - map.connect ":controller/:action/:id" - end - end - def test_using_symbol_back_with_no_referrer assert_raise(ActionController::RedirectBackError) { get :guarded_with_back } end @@ -130,8 +123,14 @@ class VerificationTest < ActionController::TestCase def test_no_deprecation_warning_for_named_route assert_not_deprecated do - get :guarded_one_for_named_route_test, :two => "not one" - assert_redirected_to '/foo' + with_routing do |set| + set.draw do |map| + map.foo '/foo', :controller => 'test', :action => 'foo' + map.connect ":controller/:action/:id" + end + get :guarded_one_for_named_route_test, :two => "not one" + assert_redirected_to '/foo' + end end end diff --git a/actionpack/test/controller/webservice_test.rb b/actionpack/test/controller/webservice_test.rb index c04d20fbad..0514c098bf 100644 --- a/actionpack/test/controller/webservice_test.rb +++ b/actionpack/test/controller/webservice_test.rb @@ -259,7 +259,6 @@ class WebServiceTest < ActionController::IntegrationTest c.connect "/", :action => "assign_parameters" end end - reset! yield end end diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index 995f36bb29..db6cf7b330 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -59,7 +59,6 @@ class JsonParamsParsingTest < ActionController::IntegrationTest set.draw do |map| map.connect ':action', :controller => "json_params_parsing_test/test" end - reset! yield end end diff --git a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb index d4ee4362eb..301080842e 100644 --- a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb @@ -153,7 +153,6 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest set.draw do |map| map.connect ':action', :controller => "multipart_params_parsing_test/test" end - reset! yield end end diff --git a/actionpack/test/dispatch/request/query_string_parsing_test.rb b/actionpack/test/dispatch/request/query_string_parsing_test.rb index 2261934e45..a31e326ddf 100644 --- a/actionpack/test/dispatch/request/query_string_parsing_test.rb +++ b/actionpack/test/dispatch/request/query_string_parsing_test.rb @@ -111,7 +111,6 @@ class QueryStringParsingTest < ActionController::IntegrationTest set.draw do |map| map.connect ':action', :controller => "query_string_parsing_test/test" end - reset! get "/parse", actual assert_response :ok diff --git a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb index 6c9967d26e..7167cdafac 100644 --- a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb @@ -132,7 +132,6 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest set.draw do |map| map.connect ':action', :controller => "url_encoded_params_parsing_test/test" end - reset! yield end end diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb index 2f2dd695c4..521002b519 100644 --- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb @@ -86,7 +86,6 @@ class XmlParamsParsingTest < ActionController::IntegrationTest set.draw do |map| map.connect ':action', :controller => "xml_params_parsing_test/test" end - reset! yield end end diff --git a/actionpack/test/dispatch/session/cookie_store_test.rb b/actionpack/test/dispatch/session/cookie_store_test.rb index 6241c79829..ab5fabde65 100644 --- a/actionpack/test/dispatch/session/cookie_store_test.rb +++ b/actionpack/test/dispatch/session/cookie_store_test.rb @@ -223,7 +223,6 @@ class CookieStoreTest < ActionController::IntegrationTest end options = {:key => SessionKey, :secret => SessionSecret}.merge(options) @app = ActionDispatch::Session::CookieStore.new(set, options) - reset! yield end end diff --git a/actionpack/test/dispatch/session/mem_cache_store_test.rb b/actionpack/test/dispatch/session/mem_cache_store_test.rb index c2d40ae24a..c7435bd06b 100644 --- a/actionpack/test/dispatch/session/mem_cache_store_test.rb +++ b/actionpack/test/dispatch/session/mem_cache_store_test.rb @@ -115,7 +115,6 @@ class MemCacheStoreTest < ActionController::IntegrationTest map.connect "/:action", :controller => "mem_cache_store_test/test" end @app = ActionDispatch::Session::MemCacheStore.new(set, :key => '_session_id') - reset! yield end end diff --git a/actionpack/test/dispatch/string_coercion_test.rb b/actionpack/test/dispatch/string_coercion_test.rb new file mode 100644 index 0000000000..d79b17b932 --- /dev/null +++ b/actionpack/test/dispatch/string_coercion_test.rb @@ -0,0 +1,40 @@ +require 'abstract_unit' + +class StringCoercionTest < ActiveSupport::TestCase + test "body responds to each" do + original_body = [] + body = ActionDispatch::StringCoercion::UglyBody.new(original_body) + + assert original_body.respond_to?(:each) + assert body.respond_to?(:each) + end + + test "body responds to to_path" do + original_body = [] + def original_body.to_path; end + body = ActionDispatch::StringCoercion::UglyBody.new(original_body) + + assert original_body.respond_to?(:to_path) + assert body.respond_to?(:to_path) + end + + test "body does not responds to to_path" do + original_body = [] + body = ActionDispatch::StringCoercion::UglyBody.new(original_body) + + assert !original_body.respond_to?(:to_path) + assert !body.respond_to?(:to_path) + end + + test "calls to_s on body parts" do + app = lambda { |env| + [200, {'Content-Type' => 'html'}, [1, 2, 3]] + } + app = ActionDispatch::StringCoercion.new(app) + parts = [] + status, headers, body = app.call({}) + body.each { |part| parts << part } + + assert_equal %w( 1 2 3 ), parts + end +end diff --git a/actionpack/test/lib/controller/fake_controllers.rb b/actionpack/test/lib/controller/fake_controllers.rb index 5dcca2e148..9ec7f330b8 100644 --- a/actionpack/test/lib/controller/fake_controllers.rb +++ b/actionpack/test/lib/controller/fake_controllers.rb @@ -1,22 +1,32 @@ class << Object; alias_method :const_available?, :const_defined?; end -class ContentController < ActionController::Base -end -class NotAController -end +class ContentController < ActionController::Base; end +class NotAController; end + module Admin class << self; alias_method :const_available?, :const_defined?; end class UserController < ActionController::Base; end class NewsFeedController < ActionController::Base; end end -class ElsewhereController < ActionController::Base; end + +module Api + class ProductsController < ActionController::Base; end +end + +# TODO: Reduce the number of test controllers we use class AddressesController < ActionController::Base; end -class SessionsController < ActionController::Base; end -class FooController < ActionController::Base; end +class ArticlesController < ActionController::Base; end +class BarController < ActionController::Base; end +class BooksController < ActionController::Base; end +class BraveController < ActionController::Base; end class CController < ActionController::Base; end +class ElsewhereController < ActionController::Base; end +class FooController < ActionController::Base; end class HiController < ActionController::Base; end -class BraveController < ActionController::Base; end class ImageController < ActionController::Base; end +class PeopleController < ActionController::Base; end +class SessionsController < ActionController::Base; end +class SubpathBooksController < ActionController::Base; end class WeblogController < ActionController::Base; end # For speed test @@ -34,8 +44,3 @@ class ChannelsController < SpeedController; end class ChannelVideosController < SpeedController; end class LostPasswordsController < SpeedController; end class PagesController < SpeedController; end - -ActionController::Routing::Routes.draw do |map| - map.route_one 'route_one', :controller => 'elsewhere', :action => 'flash_me' - map.connect ':controller/:action/:id' -end diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb index 18eff7516b..823de8bdc7 100644 --- a/actionpack/test/lib/controller/fake_models.rb +++ b/actionpack/test/lib/controller/fake_models.rb @@ -51,3 +51,99 @@ module Quiz end end +class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost) + extend ActiveModel::Naming + include ActiveModel::Conversion + + alias_method :secret?, :secret + + def new_record=(boolean) + @new_record = boolean + end + + def new_record? + @new_record + end + + attr_accessor :author + def author_attributes=(attributes); end + + attr_accessor :comments + def comments_attributes=(attributes); end + + attr_accessor :tags + def tags_attributes=(attributes); end +end + +class Comment + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + attr_reader :post_id + def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end + def save; @id = 1; @post_id = 1 end + def new_record?; @id.nil? end + def to_param; @id; end + def name + @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" + end + + attr_accessor :relevances + def relevances_attributes=(attributes); end + +end + +class Tag + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + attr_reader :post_id + def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end + def save; @id = 1; @post_id = 1 end + def new_record?; @id.nil? end + def to_param; @id; end + def value + @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" + end + + attr_accessor :relevances + def relevances_attributes=(attributes); end + +end + +class CommentRelevance + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + attr_reader :comment_id + def initialize(id = nil, comment_id = nil); @id, @comment_id = id, comment_id end + def save; @id = 1; @comment_id = 1 end + def new_record?; @id.nil? end + def to_param; @id; end + def value + @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" + end +end + +class TagRelevance + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + attr_reader :tag_id + def initialize(id = nil, tag_id = nil); @id, @tag_id = id, tag_id end + def save; @id = 1; @tag_id = 1 end + def new_record?; @id.nil? end + def to_param; @id; end + def value + @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" + end +end + +class Author < Comment + attr_accessor :post + def post_attributes=(attributes); end +end diff --git a/actionpack/test/javascript/ajax_test.rb b/actionpack/test/template/ajax_test.rb index b67a91dad3..670ba92697 100644 --- a/actionpack/test/javascript/ajax_test.rb +++ b/actionpack/test/template/ajax_test.rb @@ -32,7 +32,7 @@ class LinkToRemoteTest < AjaxTestCase end test "with no update" do - assert_html link, %w(href="/blog/destroy/3" Delete\ this\ post data-remote="true") + assert_html link, %w(href="/blog/destroy/4" Delete\ this\ post data-remote="true") end test "basic" do @@ -46,7 +46,7 @@ class LinkToRemoteTest < AjaxTestCase end test "with :html options" do - expected = %{<a href="/blog/destroy/3" data-custom="me" data-update-success="#posts">Delete this post</a>} + expected = %{<a href="/blog/destroy/4" data-custom="me" data-remote="true" data-update-success="#posts">Delete this post</a>} assert_equal expected, link(:update => "#posts", :html => {"data-custom" => "me"}) end @@ -74,7 +74,7 @@ class LinkToRemoteTest < AjaxTestCase end test "basic link_to_remote with :url =>" do - expected = %{<a href="/blog/destroy/3" data-update-success="#posts">Delete this post</a>} + expected = %{<a href="/blog/destroy/3" data-remote="true" data-update-success="#posts">Delete this post</a>} assert_equal expected, link_to_remote("Delete this post", :url => "/blog/destroy/3", :update => "#posts") end @@ -93,7 +93,7 @@ class ButtonToRemoteTest < AjaxTestCase def url_for(*) "/whatnot" end - + class StandardTest < ButtonToRemoteTest test "basic" do button = button({:url => {:action => "whatnot"}}, {:class => "fine"}) @@ -103,13 +103,12 @@ class ButtonToRemoteTest < AjaxTestCase end end end - + class LegacyButtonToRemoteTest < ButtonToRemoteTest include ActionView::Helpers::AjaxHelper::Rails2Compatibility - + assert_callbacks_work do |callback| button(callback => "undoRequestCompleted(request)") end end - -end
\ No newline at end of file +end diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 83fc6a282c..d94135b04b 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -231,6 +231,11 @@ class AssetTagHelperTest < ActionView::TestCase assert_dom_equal(%(<script src="/javascripts/prototype.js?1" type="text/javascript"></script>\n<script src="/javascripts/effects.js?1" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js?1" type="text/javascript"></script>\n<script src="/javascripts/controls.js?1" type="text/javascript"></script>\n<script src="/javascripts/application.js?1" type="text/javascript"></script>), javascript_include_tag(:defaults)) end + def test_javascript_include_tag_is_html_safe + assert javascript_include_tag(:defaults).html_safe? + assert javascript_include_tag("prototype").html_safe? + end + def test_register_javascript_include_default ENV["RAILS_ASSET_ID"] = "" ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'bank' @@ -285,6 +290,13 @@ class AssetTagHelperTest < ActionView::TestCase } end + def test_stylesheet_link_tag_is_html_safe + ENV["RAILS_ASSET_ID"] = "" + assert stylesheet_link_tag('dir/file').html_safe? + assert stylesheet_link_tag('dir/other/file', 'dir/file2').html_safe? + assert stylesheet_tag('dir/file', {}).html_safe? + end + def test_custom_stylesheet_expansions ENV["RAILS_ASSET_ID"] = '' ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :robbery => ["bank", "robber"] diff --git a/actionpack/test/template/erb_util_test.rb b/actionpack/test/template/erb_util_test.rb index 49f51c50c5..fa6b263965 100644 --- a/actionpack/test/template/erb_util_test.rb +++ b/actionpack/test/template/erb_util_test.rb @@ -15,6 +15,18 @@ class ErbUtilTest < Test::Unit::TestCase end end + def test_html_escape_is_html_safe + escaped = h("<p>") + assert_equal "<p>", escaped + assert escaped.html_safe? + end + + def test_html_escape_passes_html_escpe_unmodified + escaped = h("<p>".html_safe!) + assert_equal "<p>", escaped + assert escaped.html_safe? + end + def test_rest_in_ascii (0..127).to_a.map {|int| int.chr }.each do |chr| next if %w(& " < >).include?(chr) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index be15b06372..04c635e770 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1,103 +1,5 @@ require 'abstract_unit' - -silence_warnings do - class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost) - extend ActiveModel::Naming - include ActiveModel::Conversion - - alias_method :secret?, :secret - - def new_record=(boolean) - @new_record = boolean - end - - def new_record? - @new_record - end - - attr_accessor :author - def author_attributes=(attributes); end - - attr_accessor :comments - def comments_attributes=(attributes); end - - attr_accessor :tags - def tags_attributes=(attributes); end - end - - class Comment - extend ActiveModel::Naming - include ActiveModel::Conversion - - attr_reader :id - attr_reader :post_id - def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end - def save; @id = 1; @post_id = 1 end - def new_record?; @id.nil? end - def to_param; @id; end - def name - @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" - end - - attr_accessor :relevances - def relevances_attributes=(attributes); end - - end - - class Tag - extend ActiveModel::Naming - include ActiveModel::Conversion - - attr_reader :id - attr_reader :post_id - def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end - def save; @id = 1; @post_id = 1 end - def new_record?; @id.nil? end - def to_param; @id; end - def value - @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" - end - - attr_accessor :relevances - def relevances_attributes=(attributes); end - - end - - class CommentRelevance - extend ActiveModel::Naming - include ActiveModel::Conversion - - attr_reader :id - attr_reader :comment_id - def initialize(id = nil, comment_id = nil); @id, @comment_id = id, comment_id end - def save; @id = 1; @comment_id = 1 end - def new_record?; @id.nil? end - def to_param; @id; end - def value - @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" - end - end - - class TagRelevance - extend ActiveModel::Naming - include ActiveModel::Conversion - - attr_reader :id - attr_reader :tag_id - def initialize(id = nil, tag_id = nil); @id, @tag_id = id, tag_id end - def save; @id = 1; @tag_id = 1 end - def new_record?; @id.nil? end - def to_param; @id; end - def value - @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}" - end - end - - class Author < Comment - attr_accessor :post - def post_attributes=(attributes); end - end -end +require 'controller/fake_models' class FormHelperTest < ActionView::TestCase tests ActionView::Helpers::FormHelper @@ -1072,7 +974,7 @@ class FormHelperTest < ActionView::TestCase (field_helpers - %w(hidden_field)).each do |selector| src = <<-END_SRC def #{selector}(field, *args, &proc) - "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>" + ("<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>").html_safe! end END_SRC class_eval src, __FILE__, __LINE__ diff --git a/actionpack/test/html-scanner/cdata_node_test.rb b/actionpack/test/template/html-scanner/cdata_node_test.rb index 1822cc565a..1822cc565a 100644 --- a/actionpack/test/html-scanner/cdata_node_test.rb +++ b/actionpack/test/template/html-scanner/cdata_node_test.rb diff --git a/actionpack/test/html-scanner/document_test.rb b/actionpack/test/template/html-scanner/document_test.rb index c68f04fa75..c68f04fa75 100644 --- a/actionpack/test/html-scanner/document_test.rb +++ b/actionpack/test/template/html-scanner/document_test.rb diff --git a/actionpack/test/html-scanner/node_test.rb b/actionpack/test/template/html-scanner/node_test.rb index b0df36877e..b0df36877e 100644 --- a/actionpack/test/html-scanner/node_test.rb +++ b/actionpack/test/template/html-scanner/node_test.rb diff --git a/actionpack/test/html-scanner/sanitizer_test.rb b/actionpack/test/template/html-scanner/sanitizer_test.rb index e85a5c7abf..e85a5c7abf 100644 --- a/actionpack/test/html-scanner/sanitizer_test.rb +++ b/actionpack/test/template/html-scanner/sanitizer_test.rb diff --git a/actionpack/test/html-scanner/tag_node_test.rb b/actionpack/test/template/html-scanner/tag_node_test.rb index d1d4667378..d1d4667378 100644 --- a/actionpack/test/html-scanner/tag_node_test.rb +++ b/actionpack/test/template/html-scanner/tag_node_test.rb diff --git a/actionpack/test/html-scanner/text_node_test.rb b/actionpack/test/template/html-scanner/text_node_test.rb index 1ab3f4454e..1ab3f4454e 100644 --- a/actionpack/test/html-scanner/text_node_test.rb +++ b/actionpack/test/template/html-scanner/text_node_test.rb diff --git a/actionpack/test/html-scanner/tokenizer_test.rb b/actionpack/test/template/html-scanner/tokenizer_test.rb index a001bcbbad..a001bcbbad 100644 --- a/actionpack/test/html-scanner/tokenizer_test.rb +++ b/actionpack/test/template/html-scanner/tokenizer_test.rb diff --git a/actionpack/test/template/raw_output_helper_test.rb b/actionpack/test/template/raw_output_helper_test.rb new file mode 100644 index 0000000000..598aa5b1d8 --- /dev/null +++ b/actionpack/test/template/raw_output_helper_test.rb @@ -0,0 +1,21 @@ +require 'abstract_unit' +require 'testing_sandbox' + +class RawOutputHelperTest < ActionView::TestCase + tests ActionView::Helpers::RawOutputHelper + include TestingSandbox + + def setup + @string = "hello" + end + + test "raw returns the safe string" do + result = raw(@string) + assert_equal @string, result + assert result.html_safe? + end + + test "raw handles nil values correctly" do + assert_equal "", raw(nil) + end +end
\ No newline at end of file diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index 4144fea678..1cd18c0692 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'controller/fake_models' class Post extend ActiveModel::Naming @@ -26,7 +27,7 @@ class RecordTagHelperTest < ActionView::TestCase end def test_content_tag_for_prefix - expected = %(<ul class="post" id="archived_post_45"></ul>) + expected = %(<ul class="archived_post" id="archived_post_45"></ul>) actual = content_tag_for(:ul, @post, :archived) { } assert_dom_equal expected, actual end diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 3c192906ae..35c51ca7ea 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -229,7 +229,7 @@ module RenderTestCases end def test_render_with_nested_layout - assert_equal %(<title>title</title>\n<div id="column">column</div>\n<div id="content">content</div>\n), + assert_equal %(<title>title</title>\n\n\n<div id="column">column</div>\n<div id="content">content</div>\n), @view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield") end diff --git a/actionpack/test/template/sanitize_helper_test.rb b/actionpack/test/template/sanitize_helper_test.rb index f715071bbc..222d4dbf4c 100644 --- a/actionpack/test/template/sanitize_helper_test.rb +++ b/actionpack/test/template/sanitize_helper_test.rb @@ -39,7 +39,16 @@ class SanitizeHelperTest < ActionView::TestCase %{This is a test.\n\n\nIt no longer contains any HTML.\n}, strip_tags( %{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n})) assert_equal "This has a here.", strip_tags("This has a <!-- comment --> here.") - [nil, '', ' '].each { |blank| assert_equal blank, strip_tags(blank) } + [nil, '', ' '].each do |blank| + stripped = strip_tags(blank) + assert_equal blank, stripped + assert stripped.html_safe? unless blank.nil? + end + assert strip_tags("<script>").html_safe? + end + + def test_sanitize_is_marked_safe + assert sanitize("<html><script></script></html>").html_safe? end def assert_sanitized(text, expected = nil) diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index 2aa3d5b5fa..433f6514cf 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -34,6 +34,7 @@ class TagHelperTest < ActionView::TestCase def test_content_tag assert_equal "<a href=\"create\">Create</a>", content_tag("a", "Create", "href" => "create") + assert content_tag("a", "Create", "href" => "create").html_safe? assert_equal content_tag("a", "Create", "href" => "create"), content_tag("a", "Create", :href => "create") end diff --git a/actionpack/test/view/test_case_test.rb b/actionpack/test/template/test_case_test.rb index 3e974b87f7..ca72c13ffa 100644 --- a/actionpack/test/view/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'controller/fake_controllers' module ActionView class TestCase @@ -154,7 +155,7 @@ module ActionView class AssertionsTest < ActionView::TestCase def render_from_helper form_tag('/foo') do - concat render(:text => '<ul><li>foo</li></ul>') + concat render(:text => '<ul><li>foo</li></ul>').html_safe! end end helper_method :render_from_helper diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb index f32d0b3d42..05a14f3554 100644 --- a/actionpack/test/template/test_test.rb +++ b/actionpack/test/template/test_test.rb @@ -19,32 +19,41 @@ module PeopleHelper end class PeopleHelperTest < ActionView::TestCase - def setup - super - ActionController::Routing::Routes.draw do |map| - map.people 'people', :controller => 'people', :action => 'index' - map.connect ':controller/:action/:id' - end - end - def test_title assert_equal "<h1>Ruby on Rails</h1>", title("Ruby on Rails") end def test_homepage_path - assert_equal "/people", homepage_path + with_test_route_set do + assert_equal "/people", homepage_path + end end def test_homepage_url - assert_equal "http://test.host/people", homepage_url + with_test_route_set do + assert_equal "http://test.host/people", homepage_url + end end def test_link_to_person - person = mock(:name => "David") - person.class.extend ActiveModel::Naming - expects(:mocha_mock_path).with(person).returns("/people/1") - assert_equal '<a href="/people/1">David</a>', link_to_person(person) + with_test_route_set do + person = mock(:name => "David") + person.class.extend ActiveModel::Naming + expects(:mocha_mock_path).with(person).returns("/people/1") + assert_equal '<a href="/people/1">David</a>', link_to_person(person) + end end + + private + def with_test_route_set + with_routing do |set| + set.draw do |map| + map.people 'people', :controller => 'people', :action => 'index' + map.connect ':controller/:action/:id' + end + yield + end + end end class CrazyHelperTest < ActionView::TestCase diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index ce99482078..7f6ebc56b7 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -139,7 +139,7 @@ class UrlHelperTest < ActionView::TestCase end def test_link_tag_with_img - assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' /></a>", link_to("<img src='/favicon.jpg' />", "http://www.example.com") + assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' alt=\"Favicon\" /></a>", link_to(image_tag("/favicon.jpg"), "http://www.example.com") end def test_link_with_nil_html_options diff --git a/actionpack/test/ts_isolated.rb b/actionpack/test/ts_isolated.rb new file mode 100644 index 0000000000..21d62f6aa7 --- /dev/null +++ b/actionpack/test/ts_isolated.rb @@ -0,0 +1,17 @@ +$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib') + +require 'test/unit' +require 'rbconfig' +require 'active_support/core_ext/kernel/reporting' + +class TestIsolated < Test::Unit::TestCase + ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME')) + + Dir["#{File.dirname(__FILE__)}/{abstract,controller,dispatch,template}/**/*_test.rb"].each do |file| + define_method("test #{file}") do + command = "#{ruby} -Ilib:test #{file}" + silence_stderr { `#{command}` } + assert_equal 0, $?.to_i, command + end + end +end diff --git a/actionpack/test/view/safe_buffer_test.rb b/actionpack/test/view/safe_buffer_test.rb new file mode 100644 index 0000000000..2236709627 --- /dev/null +++ b/actionpack/test/view/safe_buffer_test.rb @@ -0,0 +1,41 @@ +require 'abstract_unit' + +class SafeBufferTest < ActionView::TestCase + def setup + @buffer = ActionView::SafeBuffer.new + end + + test "Should look like a string" do + assert @buffer.is_a?(String) + assert_equal "", @buffer + end + + test "Should escape a raw string which is passed to them" do + @buffer << "<script>" + assert_equal "<script>", @buffer + end + + test "Should NOT escape a safe value passed to it" do + @buffer << "<script>".html_safe! + assert_equal "<script>", @buffer + end + + test "Should not mess with an innocuous string" do + @buffer << "Hello" + assert_equal "Hello", @buffer + end + + test "Should not mess with a previously escape test" do + @buffer << CGI.escapeHTML("<script>") + assert_equal "<script>", @buffer + end + + test "Should be considered safe" do + assert @buffer.html_safe? + end + + test "Should return a safe buffer when calling to_s" do + new_buffer = @buffer.to_s + assert_equal ActionView::SafeBuffer, new_buffer.class + end +end |