1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
# frozen_string_literal: true
require "abstract_unit"
require "rack/test"
require "rails/engine"
module TestGenerationPrefix
class Post
extend ActiveModel::Naming
def to_param
"1"
end
def self.model_name
klass = "Post".dup
def klass.name; self end
ActiveModel::Name.new(klass)
end
def to_model; self; end
def persisted?; true; end
end
class WithMountedEngine < ActionDispatch::IntegrationTest
class BlogEngine < Rails::Engine
routes.draw do
get "/posts/:id", to: "inside_engine_generating#show", as: :post
get "/posts", to: "inside_engine_generating#index", as: :posts
get "/url_to_application", to: "inside_engine_generating#url_to_application"
get "/polymorphic_path_for_engine", to: "inside_engine_generating#polymorphic_path_for_engine"
get "/conflicting_url", to: "inside_engine_generating#conflicting"
get "/foo", to: "never#invoked", as: :named_helper_that_should_be_invoked_only_in_respond_to_test
get "/relative_path_root", to: redirect("")
get "/relative_path_redirect", to: redirect("foo")
get "/relative_option_root", to: redirect(path: "")
get "/relative_option_redirect", to: redirect(path: "foo")
get "/relative_custom_root", to: redirect { |params, request| "" }
get "/relative_custom_redirect", to: redirect { |params, request| "foo" }
get "/absolute_path_root", to: redirect("/")
get "/absolute_path_redirect", to: redirect("/foo")
get "/absolute_option_root", to: redirect(path: "/")
get "/absolute_option_redirect", to: redirect(path: "/foo")
get "/absolute_custom_root", to: redirect { |params, request| "/" }
get "/absolute_custom_redirect", to: redirect { |params, request| "/foo" }
end
end
class RailsApplication < Rails::Engine
routes.draw do
scope "/:omg", omg: "awesome" do
mount BlogEngine => "/blog", :as => "blog_engine"
end
get "/posts/:id", to: "outside_engine_generating#post", as: :post
get "/generate", to: "outside_engine_generating#index"
get "/polymorphic_path_for_app", to: "outside_engine_generating#polymorphic_path_for_app"
get "/polymorphic_path_for_engine", to: "outside_engine_generating#polymorphic_path_for_engine"
get "/polymorphic_with_url_for", to: "outside_engine_generating#polymorphic_with_url_for"
get "/conflicting_url", to: "outside_engine_generating#conflicting"
get "/ivar_usage", to: "outside_engine_generating#ivar_usage"
root to: "outside_engine_generating#index"
end
end
# force draw
RailsApplication.routes.define_mounted_helper(:main_app)
class ::InsideEngineGeneratingController < ActionController::Base
include BlogEngine.routes.url_helpers
include RailsApplication.routes.mounted_helpers
def index
render plain: posts_path
end
def show
render plain: post_path(id: params[:id])
end
def url_to_application
path = main_app.url_for(controller: "outside_engine_generating",
action: "index",
only_path: true)
render plain: path
end
def polymorphic_path_for_engine
render plain: polymorphic_path(Post.new)
end
def conflicting
render plain: "engine"
end
end
class ::OutsideEngineGeneratingController < ActionController::Base
include BlogEngine.routes.mounted_helpers
include RailsApplication.routes.url_helpers
def index
render plain: blog_engine.post_path(id: 1)
end
def polymorphic_path_for_engine
render plain: blog_engine.polymorphic_path(Post.new)
end
def polymorphic_path_for_app
render plain: polymorphic_path(Post.new)
end
def polymorphic_with_url_for
render plain: blog_engine.url_for(Post.new)
end
def conflicting
render plain: "application"
end
def ivar_usage
@blog_engine = "Not the engine route helper"
render plain: blog_engine.post_path(id: 1)
end
end
class EngineObject
include ActionDispatch::Routing::UrlFor
include BlogEngine.routes.url_helpers
end
class AppObject
include ActionDispatch::Routing::UrlFor
include RailsApplication.routes.url_helpers
end
def app
RailsApplication.instance
end
attr_reader :engine_object, :app_object
def setup
RailsApplication.routes.default_url_options = {}
@engine_object = EngineObject.new
@app_object = AppObject.new
end
include BlogEngine.routes.mounted_helpers
# Inside Engine
test "[ENGINE] generating engine's url use SCRIPT_NAME from request" do
get "/pure-awesomeness/blog/posts/1"
assert_equal "/pure-awesomeness/blog/posts/1", response.body
end
test "[ENGINE] generating application's url never uses SCRIPT_NAME from request" do
get "/pure-awesomeness/blog/url_to_application"
assert_equal "/generate", response.body
end
test "[ENGINE] generating engine's url with polymorphic path" do
get "/pure-awesomeness/blog/polymorphic_path_for_engine"
assert_equal "/pure-awesomeness/blog/posts/1", response.body
end
test "[ENGINE] url_helpers from engine have higher priority than application's url_helpers" do
get "/awesome/blog/conflicting_url"
assert_equal "engine", response.body
end
test "[ENGINE] relative path root uses SCRIPT_NAME from request" do
get "/awesome/blog/relative_path_root"
verify_redirect "http://www.example.com/awesome/blog"
end
test "[ENGINE] relative path redirect uses SCRIPT_NAME from request" do
get "/awesome/blog/relative_path_redirect"
verify_redirect "http://www.example.com/awesome/blog/foo"
end
test "[ENGINE] relative option root uses SCRIPT_NAME from request" do
get "/awesome/blog/relative_option_root"
verify_redirect "http://www.example.com/awesome/blog"
end
test "[ENGINE] relative option redirect uses SCRIPT_NAME from request" do
get "/awesome/blog/relative_option_redirect"
verify_redirect "http://www.example.com/awesome/blog/foo"
end
test "[ENGINE] relative custom root uses SCRIPT_NAME from request" do
get "/awesome/blog/relative_custom_root"
verify_redirect "http://www.example.com/awesome/blog"
end
test "[ENGINE] relative custom redirect uses SCRIPT_NAME from request" do
get "/awesome/blog/relative_custom_redirect"
verify_redirect "http://www.example.com/awesome/blog/foo"
end
test "[ENGINE] absolute path root doesn't use SCRIPT_NAME from request" do
get "/awesome/blog/absolute_path_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] absolute path redirect doesn't use SCRIPT_NAME from request" do
get "/awesome/blog/absolute_path_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] absolute option root doesn't use SCRIPT_NAME from request" do
get "/awesome/blog/absolute_option_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] absolute option redirect doesn't use SCRIPT_NAME from request" do
get "/awesome/blog/absolute_option_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] absolute custom root doesn't use SCRIPT_NAME from request" do
get "/awesome/blog/absolute_custom_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] absolute custom redirect doesn't use SCRIPT_NAME from request" do
get "/awesome/blog/absolute_custom_redirect"
verify_redirect "http://www.example.com/foo"
end
# Inside Application
test "[APP] generating engine's route includes prefix" do
get "/generate"
assert_equal "/awesome/blog/posts/1", response.body
end
test "[APP] generating engine's route includes default_url_options[:script_name]" do
RailsApplication.routes.default_url_options = { script_name: "/something" }
get "/generate"
assert_equal "/something/awesome/blog/posts/1", response.body
end
test "[APP] generating engine's url with polymorphic path" do
get "/polymorphic_path_for_engine"
assert_equal "/awesome/blog/posts/1", response.body
end
test "polymorphic_path_for_app" do
get "/polymorphic_path_for_app"
assert_equal "/posts/1", response.body
end
test "[APP] generating engine's url with url_for(@post)" do
get "/polymorphic_with_url_for"
assert_equal "http://www.example.com/awesome/blog/posts/1", response.body
end
test "[APP] instance variable with same name as engine" do
get "/ivar_usage"
assert_equal "/awesome/blog/posts/1", response.body
end
# Inside any Object
test "[OBJECT] proxy route should override respond_to?() as expected" do
assert_respond_to blog_engine, :named_helper_that_should_be_invoked_only_in_respond_to_test_path
end
test "[OBJECT] generating engine's route includes prefix" do
assert_equal "/awesome/blog/posts/1", engine_object.post_path(id: 1)
end
test "[OBJECT] generating engine's route includes dynamic prefix" do
assert_equal "/pure-awesomeness/blog/posts/3", engine_object.post_path(id: 3, omg: "pure-awesomeness")
end
test "[OBJECT] generating engine's route includes default_url_options[:script_name]" do
RailsApplication.routes.default_url_options = { script_name: "/something" }
assert_equal "/something/pure-awesomeness/blog/posts/3", engine_object.post_path(id: 3, omg: "pure-awesomeness")
end
test "[OBJECT] generating application's route" do
assert_equal "/", app_object.root_path
end
test "[OBJECT] generating application's route includes default_url_options[:script_name]" do
RailsApplication.routes.default_url_options = { script_name: "/something" }
assert_equal "/something/", app_object.root_path
end
test "[OBJECT] generating application's route includes default_url_options[:trailing_slash]" do
RailsApplication.routes.default_url_options[:trailing_slash] = true
assert_equal "/awesome/blog/posts", engine_object.posts_path
end
test "[OBJECT] generating engine's route with url_for" do
path = engine_object.url_for(controller: "inside_engine_generating",
action: "show",
only_path: true,
omg: "omg",
id: 1)
assert_equal "/omg/blog/posts/1", path
end
test "[OBJECT] generating engine's route with named helpers" do
path = engine_object.posts_path
assert_equal "/awesome/blog/posts", path
path = engine_object.posts_url(host: "example.com")
assert_equal "http://example.com/awesome/blog/posts", path
end
test "[OBJECT] generating engine's route with polymorphic_url" do
path = engine_object.polymorphic_path(Post.new)
assert_equal "/awesome/blog/posts/1", path
path = engine_object.polymorphic_url(Post.new, host: "www.example.com")
assert_equal "http://www.example.com/awesome/blog/posts/1", path
end
private
def verify_redirect(url, status = 301)
assert_equal status, response.status
assert_equal url, response.headers["Location"]
assert_equal expected_redirect_body(url), response.body
end
def expected_redirect_body(url)
%(<html><body>You are being <a href="#{url}">redirected</a>.</body></html>)
end
end
class EngineMountedAtRoot < ActionDispatch::IntegrationTest
class BlogEngine
def self.routes
@routes ||= begin
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
get "/posts/:id", to: "posts#show", as: :post
get "/relative_path_root", to: redirect("")
get "/relative_path_redirect", to: redirect("foo")
get "/relative_option_root", to: redirect(path: "")
get "/relative_option_redirect", to: redirect(path: "foo")
get "/relative_custom_root", to: redirect { |params, request| "" }
get "/relative_custom_redirect", to: redirect { |params, request| "foo" }
get "/absolute_path_root", to: redirect("/")
get "/absolute_path_redirect", to: redirect("/foo")
get "/absolute_option_root", to: redirect(path: "/")
get "/absolute_option_redirect", to: redirect(path: "/foo")
get "/absolute_custom_root", to: redirect { |params, request| "/" }
get "/absolute_custom_redirect", to: redirect { |params, request| "/foo" }
end
routes
end
end
def self.call(env)
env["action_dispatch.routes"] = routes
routes.call(env)
end
end
class RailsApplication < Rails::Engine
routes.draw do
mount BlogEngine => "/"
end
end
class ::PostsController < ActionController::Base
include BlogEngine.routes.url_helpers
include RailsApplication.routes.mounted_helpers
def show
render plain: post_path(id: params[:id])
end
end
def app
RailsApplication.instance
end
test "generating path inside engine" do
get "/posts/1"
assert_equal "/posts/1", response.body
end
test "[ENGINE] relative path root uses SCRIPT_NAME from request" do
get "/relative_path_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] relative path redirect uses SCRIPT_NAME from request" do
get "/relative_path_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] relative option root uses SCRIPT_NAME from request" do
get "/relative_option_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] relative option redirect uses SCRIPT_NAME from request" do
get "/relative_option_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] relative custom root uses SCRIPT_NAME from request" do
get "/relative_custom_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] relative custom redirect uses SCRIPT_NAME from request" do
get "/relative_custom_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] absolute path root doesn't use SCRIPT_NAME from request" do
get "/absolute_path_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] absolute path redirect doesn't use SCRIPT_NAME from request" do
get "/absolute_path_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] absolute option root doesn't use SCRIPT_NAME from request" do
get "/absolute_option_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] absolute option redirect doesn't use SCRIPT_NAME from request" do
get "/absolute_option_redirect"
verify_redirect "http://www.example.com/foo"
end
test "[ENGINE] absolute custom root doesn't use SCRIPT_NAME from request" do
get "/absolute_custom_root"
verify_redirect "http://www.example.com/"
end
test "[ENGINE] absolute custom redirect doesn't use SCRIPT_NAME from request" do
get "/absolute_custom_redirect"
verify_redirect "http://www.example.com/foo"
end
private
def verify_redirect(url, status = 301)
assert_equal status, response.status
assert_equal url, response.headers["Location"]
assert_equal expected_redirect_body(url), response.body
end
def expected_redirect_body(url)
%(<html><body>You are being <a href="#{url}">redirected</a>.</body></html>)
end
end
end
|