aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2010-07-07 18:38:14 +0200
committerPiotr Sarnacki <drogus@gmail.com>2010-09-03 22:59:05 +0200
commit177a4bd5b7f903030a100f9b5092b1fa62c7c748 (patch)
tree2ca5c7111a952e141bc0baa984876857c9ec9648 /railties/test
parent451c9942bb493190d5673c1b55be7506056db13b (diff)
downloadrails-177a4bd5b7f903030a100f9b5092b1fa62c7c748.tar.gz
rails-177a4bd5b7f903030a100f9b5092b1fa62c7c748.tar.bz2
rails-177a4bd5b7f903030a100f9b5092b1fa62c7c748.zip
Fix url generation for mounted Engine
I added integration tests for generating urls in Engine and application and tweaked Engines to fully cooparate with new router's behavior: * Rails.application now sets ORIGINAL_SCRIPT_NAME * Rails.application also sets its routes as env['action_dispatch.parent_routes'] * Engine implements responds_to? class method to respond to all the instance methods, like #routes
Diffstat (limited to 'railties/test')
-rw-r--r--railties/test/railties/mounted_engine_routes_test.rb107
1 files changed, 107 insertions, 0 deletions
diff --git a/railties/test/railties/mounted_engine_routes_test.rb b/railties/test/railties/mounted_engine_routes_test.rb
new file mode 100644
index 0000000000..fe598b58b1
--- /dev/null
+++ b/railties/test/railties/mounted_engine_routes_test.rb
@@ -0,0 +1,107 @@
+require 'isolation/abstract_unit'
+
+module ApplicationTests
+ class ApplicationRoutingTest < Test::Unit::TestCase
+ require 'rack/test'
+ include Rack::Test::Methods
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+
+ add_to_config("config.action_dispatch.show_exceptions = false")
+
+ @plugin = engine "blog"
+
+ app_file 'config/routes.rb', <<-RUBY
+ AppTemplate::Application.routes.draw do |map|
+ match "/engine_route" => "application_generating#engine_route"
+ match "/url_for_engine_route" => "application_generating#url_for_engine_route"
+ scope "/:user", :user => "anonymous" do
+ mount Blog::Engine => "/blog"
+ end
+ root :to => 'main#index'
+ end
+ RUBY
+
+ @plugin.write "lib/blog.rb", <<-RUBY
+ module Blog
+ class Engine < ::Rails::Engine
+ end
+ end
+ RUBY
+
+ app_file "config/initializers/bla.rb", <<-RUBY
+ Blog::Engine.eager_load!
+ RUBY
+
+ @plugin.write "config/routes.rb", <<-RUBY
+ Blog::Engine.routes.draw do
+ resources :posts do
+ get :generate_application_route
+ end
+ end
+ RUBY
+
+ @plugin.write "app/controllers/posts_controller.rb", <<-RUBY
+ class PostsController < ActionController::Base
+ include Blog::Engine.routes.url_helpers
+
+ def index
+ render :text => post_path(1)
+ end
+
+ def generate_application_route
+ path = url_for( :routes => Rails.application.routes,
+ :controller => "main",
+ :action => "index",
+ :only_path => true)
+ render :text => path
+ end
+ end
+ RUBY
+
+ app_file "app/controllers/application_generating_controller.rb", <<-RUBY
+ class ApplicationGeneratingController < ActionController::Base
+ include Blog::Engine.routes.url_helpers
+
+ def engine_route
+ render :text => posts_path
+ end
+
+ def url_for_engine_route
+ render :text => url_for(:controller => "posts", :action => "index", :user => "john", :only_path => true)
+ end
+ end
+ RUBY
+
+ boot_rails
+ end
+
+ def app
+ @app ||= begin
+ require "#{app_path}/config/environment"
+ Rails.application
+ end
+ end
+
+ test "routes generation in engine and application" do
+ # test generating engine's route from engine
+ get "/john/blog/posts"
+ assert_equal "/john/blog/posts/1", last_response.body
+
+ # test generating engine's route from application
+ get "/engine_route"
+ assert_equal "/anonymous/blog/posts", last_response.body
+ get "/url_for_engine_route"
+ assert_equal "/john/blog/posts", last_response.body
+
+ # test generating application's route from engine
+ get "/someone/blog/generate_application_route"
+ assert_equal "/", last_response.body
+ get "/someone/blog/generate_application_route", {}, "SCRIPT_NAME" => "/foo"
+ assert_equal "/foo/", last_response.body
+ end
+ end
+end
+