diff options
Diffstat (limited to 'actionpack/test/dispatch')
-rw-r--r-- | actionpack/test/dispatch/middleware_stack_test.rb | 9 | ||||
-rw-r--r-- | actionpack/test/dispatch/mount_test.rb | 5 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing_assertions_test.rb | 6 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing_test.rb | 135 | ||||
-rw-r--r-- | actionpack/test/dispatch/url_generation_test.rb | 15 |
5 files changed, 162 insertions, 8 deletions
diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb index 4191ed1ff4..948a690979 100644 --- a/actionpack/test/dispatch/middleware_stack_test.rb +++ b/actionpack/test/dispatch/middleware_stack_test.rb @@ -45,7 +45,7 @@ class MiddlewareStackTest < ActiveSupport::TestCase assert_equal BazMiddleware, @stack.last.klass assert_equal([true, {:foo => "bar"}], @stack.last.args) end - + test "use should push middleware class with block arguments onto the stack" do proc = Proc.new {} assert_difference "@stack.size" do @@ -54,7 +54,7 @@ class MiddlewareStackTest < ActiveSupport::TestCase assert_equal BlockMiddleware, @stack.last.klass assert_equal proc, @stack.last.block end - + test "insert inserts middleware at the integer index" do @stack.insert(1, BazMiddleware) assert_equal BazMiddleware, @stack[1].klass @@ -87,6 +87,11 @@ class MiddlewareStackTest < ActiveSupport::TestCase assert_equal FooMiddleware, @stack[0].klass end + test "unshift adds a new middleware at the beginning of the stack" do + @stack.unshift :"MiddlewareStackTest::BazMiddleware" + assert_equal BazMiddleware, @stack.first.klass + end + test "raise an error on invalid index" do assert_raise RuntimeError do @stack.insert("HiyaMiddleware", BazMiddleware) diff --git a/actionpack/test/dispatch/mount_test.rb b/actionpack/test/dispatch/mount_test.rb index f7a746120e..536e35ab2e 100644 --- a/actionpack/test/dispatch/mount_test.rb +++ b/actionpack/test/dispatch/mount_test.rb @@ -37,6 +37,11 @@ class TestRoutingMount < ActionDispatch::IntegrationTest assert_equal "/sprockets -- /omg", response.body end + def test_mounting_works_with_nested_script_name + get "/foo/sprockets/omg", {}, 'SCRIPT_NAME' => '/foo', 'PATH_INFO' => '/sprockets/omg' + assert_equal "/foo/sprockets -- /omg", response.body + end + def test_mounting_works_with_scope get "/its_a/sprocket/omg" assert_equal "/its_a/sprocket -- /omg", response.body diff --git a/actionpack/test/dispatch/routing_assertions_test.rb b/actionpack/test/dispatch/routing_assertions_test.rb index e953029456..517354ae58 100644 --- a/actionpack/test/dispatch/routing_assertions_test.rb +++ b/actionpack/test/dispatch/routing_assertions_test.rb @@ -47,7 +47,7 @@ class RoutingAssertionsTest < ActionController::TestCase def test_assert_recognizes_with_extras assert_recognizes({ :controller => 'articles', :action => 'index', :page => '1' }, '/articles', { :page => '1' }) end - + def test_assert_recognizes_with_method assert_recognizes({ :controller => 'articles', :action => 'create' }, { :path => '/articles', :method => :post }) assert_recognizes({ :controller => 'articles', :action => 'update', :id => '1' }, { :path => '/articles/1', :method => :put }) @@ -57,7 +57,7 @@ class RoutingAssertionsTest < ActionController::TestCase assert_raise(ActionController::RoutingError) do assert_recognizes({ :controller => 'secure_articles', :action => 'index' }, 'http://test.host/secure/articles') end - assert_recognizes({ :controller => 'secure_articles', :action => 'index' }, 'https://test.host/secure/articles') + assert_recognizes({ :controller => 'secure_articles', :action => 'index', :protocol => 'https://' }, 'https://test.host/secure/articles') end def test_assert_recognizes_with_block_constraint @@ -90,7 +90,7 @@ class RoutingAssertionsTest < ActionController::TestCase assert_raise(ActionController::RoutingError) do assert_routing('http://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index' }) end - assert_routing('https://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index' }) + assert_routing('https://test.host/secure/articles', { :controller => 'secure_articles', :action => 'index', :protocol => 'https://' }) end def test_assert_routing_with_block_constraint diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index e5345754cd..1a8f40037f 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -829,6 +829,15 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal original_options, options end + def test_url_for_does_not_modify_controller + controller = '/projects' + options = {:controller => controller, :action => 'status', :only_path => true} + url = url_for(options) + + assert_equal '/projects/status', url + assert_equal '/projects', controller + end + # tests the arguments modification free version of define_hash_access def test_named_route_with_no_side_effects original_options = { :host => 'test.host' } @@ -2315,6 +2324,55 @@ class TestNamespaceWithControllerOption < ActionDispatch::IntegrationTest end end +class TestDrawExternalFile < ActionDispatch::IntegrationTest + class ExternalController < ActionController::Base + def index + render :text => "external#index" + end + end + + DRAW_PATH = Pathname.new(File.expand_path('../../fixtures/routes', __FILE__)) + + DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new.tap do |app| + app.draw_paths << DRAW_PATH + end + + def app + DefaultScopeRoutes + end + + def test_draw_external_file + DefaultScopeRoutes.draw do + scope :module => 'test_draw_external_file' do + draw :external + end + end + + get '/external' + assert_equal "external#index", @response.body + end + + def test_draw_nonexistent_file + exception = assert_raise ArgumentError do + DefaultScopeRoutes.draw do + draw :nonexistent + end + end + assert_match 'Your router tried to #draw the external file nonexistent.rb', exception.message + assert_match DRAW_PATH.to_s, exception.message + end + + def test_draw_bogus_file + exception = assert_raise NoMethodError do + DefaultScopeRoutes.draw do + draw :bogus + end + end + assert_match "undefined method `wrong'", exception.message + assert_match 'test/fixtures/routes/bogus.rb:1', exception.backtrace.first + end +end + class TestDefaultScope < ActionDispatch::IntegrationTest module ::Blog class PostsController < ActionController::Base @@ -2562,3 +2620,80 @@ class TestOptimizedNamedRoutes < ActionDispatch::IntegrationTest assert_equal '/foo', foo_path end end + +class TestNamedRouteUrlHelpers < ActionDispatch::IntegrationTest + class CategoriesController < ActionController::Base + def show + render :text => "categories#show" + end + end + + class ProductsController < ActionController::Base + def show + render :text => "products#show" + end + end + + Routes = ActionDispatch::Routing::RouteSet.new.tap do |app| + app.draw do + scope :module => "test_named_route_url_helpers" do + get "/categories/:id" => 'categories#show', :as => :category + get "/products/:id" => 'products#show', :as => :product + end + end + end + + def app; Routes end + + include Routes.url_helpers + + test "url helpers do not ignore nil parameters when using non-optimized routes" do + Routes.stubs(:optimize_routes_generation?).returns(false) + + get "/categories/1" + assert_response :success + assert_raises(ActionController::RoutingError) { product_path(nil) } + end +end + +class TestUrlConstraints < ActionDispatch::IntegrationTest + Routes = ActionDispatch::Routing::RouteSet.new.tap do |app| + app.draw do + ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] } + + constraints :subdomain => 'admin' do + get '/' => ok, :as => :admin_root + end + + scope :constraints => { :protocol => 'https://' } do + get '/' => ok, :as => :secure_root + end + + get '/' => ok, :as => :alternate_root, :constraints => { :port => 8080 } + end + end + + include Routes.url_helpers + def app; Routes end + + test "constraints are copied to defaults when using constraints method" do + assert_equal 'http://admin.example.com/', admin_root_url + + get 'http://admin.example.com/' + assert_response :success + end + + test "constraints are copied to defaults when using scope constraints hash" do + assert_equal 'https://www.example.com/', secure_root_url + + get 'https://www.example.com/' + assert_response :success + end + + test "constraints are copied to defaults when using route constraints hash" do + assert_equal 'http://www.example.com:8080/', alternate_root_url + + get 'http://www.example.com:8080/' + assert_response :success + end +end diff --git a/actionpack/test/dispatch/url_generation_test.rb b/actionpack/test/dispatch/url_generation_test.rb index 985ff2e81a..e56e8ddc57 100644 --- a/actionpack/test/dispatch/url_generation_test.rb +++ b/actionpack/test/dispatch/url_generation_test.rb @@ -3,7 +3,7 @@ require 'abstract_unit' module TestUrlGeneration class WithMountPoint < ActionDispatch::IntegrationTest Routes = ActionDispatch::Routing::RouteSet.new - Routes.draw { get "/foo", :to => "my_route_generating#index", :as => :foo } + include Routes.url_helpers class ::MyRouteGeneratingController < ActionController::Base include Routes.url_helpers @@ -12,7 +12,11 @@ module TestUrlGeneration end end - include Routes.url_helpers + Routes.draw do + get "/foo", :to => "my_route_generating#index", :as => :foo + + mount MyRouteGeneratingController.action(:index), at: '/bar' + end def _routes Routes @@ -30,11 +34,16 @@ module TestUrlGeneration assert_equal "/bar/foo", foo_path(:script_name => "/bar") end - test "the request's SCRIPT_NAME takes precedence over the routes'" do + test "the request's SCRIPT_NAME takes precedence over the route" do get "/foo", {}, 'SCRIPT_NAME' => "/new", 'action_dispatch.routes' => Routes assert_equal "/new/foo", response.body end + test "the request's SCRIPT_NAME wraps the mounted app's" do + get '/new/bar/foo', {}, 'SCRIPT_NAME' => '/new', 'PATH_INFO' => '/bar/foo', 'action_dispatch.routes' => Routes + assert_equal "/new/bar/foo", response.body + end + test "handling http protocol with https set" do https! assert_equal "http://www.example.com/foo", foo_url(:protocol => "http") |