Skip to content
This repository
Browse code

Added some more tests for url generation between Engine and Application

  • Loading branch information...
commit b697ba9fd72ac8701747863b42082e59f13ba678 1 parent 8a07708
Piotr Sarnacki drogus authored
8 actionpack/lib/action_controller/metal/url_for.rb
@@ -5,10 +5,10 @@ module UrlFor
5 5 include ActionDispatch::Routing::UrlFor
6 6
7 7 def url_options
8   - options = {}
9   - if respond_to?(:env) && env && _routes.equal?(env["action_dispatch.routes"])
10   - options[:script_name] = request.script_name
11   - end
  8 + options = {}
  9 + if respond_to?(:env) && env && _routes.equal?(env["action_dispatch.routes"])
  10 + options[:script_name] = request.script_name
  11 + end
12 12
13 13 super.merge(options).reverse_merge(
14 14 :host => request.host_with_port,
38 actionpack/test/dispatch/prefix_generation_test.rb
@@ -11,7 +11,7 @@ def self.routes
11 11 routes = ActionDispatch::Routing::RouteSet.new
12 12 routes.draw do
13 13 match "/posts/:id", :to => "inside_engine_generating#index", :as => :post
14   - match "/bare_url_for", :to => "inside_engine_generating#bare_url_for", :as => :bare_url_for
  14 + match "/url_to_application", :to => "inside_engine_generating#url_to_application"
15 15 end
16 16
17 17 routes
@@ -42,10 +42,6 @@ def self.routes
42 42
43 43 def self.call(env)
44 44 env['action_dispatch.routes'] = routes
45   -
46   - # the next to values should be set only in application
47   - env['ORIGINAL_SCRIPT_NAME'] = env['SCRIPT_NAME']
48   - env['action_dispatch.parent_routes'] = routes
49 45 routes.call(env)
50 46 end
51 47 end
@@ -56,7 +52,7 @@ def index
56 52 render :text => post_path(:id => params[:id])
57 53 end
58 54
59   - def bare_url_for
  55 + def url_to_application
60 56 path = url_for( :routes => RailsApplication.routes,
61 57 :controller => "outside_engine_generating",
62 58 :action => "index",
@@ -111,12 +107,23 @@ def setup
111 107 end
112 108
113 109 test "prepend prefix outside the engine" do
  110 + get "/generate"
  111 + assert_equal "/awesome/blog/posts/1", last_response.body
  112 + end
  113 +
  114 + test "prepend prefix outside the engine and use default_url_options[:script_name]" do
  115 + RailsApplication.routes.default_url_options = {:script_name => "/something"}
  116 + get "/generate"
  117 + assert_equal "/something/awesome/blog/posts/1", last_response.body
  118 + end
  119 +
  120 + test "give higher priority to default_url_options[:script_name]" do
114 121 RailsApplication.routes.default_url_options = {:script_name => "/something"}
115   - get "/generate", {}, 'SCRIPT_NAME' => "/something"
  122 + get "/generate", {}, 'SCRIPT_NAME' => "/foo"
116 123 assert_equal "/something/awesome/blog/posts/1", last_response.body
117 124 end
118 125
119   - test "generating urls with options for both prefix and named_route" do
  126 + test "generating urls with options for prefix and named_route" do
120 127 assert_equal "/pure-awesomness/blog/posts/3", post_path(:id => 3, :omg => "pure-awesomness")
121 128 end
122 129
@@ -129,9 +136,20 @@ def setup
129 136 assert_equal "/awesome/blog/posts/42", Foo.new.foo
130 137 end
131 138
132   - test "passing :routes to url_for to change current routes" do
  139 + test "generating application's url from engine" do
  140 + get "/pure-awesomness/blog/url_to_application"
  141 + assert_equal "/generate", last_response.body
  142 + end
  143 +
  144 + test "generating application's url from engine with default_url_options[:script_name]" do
  145 + RailsApplication.routes.default_url_options = {:script_name => "/something"}
  146 + get "/pure-awesomness/blog/url_to_application"
  147 + assert_equal "/something/generate", last_response.body
  148 + end
  149 +
  150 + test "generating application's url from engine should give higher priority to default_url_options[:script_name]" do
133 151 RailsApplication.routes.default_url_options = {:script_name => "/something"}
134   - get "/pure-awesomness/blog/bare_url_for", {}, 'SCRIPT_NAME' => "/something"
  152 + get "/pure-awesomness/blog/url_to_application", {}, 'SCRIPT_NAME' => '/foo'
135 153 assert_equal "/something/generate", last_response.body
136 154 end
137 155
31 railties/test/railties/mounted_engine_routes_test.rb
@@ -70,7 +70,7 @@ def engine_route
70 70 end
71 71
72 72 def url_for_engine_route
73   - render :text => url_for(:controller => "posts", :action => "index", :user => "john", :only_path => true)
  73 + render :text => url_for(:controller => "posts", :action => "index", :user => "john", :only_path => true, :routes => Blog::Engine.routes)
74 74 end
75 75 end
76 76 RUBY
@@ -85,24 +85,47 @@ def app
85 85 end
86 86 end
87 87
  88 + def reset_script_name!
  89 + Rails.application.routes.default_url_options = {}
  90 + end
  91 +
  92 + def script_name(script_name)
  93 + Rails.application.routes.default_url_options = {:script_name => script_name}
  94 + end
  95 +
88 96 test "routes generation in engine and application" do
89 97 # test generating engine's route from engine
90 98 get "/john/blog/posts"
91 99 assert_equal "/john/blog/posts/1", last_response.body
92 100
  101 + # test generating engine's route from engine with default_url_options
  102 + script_name "/foo"
  103 + get "/john/blog/posts", {}, 'SCRIPT_NAME' => "/foo"
  104 + assert_equal "/foo/john/blog/posts/1", last_response.body
  105 + reset_script_name!
  106 +
93 107 # test generating engine's route from application
94 108 get "/engine_route"
95 109 assert_equal "/anonymous/blog/posts", last_response.body
96 110 get "/url_for_engine_route"
97 111 assert_equal "/john/blog/posts", last_response.body
98 112
  113 + # test generating engine's route from application with default_url_options
  114 + script_name "/foo"
  115 + get "/engine_route", {}, 'SCRIPT_NAME' => "/foo"
  116 + assert_equal "/foo/anonymous/blog/posts", last_response.body
  117 + script_name "/foo"
  118 + get "/url_for_engine_route", {}, 'SCRIPT_NAME' => "/foo"
  119 + assert_equal "/foo/john/blog/posts", last_response.body
  120 + reset_script_name!
  121 +
99 122 # test generating application's route from engine
100 123 get "/someone/blog/generate_application_route"
101 124 assert_equal "/", last_response.body
102 125
103   - # with script_name
104   - Rails.application.default_url_options = {:script_name => "/foo"}
105   - get "/someone/blog/generate_application_route", {}, "SCRIPT_NAME" => "/foo"
  126 + # test generating application's route from engine with default_url_options
  127 + script_name "/foo"
  128 + get "/someone/blog/generate_application_route", {}, 'SCRIPT_NAME' => '/foo'
106 129 assert_equal "/foo/", last_response.body
107 130 end
108 131 end

0 comments on commit b697ba9

Please sign in to comment.
Something went wrong with that request. Please try again.