rails_legacy_mapper 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gemtest +0 -0
- data/CHANGELOG +3 -0
- data/LICENSE +20 -0
- data/README +25 -0
- data/Rakefile +27 -0
- data/lib/rails_legacy_mapper.rb +23 -0
- data/lib/rails_legacy_mapper/mapper.rb +488 -0
- data/lib/rails_legacy_mapper/route_set_extensions.rb +53 -0
- data/lib/rails_legacy_mapper/version.rb +3 -0
- data/rails_legacy_mapper.gemspec +55 -0
- data/test/fake_controllers.rb +66 -0
- data/test/legacy_route_set_test.rb +578 -0
- data/test/rack_mount_integration_test.rb +310 -0
- data/test/resources_test.rb +1395 -0
- data/test/route_set_test.rb +959 -0
- data/test/test_helper.rb +48 -0
- data/test/uri_reserved_characters_routing_test.rb +54 -0
- metadata +158 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/module/aliasing'
|
3
|
+
|
4
|
+
module RailsLegacyMapper
|
5
|
+
module RouteSetExtensions #:nodoc:
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
CONTROLLER_REGEXP = /[_a-zA-Z0-9]+/
|
9
|
+
|
10
|
+
included do
|
11
|
+
attr_accessor :controller_namespaces
|
12
|
+
alias_method_chain :initialize, :legacy_mapper
|
13
|
+
alias_method_chain :eval_block, :legacy_mapper
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods #:nodoc:
|
17
|
+
def initialize_with_legacy_mapper(request_class = ActionDispatch::Request)
|
18
|
+
initialize_without_legacy_mapper
|
19
|
+
self.controller_namespaces = Set.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def controller_constraints
|
23
|
+
@controller_constraints ||= begin
|
24
|
+
namespaces = controller_namespaces + in_memory_controller_namespaces
|
25
|
+
source = namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" }
|
26
|
+
source << CONTROLLER_REGEXP.source
|
27
|
+
Regexp.compile(source.sort.reverse.join('|'))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def in_memory_controller_namespaces
|
32
|
+
namespaces = Set.new
|
33
|
+
ActionController::Base.descendants.each do |klass|
|
34
|
+
next if klass.anonymous?
|
35
|
+
namespaces << klass.name.underscore.split('/')[0...-1].join('/')
|
36
|
+
end
|
37
|
+
namespaces.delete('')
|
38
|
+
namespaces
|
39
|
+
end
|
40
|
+
|
41
|
+
def eval_block_with_legacy_mapper(block)
|
42
|
+
mapper = ActionDispatch::Routing::Mapper.new(self)
|
43
|
+
if block.arity == 1
|
44
|
+
mapper.instance_exec(RailsLegacyMapper::Mapper.new(self), &block)
|
45
|
+
elsif default_scope
|
46
|
+
mapper.with_default_scope(default_scope, &block)
|
47
|
+
else
|
48
|
+
mapper.instance_exec(&block)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rails_legacy_mapper/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rails_legacy_mapper"
|
7
|
+
s.version = RailsLegacyMapper::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Andrew White"]
|
10
|
+
s.email = ["andyw@pixeltrix.co.uk"]
|
11
|
+
s.homepage = %q{https://github.com/pixeltrix/rails_legacy_mapper/}
|
12
|
+
s.summary = %q{Old style routes for Rails 3.1}
|
13
|
+
s.description = <<-EOF
|
14
|
+
This gem provides an extraction of the DeprecatedMapper from Rails 3.0.
|
15
|
+
If you have a legacy application with an old style routes.rb file this
|
16
|
+
allows you to get your application running quickly in Rails 3.1.
|
17
|
+
EOF
|
18
|
+
|
19
|
+
s.files = [
|
20
|
+
".gemtest",
|
21
|
+
"CHANGELOG",
|
22
|
+
"LICENSE",
|
23
|
+
"README",
|
24
|
+
"Rakefile",
|
25
|
+
"lib/rails_legacy_mapper.rb",
|
26
|
+
"lib/rails_legacy_mapper/mapper.rb",
|
27
|
+
"lib/rails_legacy_mapper/route_set_extensions.rb",
|
28
|
+
"lib/rails_legacy_mapper/version.rb",
|
29
|
+
"rails_legacy_mapper.gemspec",
|
30
|
+
"test/fake_controllers.rb",
|
31
|
+
"test/legacy_route_set_test.rb",
|
32
|
+
"test/rack_mount_integration_test.rb",
|
33
|
+
"test/resources_test.rb",
|
34
|
+
"test/route_set_test.rb",
|
35
|
+
"test/test_helper.rb",
|
36
|
+
"test/uri_reserved_characters_routing_test.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
s.test_files = [
|
40
|
+
"test/fake_controllers.rb",
|
41
|
+
"test/legacy_route_set_test.rb",
|
42
|
+
"test/rack_mount_integration_test.rb",
|
43
|
+
"test/resources_test.rb",
|
44
|
+
"test/route_set_test.rb",
|
45
|
+
"test/test_helper.rb",
|
46
|
+
"test/uri_reserved_characters_routing_test.rb"
|
47
|
+
]
|
48
|
+
|
49
|
+
s.require_paths = ["lib"]
|
50
|
+
|
51
|
+
s.add_dependency "rails", "~> 3.1.0.beta"
|
52
|
+
s.add_development_dependency "bundler", "~> 1.0.10"
|
53
|
+
s.add_development_dependency "mocha", "~> 0.9.8"
|
54
|
+
s.add_development_dependency "rake", "~> 0.8.7"
|
55
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class ContentController < ActionController::Base; end
|
2
|
+
|
3
|
+
module Admin
|
4
|
+
class << self; alias_method :const_available?, :const_defined?; end
|
5
|
+
class AccountsController < ActionController::Base; end
|
6
|
+
class NewsFeedController < ActionController::Base; end
|
7
|
+
class PostsController < ActionController::Base; end
|
8
|
+
class StuffController < ActionController::Base; end
|
9
|
+
class UserController < ActionController::Base; end
|
10
|
+
class UsersController < ActionController::Base; end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Api
|
14
|
+
class UsersController < ActionController::Base; end
|
15
|
+
class ProductsController < ActionController::Base; end
|
16
|
+
end
|
17
|
+
|
18
|
+
class AccountController < ActionController::Base; end
|
19
|
+
class AddressesController < ActionController::Base; end
|
20
|
+
class ArchiveController < ActionController::Base; end
|
21
|
+
class ArticlesController < ActionController::Base; end
|
22
|
+
class BarController < ActionController::Base; end
|
23
|
+
class BlogController < ActionController::Base; end
|
24
|
+
class BooksController < ActionController::Base; end
|
25
|
+
class BraveController < ActionController::Base; end
|
26
|
+
class CarsController < ActionController::Base; end
|
27
|
+
class CcController < ActionController::Base; end
|
28
|
+
class CController < ActionController::Base; end
|
29
|
+
class ElsewhereController < ActionController::Base; end
|
30
|
+
class FooController < ActionController::Base; end
|
31
|
+
class GeocodeController < ActionController::Base; end
|
32
|
+
class HiController < ActionController::Base; end
|
33
|
+
class ImageController < ActionController::Base; end
|
34
|
+
class NewsController < ActionController::Base; end
|
35
|
+
class NotesController < ActionController::Base; end
|
36
|
+
class PeopleController < ActionController::Base; end
|
37
|
+
class PostsController < ActionController::Base; end
|
38
|
+
class SessionsController < ActionController::Base; end
|
39
|
+
class StuffController < ActionController::Base; end
|
40
|
+
class SubpathBooksController < ActionController::Base; end
|
41
|
+
class SymbolsController < ActionController::Base; end
|
42
|
+
class UserController < ActionController::Base; end
|
43
|
+
class WeblogController < ActionController::Base; end
|
44
|
+
|
45
|
+
# For speed test
|
46
|
+
class SpeedController < ActionController::Base; end
|
47
|
+
class SearchController < SpeedController; end
|
48
|
+
class VideosController < SpeedController; end
|
49
|
+
class VideoFileController < SpeedController; end
|
50
|
+
class VideoSharesController < SpeedController; end
|
51
|
+
class VideoAbusesController < SpeedController; end
|
52
|
+
class VideoUploadsController < SpeedController; end
|
53
|
+
class VideoVisitsController < SpeedController; end
|
54
|
+
class UsersController < SpeedController; end
|
55
|
+
class SettingsController < SpeedController; end
|
56
|
+
class ChannelsController < SpeedController; end
|
57
|
+
class ChannelVideosController < SpeedController; end
|
58
|
+
class LostPasswordsController < SpeedController; end
|
59
|
+
class PagesController < SpeedController; end
|
60
|
+
|
61
|
+
# For nested resource test
|
62
|
+
class MilestonesController < ActionController::Base
|
63
|
+
def index() head :ok end
|
64
|
+
alias_method :show, :index
|
65
|
+
def rescue_action(e) raise e end
|
66
|
+
end
|
@@ -0,0 +1,578 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
class LegacyRouteSetTest < ActiveSupport::TestCase
|
5
|
+
include RoutingTestHelpers
|
6
|
+
|
7
|
+
def test_default_setup
|
8
|
+
set.draw {|map| map.connect ':controller/:action/:id' }
|
9
|
+
|
10
|
+
assert_equal({:controller => "content", :action => 'index'}, recognize_path("/content"))
|
11
|
+
assert_equal({:controller => "content", :action => 'list'}, recognize_path("/content/list"))
|
12
|
+
assert_equal({:controller => "content", :action => 'show', :id => '10'}, recognize_path("/content/show/10"))
|
13
|
+
assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, recognize_path("/admin/user/show/10"))
|
14
|
+
|
15
|
+
assert_equal '/admin/user/show/10', url_for(:controller => 'admin/user', :action => 'show', :id => 10)
|
16
|
+
assert_equal '/admin/user/show', url_for({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
17
|
+
assert_equal '/admin/user/list/10', url_for({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
18
|
+
assert_equal '/admin/stuff', url_for({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
19
|
+
assert_equal '/stuff', url_for({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_ignores_leading_slash
|
23
|
+
set.draw {|map| map.connect '/:controller/:action/:id'}
|
24
|
+
|
25
|
+
assert_equal({:controller => "content", :action => 'index'}, recognize_path("/content"))
|
26
|
+
assert_equal({:controller => "content", :action => 'list'}, recognize_path("/content/list"))
|
27
|
+
assert_equal({:controller => "content", :action => 'show', :id => '10'}, recognize_path("/content/show/10"))
|
28
|
+
assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, recognize_path("/admin/user/show/10"))
|
29
|
+
|
30
|
+
assert_equal '/admin/user/show/10', url_for(:controller => 'admin/user', :action => 'show', :id => 10)
|
31
|
+
assert_equal '/admin/user/show', url_for({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
32
|
+
assert_equal '/admin/user/list/10', url_for({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
33
|
+
assert_equal '/admin/stuff', url_for({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
34
|
+
assert_equal '/stuff', url_for({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_time_recognition
|
38
|
+
# We create many routes to make situation more realistic
|
39
|
+
set.draw { |map|
|
40
|
+
map.frontpage '', :controller => 'search', :action => 'new'
|
41
|
+
map.resources :videos do |video|
|
42
|
+
video.resources :comments
|
43
|
+
video.resource :file, :controller => 'video_file'
|
44
|
+
video.resource :share, :controller => 'video_shares'
|
45
|
+
video.resource :abuse, :controller => 'video_abuses'
|
46
|
+
end
|
47
|
+
map.resources :abuses, :controller => 'video_abuses'
|
48
|
+
map.resources :video_uploads
|
49
|
+
map.resources :video_visits
|
50
|
+
|
51
|
+
map.resources :users do |user|
|
52
|
+
user.resource :settings
|
53
|
+
user.resources :videos
|
54
|
+
end
|
55
|
+
map.resources :channels do |channel|
|
56
|
+
channel.resources :videos, :controller => 'channel_videos'
|
57
|
+
end
|
58
|
+
map.resource :session
|
59
|
+
map.resource :lost_password
|
60
|
+
map.search 'search', :controller => 'search'
|
61
|
+
map.resources :pages
|
62
|
+
map.connect ':controller/:action/:id'
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_route_with_colon_first
|
67
|
+
set.draw do |map|
|
68
|
+
map.connect '/:controller/:action/:id', :action => 'index', :id => nil
|
69
|
+
map.connect ':url', :controller => 'tiny_url', :action => 'translate'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_route_with_regexp_for_controller
|
74
|
+
set.draw do |map|
|
75
|
+
map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
|
76
|
+
map.connect ':controller/:action/:id'
|
77
|
+
end
|
78
|
+
assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
|
79
|
+
recognize_path("/admin/user/foo"))
|
80
|
+
assert_equal({:controller => "content", :action => "foo"}, recognize_path("/content/foo"))
|
81
|
+
assert_equal '/admin/user/foo', url_for(:controller => "admin/user", :admintoken => "foo", :action => "index")
|
82
|
+
assert_equal '/content/foo', url_for(:controller => "content", :action => "foo")
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_route_with_regexp_and_captures_for_controller
|
86
|
+
set.draw do |map|
|
87
|
+
map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
|
88
|
+
end
|
89
|
+
assert_equal({:controller => "admin/accounts", :action => "index"}, recognize_path("/admin/accounts"))
|
90
|
+
assert_equal({:controller => "admin/users", :action => "index"}, recognize_path("/admin/users"))
|
91
|
+
assert_raise(ActionController::RoutingError) { recognize_path("/admin/products") }
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_route_with_regexp_and_dot
|
95
|
+
set.draw do |map|
|
96
|
+
map.connect ':controller/:action/:file',
|
97
|
+
:controller => /admin|user/,
|
98
|
+
:action => /upload|download/,
|
99
|
+
:defaults => {:file => nil},
|
100
|
+
:requirements => {:file => %r{[^/]+(\.[^/]+)?}}
|
101
|
+
end
|
102
|
+
# Without a file extension
|
103
|
+
assert_equal '/user/download/file',
|
104
|
+
url_for(:controller => "user", :action => "download", :file => "file")
|
105
|
+
assert_equal(
|
106
|
+
{:controller => "user", :action => "download", :file => "file"},
|
107
|
+
recognize_path("/user/download/file"))
|
108
|
+
|
109
|
+
# Now, let's try a file with an extension, really a dot (.)
|
110
|
+
assert_equal '/user/download/file.jpg',
|
111
|
+
url_for(
|
112
|
+
:controller => "user", :action => "download", :file => "file.jpg")
|
113
|
+
assert_equal(
|
114
|
+
{:controller => "user", :action => "download", :file => "file.jpg"},
|
115
|
+
recognize_path("/user/download/file.jpg"))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_basic_named_route
|
119
|
+
set.draw do |map|
|
120
|
+
map.home '', :controller => 'content', :action => 'list'
|
121
|
+
end
|
122
|
+
x = setup_for_named_route
|
123
|
+
assert_equal("http://test.host/",
|
124
|
+
x.send(:home_url))
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_named_route_with_option
|
128
|
+
set.draw do |map|
|
129
|
+
map.page 'page/:title', :controller => 'content', :action => 'show_page'
|
130
|
+
end
|
131
|
+
x = setup_for_named_route
|
132
|
+
assert_equal("http://test.host/page/new%20stuff",
|
133
|
+
x.send(:page_url, :title => 'new stuff'))
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_named_route_with_default
|
137
|
+
set.draw do |map|
|
138
|
+
map.page 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
|
139
|
+
end
|
140
|
+
x = setup_for_named_route
|
141
|
+
assert_equal("http://test.host/page/AboutRails",
|
142
|
+
x.send(:page_url, :title => "AboutRails"))
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_named_route_with_name_prefix
|
147
|
+
set.draw do |map|
|
148
|
+
map.page 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
|
149
|
+
end
|
150
|
+
x = setup_for_named_route
|
151
|
+
assert_equal("http://test.host/page",
|
152
|
+
x.send(:my_page_url))
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_named_route_with_path_prefix
|
156
|
+
set.draw do |map|
|
157
|
+
map.page 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
|
158
|
+
end
|
159
|
+
x = setup_for_named_route
|
160
|
+
assert_equal("http://test.host/my/page",
|
161
|
+
x.send(:page_url))
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_named_route_with_blank_path_prefix
|
165
|
+
set.draw do |map|
|
166
|
+
map.page 'page', :controller => 'content', :action => 'show_page', :path_prefix => ''
|
167
|
+
end
|
168
|
+
x = setup_for_named_route
|
169
|
+
assert_equal("http://test.host/page",
|
170
|
+
x.send(:page_url))
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_named_route_with_nested_controller
|
174
|
+
set.draw do |map|
|
175
|
+
map.users 'admin/user', :controller => 'admin/user', :action => 'index'
|
176
|
+
end
|
177
|
+
x = setup_for_named_route
|
178
|
+
assert_equal("http://test.host/admin/user",
|
179
|
+
x.send(:users_url))
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_optimised_named_route_with_host
|
183
|
+
set.draw do |map|
|
184
|
+
map.pages 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
|
185
|
+
end
|
186
|
+
x = setup_for_named_route
|
187
|
+
x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
|
188
|
+
x.send(:pages_url)
|
189
|
+
end
|
190
|
+
|
191
|
+
def setup_for_named_route
|
192
|
+
MockController.build(set.url_helpers).new
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_named_route_without_hash
|
196
|
+
set.draw do |map|
|
197
|
+
map.normal ':controller/:action/:id'
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_named_route_root
|
202
|
+
set.draw do |map|
|
203
|
+
map.root :controller => "hello"
|
204
|
+
end
|
205
|
+
x = setup_for_named_route
|
206
|
+
assert_equal("http://test.host/", x.send(:root_url))
|
207
|
+
assert_equal("/", x.send(:root_path))
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_named_route_with_regexps
|
211
|
+
set.draw do |map|
|
212
|
+
map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
|
213
|
+
:year => /\d+/, :month => /\d+/, :day => /\d+/
|
214
|
+
map.connect ':controller/:action/:id'
|
215
|
+
end
|
216
|
+
x = setup_for_named_route
|
217
|
+
# assert_equal(
|
218
|
+
# {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
|
219
|
+
# x.send(:article_url, :title => 'hi')
|
220
|
+
# )
|
221
|
+
assert_equal(
|
222
|
+
"http://test.host/page/2005/6/10/hi",
|
223
|
+
x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
|
224
|
+
)
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_changing_controller
|
228
|
+
set.draw {|map| map.connect ':controller/:action/:id' }
|
229
|
+
|
230
|
+
assert_equal '/admin/stuff/show/10', url_for(
|
231
|
+
{:controller => 'stuff', :action => 'show', :id => 10},
|
232
|
+
{:controller => 'admin/user', :action => 'index'}
|
233
|
+
)
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_paths_escaped
|
237
|
+
set.draw do |map|
|
238
|
+
map.path 'file/*path', :controller => 'content', :action => 'show_file'
|
239
|
+
map.connect ':controller/:action/:id'
|
240
|
+
end
|
241
|
+
|
242
|
+
# No + to space in URI escaping, only for query params.
|
243
|
+
results = recognize_path "/file/hello+world/how+are+you%3F"
|
244
|
+
assert results, "Recognition should have succeeded"
|
245
|
+
assert_equal ['hello+world', 'how+are+you?'], results[:path]
|
246
|
+
|
247
|
+
# Use %20 for space instead.
|
248
|
+
results = recognize_path "/file/hello%20world/how%20are%20you%3F"
|
249
|
+
assert results, "Recognition should have succeeded"
|
250
|
+
assert_equal ['hello world', 'how are you?'], results[:path]
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_paths_slashes_unescaped_with_ordered_parameters
|
254
|
+
set.draw do |map|
|
255
|
+
map.path '/file/*path', :controller => 'content'
|
256
|
+
end
|
257
|
+
|
258
|
+
# No / to %2F in URI, only for query params.
|
259
|
+
x = setup_for_named_route
|
260
|
+
assert_equal("/file/hello/world", x.send(:path_path, ['hello', 'world']))
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_non_controllers_cannot_be_matched
|
264
|
+
set.draw do |map|
|
265
|
+
map.connect ':controller/:action/:id'
|
266
|
+
end
|
267
|
+
assert_raise(ActionController::RoutingError) { recognize_path("/not_a/show/10") }
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_paths_do_not_accept_defaults
|
271
|
+
assert_raise(ActionController::RoutingError) do
|
272
|
+
set.draw do |map|
|
273
|
+
map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
|
274
|
+
map.connect ':controller/:action/:id'
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
set.draw do |map|
|
279
|
+
map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
|
280
|
+
map.connect ':controller/:action/:id'
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def test_should_list_options_diff_when_routing_requirements_dont_match
|
285
|
+
set.draw do |map|
|
286
|
+
map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
|
287
|
+
end
|
288
|
+
assert_raise(ActionController::RoutingError) { url_for(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_dynamic_path_allowed
|
292
|
+
set.draw do |map|
|
293
|
+
map.connect '*path', :controller => 'content', :action => 'show_file'
|
294
|
+
end
|
295
|
+
|
296
|
+
assert_equal '/pages/boo', url_for(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_dynamic_recall_paths_allowed
|
300
|
+
set.draw do |map|
|
301
|
+
map.connect '*path', :controller => 'content', :action => 'show_file'
|
302
|
+
end
|
303
|
+
|
304
|
+
assert_equal '/pages/boo', url_for({}, :controller => 'content', :action => 'show_file', :path => %w(pages boo))
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_backwards
|
308
|
+
set.draw do |map|
|
309
|
+
map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
|
310
|
+
map.connect ':controller/:action/:id'
|
311
|
+
end
|
312
|
+
|
313
|
+
assert_equal '/page/20', url_for({:id => 20}, {:controller => 'pages', :action => 'show'})
|
314
|
+
assert_equal '/page/20', url_for(:controller => 'pages', :id => 20, :action => 'show')
|
315
|
+
assert_equal '/pages/boo', url_for(:controller => 'pages', :action => 'boo')
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_route_with_fixnum_default
|
319
|
+
set.draw do |map|
|
320
|
+
map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
|
321
|
+
map.connect ':controller/:action/:id'
|
322
|
+
end
|
323
|
+
|
324
|
+
assert_equal '/page', url_for(:controller => 'content', :action => 'show_page')
|
325
|
+
assert_equal '/page', url_for(:controller => 'content', :action => 'show_page', :id => 1)
|
326
|
+
assert_equal '/page', url_for(:controller => 'content', :action => 'show_page', :id => '1')
|
327
|
+
assert_equal '/page/10', url_for(:controller => 'content', :action => 'show_page', :id => 10)
|
328
|
+
|
329
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, recognize_path("/page"))
|
330
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, recognize_path("/page/1"))
|
331
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, recognize_path("/page/10"))
|
332
|
+
end
|
333
|
+
|
334
|
+
# For newer revision
|
335
|
+
def test_route_with_text_default
|
336
|
+
set.draw do |map|
|
337
|
+
map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
|
338
|
+
map.connect ':controller/:action/:id'
|
339
|
+
end
|
340
|
+
|
341
|
+
assert_equal '/page/foo', url_for(:controller => 'content', :action => 'show_page', :id => 'foo')
|
342
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, recognize_path("/page/foo"))
|
343
|
+
|
344
|
+
token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
|
345
|
+
token.force_encoding(Encoding::BINARY) if token.respond_to?(:force_encoding)
|
346
|
+
escaped_token = CGI::escape(token)
|
347
|
+
|
348
|
+
assert_equal '/page/' + escaped_token, url_for(:controller => 'content', :action => 'show_page', :id => token)
|
349
|
+
assert_equal({:controller => "content", :action => 'show_page', :id => token}, recognize_path("/page/#{escaped_token}"))
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_action_expiry
|
353
|
+
set.draw {|map| map.connect ':controller/:action/:id' }
|
354
|
+
assert_equal '/content', url_for({:controller => 'content'}, {:controller => 'content', :action => 'show'})
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_requirement_should_prevent_optional_id
|
358
|
+
set.draw do |map|
|
359
|
+
map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
|
360
|
+
end
|
361
|
+
|
362
|
+
assert_equal '/post/10', url_for(:controller => 'post', :action => 'show', :id => 10)
|
363
|
+
|
364
|
+
assert_raise ActionController::RoutingError do
|
365
|
+
url_for(:controller => 'post', :action => 'show')
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
def test_both_requirement_and_optional
|
370
|
+
set.draw do |map|
|
371
|
+
map.blog('test/:year', :controller => 'post', :action => 'show',
|
372
|
+
:defaults => { :year => nil },
|
373
|
+
:requirements => { :year => /\d{4}/ }
|
374
|
+
)
|
375
|
+
map.connect ':controller/:action/:id'
|
376
|
+
end
|
377
|
+
|
378
|
+
assert_equal '/test', url_for(:controller => 'post', :action => 'show')
|
379
|
+
assert_equal '/test', url_for(:controller => 'post', :action => 'show', :year => nil)
|
380
|
+
|
381
|
+
x = setup_for_named_route
|
382
|
+
assert_equal("http://test.host/test",
|
383
|
+
x.send(:blog_url))
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_set_to_nil_forgets
|
387
|
+
set.draw do |map|
|
388
|
+
map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
|
389
|
+
map.connect ':controller/:action/:id'
|
390
|
+
end
|
391
|
+
|
392
|
+
assert_equal '/pages/2005',
|
393
|
+
url_for(:controller => 'content', :action => 'list_pages', :year => 2005)
|
394
|
+
assert_equal '/pages/2005/6',
|
395
|
+
url_for(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
|
396
|
+
assert_equal '/pages/2005/6/12',
|
397
|
+
url_for(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
|
398
|
+
|
399
|
+
assert_equal '/pages/2005/6/4',
|
400
|
+
url_for({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
|
401
|
+
|
402
|
+
assert_equal '/pages/2005/6',
|
403
|
+
url_for({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
|
404
|
+
|
405
|
+
assert_equal '/pages/2005',
|
406
|
+
url_for({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_url_with_no_action_specified
|
410
|
+
set.draw do |map|
|
411
|
+
map.connect '', :controller => 'content'
|
412
|
+
map.connect ':controller/:action/:id'
|
413
|
+
end
|
414
|
+
|
415
|
+
assert_equal '/', url_for(:controller => 'content', :action => 'index')
|
416
|
+
assert_equal '/', url_for(:controller => 'content')
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_named_url_with_no_action_specified
|
420
|
+
set.draw do |map|
|
421
|
+
map.home '', :controller => 'content'
|
422
|
+
map.connect ':controller/:action/:id'
|
423
|
+
end
|
424
|
+
|
425
|
+
assert_equal '/', url_for(:controller => 'content', :action => 'index')
|
426
|
+
assert_equal '/', url_for(:controller => 'content')
|
427
|
+
|
428
|
+
x = setup_for_named_route
|
429
|
+
assert_equal("http://test.host/",
|
430
|
+
x.send(:home_url))
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_url_generated_when_forgetting_action
|
434
|
+
[{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
|
435
|
+
set.draw do |map|
|
436
|
+
map.home '', hash
|
437
|
+
map.connect ':controller/:action/:id'
|
438
|
+
end
|
439
|
+
assert_equal '/', url_for({:action => nil}, {:controller => 'content', :action => 'hello'})
|
440
|
+
assert_equal '/', url_for({:controller => 'content'})
|
441
|
+
assert_equal '/content/hi', url_for({:controller => 'content', :action => 'hi'})
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_named_route_method
|
446
|
+
set.draw do |map|
|
447
|
+
map.categories 'categories', :controller => 'content', :action => 'categories'
|
448
|
+
map.connect ':controller/:action/:id'
|
449
|
+
end
|
450
|
+
|
451
|
+
assert_equal '/categories', url_for(:controller => 'content', :action => 'categories')
|
452
|
+
assert_equal '/content/hi', url_for({:controller => 'content', :action => 'hi'})
|
453
|
+
end
|
454
|
+
|
455
|
+
def test_named_routes_array
|
456
|
+
test_named_route_method
|
457
|
+
assert_equal [:categories], set.named_routes.names
|
458
|
+
end
|
459
|
+
|
460
|
+
def test_nil_defaults
|
461
|
+
set.draw do |map|
|
462
|
+
map.connect 'journal',
|
463
|
+
:controller => 'content',
|
464
|
+
:action => 'list_journal',
|
465
|
+
:date => nil, :user_id => nil
|
466
|
+
map.connect ':controller/:action/:id'
|
467
|
+
end
|
468
|
+
|
469
|
+
assert_equal '/journal', url_for(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
|
470
|
+
end
|
471
|
+
|
472
|
+
def setup_request_method_routes_for(method)
|
473
|
+
set.draw do |map|
|
474
|
+
map.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
|
475
|
+
map.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
|
476
|
+
map.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
|
477
|
+
map.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
481
|
+
%w(GET POST PUT DELETE).each do |request_method|
|
482
|
+
define_method("test_request_method_recognized_with_#{request_method}") do
|
483
|
+
setup_request_method_routes_for(request_method)
|
484
|
+
params = recognize_path("/match", :method => request_method)
|
485
|
+
assert_equal request_method.downcase, params[:action]
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
def test_recognize_array_of_methods
|
490
|
+
set.draw do |map|
|
491
|
+
map.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
|
492
|
+
map.connect '/match', :controller => 'books', :action => 'not_get_or_post'
|
493
|
+
end
|
494
|
+
|
495
|
+
params = recognize_path("/match", :method => :post)
|
496
|
+
assert_equal 'get_or_post', params[:action]
|
497
|
+
|
498
|
+
params = recognize_path("/match", :method => :put)
|
499
|
+
assert_equal 'not_get_or_post', params[:action]
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_subpath_recognized
|
503
|
+
set.draw do |map|
|
504
|
+
map.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
|
505
|
+
map.connect '/items/:id/:action', :controller => 'subpath_books'
|
506
|
+
map.connect '/posts/new/:action', :controller => 'subpath_books'
|
507
|
+
map.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
|
508
|
+
end
|
509
|
+
|
510
|
+
hash = recognize_path "/books/17/edit"
|
511
|
+
assert_not_nil hash
|
512
|
+
assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
|
513
|
+
|
514
|
+
hash = recognize_path "/items/3/complete"
|
515
|
+
assert_not_nil hash
|
516
|
+
assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
|
517
|
+
|
518
|
+
hash = recognize_path "/posts/new/preview"
|
519
|
+
assert_not_nil hash
|
520
|
+
assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
|
521
|
+
|
522
|
+
hash = recognize_path "/posts/7"
|
523
|
+
assert_not_nil hash
|
524
|
+
assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_subpath_generated
|
528
|
+
set.draw do |map|
|
529
|
+
map.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
|
530
|
+
map.connect '/items/:id/:action', :controller => 'subpath_books'
|
531
|
+
map.connect '/posts/new/:action', :controller => 'subpath_books'
|
532
|
+
end
|
533
|
+
|
534
|
+
assert_equal "/books/7/edit", url_for(:controller => "subpath_books", :id => 7, :action => "edit")
|
535
|
+
assert_equal "/items/15/complete", url_for(:controller => "subpath_books", :id => 15, :action => "complete")
|
536
|
+
assert_equal "/posts/new/preview", url_for(:controller => "subpath_books", :action => "preview")
|
537
|
+
end
|
538
|
+
|
539
|
+
def test_failed_requirements_raises_exception_with_violated_requirements
|
540
|
+
set.draw do |map|
|
541
|
+
map.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
|
542
|
+
end
|
543
|
+
|
544
|
+
x = setup_for_named_route
|
545
|
+
assert_raise(ActionController::RoutingError) do
|
546
|
+
x.send(:foo_with_requirement_url, "I am Against the requirements")
|
547
|
+
end
|
548
|
+
end
|
549
|
+
|
550
|
+
def test_routes_changed_correctly_after_clear
|
551
|
+
rs = ::ActionController::Routing::RouteSet.new
|
552
|
+
set.draw do |map|
|
553
|
+
map.connect 'ca', :controller => 'ca', :action => "aa"
|
554
|
+
map.connect 'cb', :controller => 'cb', :action => "ab"
|
555
|
+
map.connect 'cc', :controller => 'cc', :action => "ac"
|
556
|
+
map.connect ':controller/:action/:id'
|
557
|
+
map.connect ':controller/:action/:id.:format'
|
558
|
+
end
|
559
|
+
|
560
|
+
hash = recognize_path "/cc"
|
561
|
+
|
562
|
+
assert_not_nil hash
|
563
|
+
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
|
564
|
+
|
565
|
+
set.draw do |map|
|
566
|
+
map.connect 'cb', :controller => 'cb', :action => "ab"
|
567
|
+
map.connect 'cc', :controller => 'cc', :action => "ac"
|
568
|
+
map.connect ':controller/:action/:id'
|
569
|
+
map.connect ':controller/:action/:id.:format'
|
570
|
+
end
|
571
|
+
|
572
|
+
hash = recognize_path "/cc"
|
573
|
+
|
574
|
+
assert_not_nil hash
|
575
|
+
assert_equal %w(cc ac), [hash[:controller], hash[:action]]
|
576
|
+
|
577
|
+
end
|
578
|
+
end
|