actionpack 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

Files changed (84) hide show
  1. data/CHANGELOG +66 -0
  2. data/README +94 -64
  3. data/install.rb +1 -20
  4. data/lib/action_controller.rb +15 -7
  5. data/lib/action_controller/assertions/action_pack_assertions.rb +56 -3
  6. data/lib/action_controller/base.rb +137 -64
  7. data/lib/action_controller/caching.rb +11 -11
  8. data/lib/action_controller/cgi_ext/cgi_ext.rb +2 -2
  9. data/lib/action_controller/cgi_ext/raw_post_data_fix.rb +4 -4
  10. data/lib/action_controller/cgi_process.rb +9 -1
  11. data/lib/action_controller/components.rb +73 -0
  12. data/lib/action_controller/cookies.rb +1 -1
  13. data/lib/action_controller/dependencies.rb +6 -1
  14. data/lib/action_controller/filters.rb +1 -1
  15. data/lib/action_controller/flash.rb +3 -3
  16. data/lib/action_controller/helpers.rb +17 -21
  17. data/lib/action_controller/layout.rb +2 -2
  18. data/lib/action_controller/request.rb +16 -6
  19. data/lib/action_controller/rescue.rb +15 -3
  20. data/lib/action_controller/routing.rb +304 -0
  21. data/lib/action_controller/scaffolding.rb +10 -12
  22. data/lib/action_controller/session/active_record_store.rb +4 -7
  23. data/lib/action_controller/session/mem_cache_store.rb +2 -2
  24. data/lib/action_controller/templates/rescues/_request_and_response.rhtml +9 -1
  25. data/lib/action_controller/templates/rescues/diagnostics.rhtml +1 -1
  26. data/lib/action_controller/templates/rescues/routing_error.rhtml +8 -0
  27. data/lib/action_controller/test_process.rb +29 -7
  28. data/lib/action_controller/url_rewriter.rb +28 -112
  29. data/lib/action_view.rb +1 -1
  30. data/lib/action_view/base.rb +44 -17
  31. data/lib/action_view/helpers/active_record_helper.rb +1 -1
  32. data/lib/action_view/helpers/asset_tag_helper.rb +59 -0
  33. data/lib/action_view/helpers/date_helper.rb +24 -13
  34. data/lib/action_view/helpers/form_helper.rb +6 -1
  35. data/lib/action_view/helpers/form_options_helper.rb +87 -9
  36. data/lib/action_view/helpers/form_tag_helper.rb +80 -0
  37. data/lib/action_view/helpers/tag_helper.rb +0 -23
  38. data/lib/action_view/helpers/text_helper.rb +26 -1
  39. data/lib/action_view/helpers/url_helper.rb +29 -35
  40. data/lib/action_view/partials.rb +2 -2
  41. data/lib/action_view/vendor/builder/xmlbase.rb +3 -3
  42. data/rakefile +5 -2
  43. data/test/abstract_unit.rb +3 -1
  44. data/test/controller/action_pack_assertions_test.rb +29 -1
  45. data/test/controller/active_record_assertions_test.rb +109 -101
  46. data/test/controller/base_tests.rb +72 -0
  47. data/test/controller/components_test.rb +74 -0
  48. data/test/controller/cookie_test.rb +0 -9
  49. data/test/controller/custom_handler_test.rb +33 -0
  50. data/test/controller/filters_test.rb +36 -0
  51. data/test/controller/helper_test.rb +27 -10
  52. data/test/controller/redirect_test.rb +23 -31
  53. data/test/controller/render_test.rb +81 -66
  54. data/test/controller/request_test.rb +22 -0
  55. data/test/controller/routing_tests.rb +490 -0
  56. data/test/controller/{url_test.rb → url_obsolete.rb} +24 -14
  57. data/test/controller/url_obsolete.rb.rej +747 -0
  58. data/test/fixtures/fun/games/hello_world.rhtml +1 -0
  59. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  60. data/test/template/asset_tag_helper_test.rb +45 -0
  61. data/test/template/form_options_helper_test.rb +161 -1
  62. data/test/template/form_tag_helper_test.rb +22 -0
  63. data/test/template/text_helper_test.rb +7 -0
  64. data/test/template/url_helper_test.rb +5 -2
  65. data/test/template/url_helper_test.rb.rej +105 -0
  66. metadata +32 -27
  67. data/lib/action_controller/support/binding_of_caller.rb +0 -83
  68. data/lib/action_controller/support/breakpoint.rb +0 -518
  69. data/lib/action_controller/support/class_attribute_accessors.rb +0 -57
  70. data/lib/action_controller/support/class_inheritable_attributes.rb +0 -117
  71. data/lib/action_controller/support/clean_logger.rb +0 -10
  72. data/lib/action_controller/support/core_ext.rb +0 -1
  73. data/lib/action_controller/support/core_ext/hash.rb +0 -5
  74. data/lib/action_controller/support/core_ext/hash/keys.rb +0 -35
  75. data/lib/action_controller/support/core_ext/numeric.rb +0 -7
  76. data/lib/action_controller/support/core_ext/numeric/bytes.rb +0 -33
  77. data/lib/action_controller/support/core_ext/numeric/time.rb +0 -59
  78. data/lib/action_controller/support/core_ext/object_and_class.rb +0 -24
  79. data/lib/action_controller/support/core_ext/string.rb +0 -5
  80. data/lib/action_controller/support/core_ext/string/inflections.rb +0 -45
  81. data/lib/action_controller/support/dependencies.rb +0 -63
  82. data/lib/action_controller/support/inflector.rb +0 -84
  83. data/lib/action_controller/support/misc.rb +0 -8
  84. data/lib/action_controller/support/module_attribute_accessors.rb +0 -57
@@ -31,6 +31,28 @@ class RequestTest < Test::Unit::TestCase
31
31
  @request.port = 8080
32
32
  assert_equal ":8080", @request.port_string
33
33
  end
34
+
35
+ def test_request_uri
36
+ @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
37
+ assert_equal "/path/of/some/uri?mapped=1", @request.request_uri
38
+ assert_equal "/path/of/some/uri", @request.path
39
+
40
+ @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"
41
+ assert_equal "/path/of/some/uri", @request.request_uri
42
+ assert_equal "/path/of/some/uri", @request.path
43
+
44
+ @request.set_REQUEST_URI "/path/of/some/uri"
45
+ assert_equal "/path/of/some/uri", @request.request_uri
46
+ assert_equal "/path/of/some/uri", @request.path
47
+
48
+ @request.set_REQUEST_URI "/"
49
+ assert_equal "/", @request.request_uri
50
+ assert_equal "/", @request.path
51
+
52
+ @request.set_REQUEST_URI "/?m=b"
53
+ assert_equal "/?m=b", @request.request_uri
54
+ assert_equal "/", @request.path
55
+ end
34
56
 
35
57
  def test_host_with_port
36
58
  @request.env['HTTP_HOST'] = "rubyonrails.org:8080"
@@ -0,0 +1,490 @@
1
+ # Code Generated by ZenTest v. 2.3.0
2
+ # Couldn't find class for name Routing
3
+ # classname: asrt / meth = ratio%
4
+ # ActionController::Routing::RouteSet: 0 / 16 = 0.00%
5
+ # ActionController::Routing::RailsRoute: 0 / 4 = 0.00%
6
+ # ActionController::Routing::Route: 0 / 8 = 0.00%
7
+
8
+ require File.dirname(__FILE__) + '/../abstract_unit'
9
+ require 'test/unit'
10
+ require 'cgi'
11
+
12
+ class FakeController
13
+ attr_reader :controller_path
14
+ attr_reader :name
15
+ def initialize(name, controller_path)
16
+ @name = name
17
+ @controller_path = controller_path
18
+ end
19
+ def kind_of?(x)
20
+ x === Class || x == FakeController
21
+ end
22
+ end
23
+
24
+ module Controllers
25
+ module Admin
26
+ UserController = FakeController.new 'Admin::UserController', 'admin/user'
27
+ AccessController = FakeController.new 'Admin::AccessController', 'admin/access'
28
+ end
29
+ module Editing
30
+ PageController = FakeController.new 'Editing::PageController', 'editing/page'
31
+ ImageController = FakeController.new 'Editing::ImageController', 'editing/image'
32
+ end
33
+ module User
34
+ NewsController = FakeController.new 'User::NewsController', 'user/news'
35
+ PaymentController = FakeController.new 'User::PaymentController', 'user/payment'
36
+ end
37
+ ContentController = FakeController.new 'ContentController', 'content'
38
+ ResourceController = FakeController.new 'ResourceController', 'resource'
39
+ end
40
+
41
+ # Extend the modules with the required methods...
42
+ [Controllers, Controllers::Admin, Controllers::Editing, Controllers::User].each do |mod|
43
+ mod.instance_eval('alias :const_available? :const_defined?')
44
+ mod.constants.each {|k| Object.const_set(k, mod.const_get(k))} # export the modules & controller classes.
45
+ end
46
+
47
+
48
+ class RouteTests < Test::Unit::TestCase
49
+ def route(*args)
50
+ return @route if @route && (args.empty? || @args == args)
51
+ @args = args
52
+ @route = ActionController::Routing::Route.new(*args)
53
+ return @route
54
+ end
55
+
56
+ def setup
57
+ self.route '/:controller/:action/:id'
58
+ @defaults = {:controller => 'content', :action => 'show', :id => '314'}
59
+ end
60
+
61
+ # Don't put a leading / on the url.
62
+ # Make sure the controller is one from the above fake Controllers module.
63
+ def verify_recognize(url, expected_options, reason='')
64
+ url = url.split('/') if url.kind_of? String
65
+ reason = ": #{reason}" unless reason.empty?
66
+ controller_class, options = @route.recognize(url)
67
+ assert_not_equal nil, controller_class, "#{@route.inspect} didn't recognize #{url}#{reason}\n #{options}"
68
+ assert_equal expected_options, options, "#{@route.inspect} produced wrong options for #{url}#{reason}"
69
+ end
70
+
71
+ # The expected url should not have a leading /
72
+ # You can use @defaults if you want a set of plausible defaults
73
+ def verify_generate(expected_url, expected_extras, options, defaults, reason='')
74
+ reason = "#{reason}: " unless reason.empty?
75
+ components, extras = @route.generate(options, defaults)
76
+ assert_not_equal nil, components, "#{reason}#{@route.inspect} didn't generate for \n options = #{options.inspect}\n defaults = #{defaults.inspect}\n #{extras}"
77
+ assert_equal expected_extras, extras, "#{reason} #{@route.inspect}.generate: incorrect extra's"
78
+ assert_equal expected_url, components.join('/'), "#{reason} #{@route.inspect}.generate: incorrect url"
79
+ end
80
+
81
+ def test_recognize_default_unnested_with_action_and_id
82
+ verify_recognize('content/action/id', {:controller => 'content', :action => 'action', :id => 'id'})
83
+ verify_recognize('content/show/10', {:controller => 'content', :action => 'show', :id => '10'})
84
+ end
85
+ def test_generate_default_unnested_with_action_and_id_no_extras
86
+ verify_generate('content/action/id', {}, {:controller => 'content', :action => 'action', :id => 'id'}, @defaults)
87
+ verify_generate('content/show/10', {}, {:controller => 'content', :action => 'show', :id => '10'}, @defaults)
88
+ end
89
+ def test_generate_default_unnested_with_action_and_id
90
+ verify_generate('content/action/id', {:a => 'a'}, {:controller => 'content', :action => 'action', :id => 'id', :a => 'a'}, @defaults)
91
+ verify_generate('content/show/10', {:a => 'a'}, {:controller => 'content', :action => 'show', :id => '10', :a => 'a'}, @defaults)
92
+ end
93
+
94
+ # Note that we can't put tests here for proper relative controller handline
95
+ # because that is handled by RouteSet.
96
+ def test_recognize_default_nested_with_action_and_id
97
+ verify_recognize('admin/user/action/id', {:controller => 'admin/user', :action => 'action', :id => 'id'})
98
+ verify_recognize('admin/user/show/10', {:controller => 'admin/user', :action => 'show', :id => '10'})
99
+ end
100
+ def test_generate_default_nested_with_action_and_id_no_extras
101
+ verify_generate('admin/user/action/id', {}, {:controller => 'admin/user', :action => 'action', :id => 'id'}, @defaults)
102
+ verify_generate('admin/user/show/10', {}, {:controller => 'admin/user', :action => 'show', :id => '10'}, @defaults)
103
+ end
104
+ def test_generate_default_nested_with_action_and_id_relative_to_root
105
+ verify_generate('admin/user/action/id', {:a => 'a'}, {:controller => 'admin/user', :action => 'action', :id => 'id', :a => 'a'}, @defaults)
106
+ verify_generate('admin/user/show/10', {:a => 'a'}, {:controller => 'admin/user', :action => 'show', :id => '10', :a => 'a'}, @defaults)
107
+ end
108
+
109
+ def test_recognize_default_nested_with_action
110
+ verify_recognize('admin/user/action', {:controller => 'admin/user', :action => 'action'})
111
+ verify_recognize('admin/user/show', {:controller => 'admin/user', :action => 'show'})
112
+ end
113
+ def test_generate_default_nested_with_action_no_extras
114
+ verify_generate('admin/user/action', {}, {:controller => 'admin/user', :action => 'action'}, @defaults)
115
+ verify_generate('admin/user/show', {}, {:controller => 'admin/user', :action => 'show'}, @defaults)
116
+ end
117
+ def test_generate_default_nested_with_action
118
+ verify_generate('admin/user/action', {:a => 'a'}, {:controller => 'admin/user', :action => 'action', :a => 'a'}, @defaults)
119
+ verify_generate('admin/user/show', {:a => 'a'}, {:controller => 'admin/user', :action => 'show', :a => 'a'}, @defaults)
120
+ end
121
+
122
+ def test_recognize_default_nested_with_id_and_index
123
+ verify_recognize('admin/user/index/hello', {:controller => 'admin/user', :id => 'hello', :action => 'index'})
124
+ verify_recognize('admin/user/index/10', {:controller => 'admin/user', :id => "10", :action => 'index'})
125
+ end
126
+ def test_generate_default_nested_with_id_no_extras
127
+ verify_generate('admin/user/index/hello', {}, {:controller => 'admin/user', :id => 'hello'}, @defaults)
128
+ verify_generate('admin/user/index/10', {}, {:controller => 'admin/user', :id => 10}, @defaults)
129
+ end
130
+ def test_generate_default_nested_with_id
131
+ verify_generate('admin/user/index/hello', {:a => 'a'}, {:controller => 'admin/user', :id => 'hello', :a => 'a'}, @defaults)
132
+ verify_generate('admin/user/index/10', {:a => 'a'}, {:controller => 'admin/user', :id => 10, :a => 'a'}, @defaults)
133
+ end
134
+
135
+ def test_recognize_default_nested
136
+ verify_recognize('admin/user', {:controller => 'admin/user', :action => 'index'})
137
+ verify_recognize('admin/user', {:controller => 'admin/user', :action => 'index'})
138
+ end
139
+ def test_generate_default_nested_no_extras
140
+ verify_generate('admin/user', {}, {:controller => 'admin/user'}, @defaults)
141
+ verify_generate('admin/user', {}, {:controller => 'admin/user'}, @defaults)
142
+ end
143
+ def test_generate_default_nested
144
+ verify_generate('admin/user', {:a => 'a'}, {:controller => 'admin/user', :a => 'a'}, @defaults)
145
+ verify_generate('admin/user', {:a => 'a'}, {:controller => 'admin/user', :a => 'a'}, @defaults)
146
+ end
147
+
148
+ # Test generate with a default controller set.
149
+ def test_generate_default_controller
150
+ route '/:controller/:action/:id', :action => 'index', :id => nil, :controller => 'content'
151
+ @defaults[:controller] = 'resource'
152
+
153
+ verify_generate('', {}, {:controller => 'content'}, @defaults)
154
+ verify_generate('', {}, {:controller => 'content', :action => 'index'}, @defaults)
155
+ verify_generate('content/not-index', {}, {:controller => 'content', :action => 'not-index'}, @defaults)
156
+ verify_generate('content/index/10', {}, {:controller => 'content', :id => 10}, @defaults)
157
+ verify_generate('content/index/hi', {}, {:controller => 'content', :action => 'index', :id => 'hi'}, @defaults)
158
+ verify_generate('', {:a => 'a'}, {:controller => 'content', :a => 'a'}, @defaults)
159
+ verify_generate('', {:a => 'a'}, {:controller => 'content', :a => 'a'}, @defaults)
160
+
161
+ # Call some other generator tests
162
+ test_generate_default_unnested_with_action_and_id
163
+ test_generate_default_nested_with_action_and_id_no_extras
164
+ test_generate_default_nested_with_id
165
+ test_generate_default_nested_with_id_no_extras
166
+ end
167
+
168
+ # Test generate with a default controller set.
169
+ def test_generate_default_controller
170
+ route '/:controller/:action/:id', :action => 'index', :id => nil, :controller => 'content'
171
+ @defaults[:controller] = 'resource'
172
+ verify_recognize('', {:controller => 'content', :action => 'index'})
173
+ verify_recognize('content', {:controller => 'content', :action => 'index'})
174
+ verify_recognize('content/index', {:controller => 'content', :action => 'index'})
175
+ verify_recognize('content/index/10', {:controller => 'content', :action => 'index', :id => '10'})
176
+ end
177
+ # Make sure generation & recognition don't happen in some cases:
178
+ def test_no_generate_on_no_options
179
+ assert_equal nil, @route.generate({}, {})[0]
180
+ end
181
+ def test_requirements
182
+ route 'some_static/route', :controller => 'content'
183
+ assert_equal nil, @route.generate({}, {})[0]
184
+ assert_equal nil, @route.generate({:controller => "dog"}, {})[0]
185
+ assert_equal nil, @route.recognize([])[0]
186
+ assert_equal nil, @route.recognize(%w{some_static route with more than expected})[0]
187
+ end
188
+
189
+ def test_basecamp
190
+ route 'clients/', :controller => 'content'
191
+ verify_generate('clients', {}, {:controller => 'content'}, {}) # Would like to have clients/
192
+ verify_generate('clients', {}, {:controller => 'content'}, @defaults)
193
+ end
194
+
195
+ def test_regexp_requirements
196
+ const_options = {:controller => 'content', :action => 'by_date'}
197
+ route ':year/:month/:day', const_options.merge(:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/)
198
+ verify_recognize('2004/01/02', const_options.merge(:year => '2004', :month => '01', :day => '02'))
199
+ verify_recognize('2004/1/2', const_options.merge(:year => '2004', :month => '1', :day => '2'))
200
+ assert_equal nil, @route.recognize(%w{200 10 10})[0]
201
+ assert_equal nil, @route.recognize(%w{content show 10})[0]
202
+
203
+ verify_generate('2004/01/02', {}, const_options.merge(:year => '2004', :month => '01', :day => '02'), @defaults)
204
+ verify_generate('2004/1/2', {}, const_options.merge(:year => '2004', :month => '1', :day => '2'), @defaults)
205
+ assert_equal nil, @route.generate(const_options.merge(:year => '12004', :month => '01', :day => '02'), @defaults)[0]
206
+ end
207
+
208
+ def test_regexp_requirement_not_in_path
209
+ assert_raises(ArgumentError) {route 'constant/path', :controller => 'content', :action => 'by_date', :something => /\d+/}
210
+ end
211
+
212
+ def test_special_hash_names
213
+ route ':year/:name', :requirements => {:year => /\d{4}/, :controller => 'content'}, :defaults => {:name => 'ulysses'}, :action => 'show_bio_year'
214
+ verify_generate('1984', {}, {:controller => 'content', :action => 'show_bio_year', :year => 1984}, @defaults)
215
+ verify_generate('1984', {}, {:controller => 'content', :action => 'show_bio_year', :year => '1984'}, @defaults)
216
+ verify_generate('1984/odessys', {}, {:controller => 'content', :action => 'show_bio_year', :year => 1984, :name => 'odessys'}, @defaults)
217
+ verify_generate('1984/odessys', {}, {:controller => 'content', :action => 'show_bio_year', :year => '1984', :name => 'odessys'}, @defaults)
218
+
219
+ verify_recognize('1984/odessys', {:controller => 'content', :action => 'show_bio_year', :year => '1984', :name => 'odessys'})
220
+ verify_recognize('1984', {:controller => 'content', :action => 'show_bio_year', :year => '1984', :name => 'ulysses'})
221
+ end
222
+
223
+ def test_defaults_and_restrictions_for_items_not_in_path
224
+ assert_raises(ArgumentError) {route ':year/:name', :requirements => {:year => /\d{4}/}, :defaults => {:name => 'ulysses', :controller => 'content'}, :action => 'show_bio_year'}
225
+ assert_raises(ArgumentError) {route ':year/:name', :requirements => {:year => /\d{4}/, :imagine => /./}, :defaults => {:name => 'ulysses'}, :controller => 'content', :action => 'show_bio_year'}
226
+ end
227
+
228
+ def test_optionals_with_regexp
229
+ route ':year/:month/:day', :requirements => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/},
230
+ :defaults => {:month => nil, :day => nil},
231
+ :controller => 'content', :action => 'post_by_day'
232
+ verify_recognize('2005/06/12', {:controller => 'content', :action => 'post_by_day', :year => '2005', :month => '06', :day => '12'})
233
+ verify_recognize('2005/06', {:controller => 'content', :action => 'post_by_day', :year => '2005', :month => '06'})
234
+ verify_recognize('2005', {:controller => 'content', :action => 'post_by_day', :year => '2005'})
235
+
236
+ verify_generate('2005/06/12', {}, {:controller => 'content', :action => 'post_by_day', :year => '2005', :month => '06', :day => '12'}, @defaults)
237
+ verify_generate('2005/06', {}, {:controller => 'content', :action => 'post_by_day', :year => '2005', :month => '06'}, @defaults)
238
+ verify_generate('2005', {}, {:controller => 'content', :action => 'post_by_day', :year => '2005'}, @defaults)
239
+ end
240
+
241
+
242
+ def test_basecamp2
243
+ route 'clients/:client_name/:project_name/', :controller => 'content', :action => 'start_page_redirect'
244
+ verify_recognize('clients/projects/2', {:controller => 'content', :client_name => 'projects', :project_name => '2', :action => 'start_page_redirect'})
245
+ end
246
+
247
+ def test_xal_style_dates
248
+ route 'articles/:category/:year/:month/:day', :controller => 'content', :action => 'list_articles', :category => 'all', :year => nil, :month => nil, :day =>nil
249
+ verify_recognize('articles', {:controller => 'content', :action => 'list_articles', :category => 'all'})
250
+ verify_recognize('articles/porn', {:controller => 'content', :action => 'list_articles', :category => 'porn'})
251
+ verify_recognize('articles/news/2005/08', {:controller => 'content', :action => 'list_articles', :category => 'news', :year => '2005', :month => '08'})
252
+ verify_recognize('articles/news/2005/08/04', {:controller => 'content', :action => 'list_articles', :category => 'news', :year => '2005', :month => '08', :day => '04'})
253
+ assert_equal nil, @route.recognize(%w{articles too many components are here})[0]
254
+ assert_equal nil, @route.recognize('')[0]
255
+
256
+ verify_generate('articles', {}, {:controller => 'content', :action => 'list_articles'}, @defaults)
257
+ verify_generate('articles', {}, {:controller => 'content', :action => 'list_articles', :category => 'all'}, @defaults)
258
+ verify_generate('articles/news', {}, {:controller => 'content', :action => 'list_articles', :category => 'news'}, @defaults)
259
+ verify_generate('articles/news/2005', {}, {:controller => 'content', :action => 'list_articles', :category => 'news', :year => '2005'}, @defaults)
260
+ verify_generate('articles/news/2005/05', {}, {:controller => 'content', :action => 'list_articles', :category => 'news', :year => '2005', :month => '05'}, @defaults)
261
+ verify_generate('articles/news/2005/05/16', {}, {:controller => 'content', :action => 'list_articles', :category => 'news', :year => '2005', :month => '05', :day => '16'}, @defaults)
262
+
263
+ assert_equal nil, @route.generate({:controller => 'content', :action => 'list_articles', :day => '2'}, @defaults)[0]
264
+ # The above case should fail because a nil value cannot be present in a path.
265
+ # In other words, since :day is given, :month and :year must be given too.
266
+ end
267
+
268
+
269
+ def test_no_controller
270
+ route 'some/:special/:route', :controller => 'a/missing/controller', :action => 'anything'
271
+ assert_raises(ActionController::RoutingError, "Should raise due to nonexistant controller") {@route.recognize(%w{some matching path})}
272
+ end
273
+ def test_bad_controller_path
274
+ assert_equal nil, @route.recognize(%w{no such controller fake_action id})[0]
275
+ end
276
+ def test_too_short_path
277
+ assert_equal nil, @route.recognize([])[0]
278
+ route 'some/static/route', :controller => 'content', :action => 'show'
279
+ assert_equal nil, route.recognize([])[0]
280
+ end
281
+ def test_too_long_path
282
+ assert_equal nil, @route.recognize(%w{content action id some extra components})[0]
283
+ end
284
+ def test_incorrect_static_component
285
+ route 'some/static/route', :controller => 'content', :action => 'show'
286
+ assert_equal nil, route.recognize(%w{an non_matching path})[0]
287
+ end
288
+ def test_no_controller_defined
289
+ route 'some/:path/:without/a/controller'
290
+ assert_equal nil, route.recognize(%w{some matching path a controller})[0]
291
+ end
292
+
293
+ def test_mismatching_requirements
294
+ route 'some/path', :controller => 'content', :action => 'fish'
295
+ assert_equal nil, route.generate({:controller => 'admin/user', :action => 'list'})[0]
296
+ assert_equal nil, route.generate({:controller => 'content', :action => 'list'})[0]
297
+ assert_equal nil, route.generate({:controller => 'admin/user', :action => 'fish'})[0]
298
+ end
299
+
300
+ def test_missing_value_for_generate
301
+ assert_equal nil, route.generate({})[0] # :controller is missing
302
+ end
303
+ def test_nils_inside_generated_path
304
+ route 'show/:year/:month/:day', :month => nil, :day => nil, :controller => 'content', :action => 'by_date'
305
+ assert_equal nil, route.generate({:year => 2005, :day => 10})[0]
306
+ end
307
+
308
+ def test_expand_controller_path_non_nested_no_leftover
309
+ controller, leftovers = @route.send :eat_path_to_controller, %w{content}
310
+ assert_equal Controllers::ContentController, controller
311
+ assert_equal [], leftovers
312
+ end
313
+ def test_expand_controller_path_non_nested_with_leftover
314
+ controller, leftovers = @route.send :eat_path_to_controller, %w{content action id}
315
+ assert_equal Controllers::ContentController, controller
316
+ assert_equal %w{action id}, leftovers
317
+ end
318
+ def test_expand_controller_path_nested_no_leftover
319
+ controller, leftovers = @route.send :eat_path_to_controller, %w{admin user}
320
+ assert_equal Controllers::Admin::UserController, controller
321
+ assert_equal [], leftovers
322
+ end
323
+ def test_expand_controller_path_nested_no_leftover
324
+ controller, leftovers = @route.send :eat_path_to_controller, %w{admin user action id}
325
+ assert_equal Controllers::Admin::UserController, controller
326
+ assert_equal %w{action id}, leftovers
327
+ end
328
+
329
+ def test_special_characters
330
+ route ':id', :controller => 'content', :action => 'fish'
331
+ verify_recognize'id+with+spaces',
332
+ :controller => 'content', :action => 'fish', :id => 'id with spaces'
333
+ verify_generate('id+with+spaces', {},
334
+ {:controller => 'content', :action => 'fish', :id => 'id with spaces'}, {})
335
+ verify_recognize 'id%2Fwith%2Fslashes',
336
+ :controller => 'content', :action => 'fish', :id => 'id/with/slashes'
337
+ verify_generate('id%2Fwith%2Fslashes', {},
338
+ {:controller => 'content', :action => 'fish', :id => 'id/with/slashes'}, {})
339
+ end
340
+ end
341
+
342
+ class RouteSetTests < Test::Unit::TestCase
343
+ def setup
344
+ @set = ActionController::Routing::RouteSet.new
345
+ @rails_route = ActionController::Routing::Route.new '/:controller/:action/:id', :action => 'index', :id => nil
346
+ @request = ActionController::TestRequest.new({}, {}, nil)
347
+ end
348
+ def test_emptyness
349
+ assert_equal true, @set.empty?, "New RouteSets should respond to empty? with true."
350
+ @set.each { flunk "New RouteSets should be empty." }
351
+ end
352
+ def test_add_illegal_route
353
+ assert_raises(TypeError) {@set.add_route "I'm not actually a route."}
354
+ end
355
+ def test_add_normal_route
356
+ @set.add_route @rails_route
357
+ seen = false
358
+ @set.each do |route|
359
+ assert_equal @rails_route, route
360
+ flunk("Each should have yielded only a single route!") if seen
361
+ seen = true
362
+ end
363
+ end
364
+
365
+ def test_expand_controller_path_non_relative
366
+ defaults = {:controller => 'admin/user', :action => 'list'}
367
+ options = {:controller => '/content'}
368
+ @set.expand_controller_path!(options, defaults)
369
+ assert_equal({:controller => 'content'}, options)
370
+ end
371
+ def test_expand_controller_path_relative_to_nested
372
+ defaults = {:controller => 'admin/user', :action => 'list'}
373
+ options = {:controller => 'access'}
374
+ @set.expand_controller_path!(options, defaults)
375
+ assert_equal({:controller => 'admin/access'}, options)
376
+ end
377
+ def test_expand_controller_path_relative_to_root
378
+ defaults = {:controller => 'content', :action => 'list'}
379
+ options = {:controller => 'resource'}
380
+ @set.expand_controller_path!(options, defaults)
381
+ assert_equal({:controller => 'resource'}, options)
382
+ end
383
+ def test_expand_controller_path_into_module
384
+ defaults = {:controller => 'content', :action => 'list'}
385
+ options = {:controller => 'admin/user'}
386
+ @set.expand_controller_path!(options, defaults)
387
+ assert_equal({:controller => 'admin/user'}, options)
388
+ end
389
+ def test_expand_controller_path_switch_module_with_absolute
390
+ defaults = {:controller => 'user/news', :action => 'list'}
391
+ options = {:controller => '/admin/user'}
392
+ @set.expand_controller_path!(options, defaults)
393
+ assert_equal({:controller => 'admin/user'}, options)
394
+ end
395
+ def test_expand_controller_no_default
396
+ options = {:controller => 'content'}
397
+ @set.expand_controller_path!(options, {})
398
+ assert_equal({:controller => 'content'}, options)
399
+ end
400
+
401
+ # Don't put a leading / on the url.
402
+ # Make sure the controller is one from the above fake Controllers module.
403
+ def verify_recognize(expected_controller, expected_path_parameters=nil, path=nil)
404
+ @set.add_route(@rails_route) if @set.empty?
405
+ @request.path = path if path
406
+ controller = @set.recognize!(@request)
407
+ assert_equal expected_controller, controller
408
+ assert_equal expected_path_parameters, @request.path_parameters if expected_path_parameters
409
+ end
410
+
411
+ # The expected url should not have a leading /
412
+ # You can use @defaults if you want a set of plausible defaults
413
+ def verify_generate(expected_url, options, expected_extras={})
414
+ @set.add_route(@rails_route) if @set.empty?
415
+ components, extras = @set.generate(options, @request)
416
+ assert_equal expected_extras, extras, "#incorrect extra's"
417
+ assert_equal expected_url, components.join('/'), "incorrect url"
418
+ end
419
+ def typical_request
420
+ @request.path_parameters = {:controller => 'content', :action => 'show', :id => '10'}
421
+ end
422
+ def typical_nested_request
423
+ @request.path_parameters = {:controller => 'admin/user', :action => 'grant', :id => '02seckar'}
424
+ end
425
+
426
+ def test_generate_typical_controller_action_path
427
+ typical_request
428
+ verify_generate('content/list', {:controller => 'content', :action => 'list'})
429
+ end
430
+ def test_generate_typical_controller_index_path_explicit_index
431
+ typical_request
432
+ verify_generate('content', {:controller => 'content', :action => 'index'})
433
+ end
434
+ def test_generate_typical_controller_index_path_explicit_index
435
+ typical_request
436
+ verify_generate('content', {:controller => 'content', :action => 'index'})
437
+ end
438
+ def test_generate_typical_controller_index_path_implicit_index
439
+ typical_request
440
+ @request.path_parameters[:controller] = 'resource'
441
+ verify_generate('content', {:controller => 'content'})
442
+ end
443
+
444
+ def test_generate_no_perfect_route
445
+ typical_request
446
+ verify_generate('admin/user/show/43seckar', {:controller => 'admin/user', :action => 'show', :id => '43seckar', :likes_fishing => 'fuzzy(0.3)'}, {:likes_fishing => 'fuzzy(0.3)'})
447
+ end
448
+
449
+ def test_generate_no_match
450
+ @set.add_route(@rails_route)
451
+ @request.path_parameters = {}
452
+ assert_raises(ActionController::RoutingError) {@set.generate({}, @request)}
453
+ end
454
+
455
+ def test_encoded_strings
456
+ verify_recognize(Controllers::Admin::UserController, {:controller => 'admin/user', :action => 'info', :id => "Nicholas Seckar"}, path='/admin/user/info/Nicholas%20Seckar')
457
+ end
458
+
459
+ def test_action_dropped_when_controller_changes
460
+ @request.path_parameters = {:controller => 'content', :action => 'list'}
461
+ options = {:controller => 'resource'}
462
+ @set.connect ':action/:controller'
463
+ verify_generate('index/resource', options)
464
+ end
465
+
466
+ def test_action_dropped_when_controller_given
467
+ @request.path_parameters = {:controller => 'content', :action => 'list'}
468
+ options = {:controller => 'content'}
469
+ @set.connect ':action/:controller'
470
+ verify_generate('index/content', options)
471
+ end
472
+
473
+ def test_default_dropped_with_nil_option
474
+ @request.path_parameters = {:controller => 'content', :action => 'action', :id => '10'}
475
+ verify_generate 'content/action', {:id => nil}
476
+ end
477
+
478
+ def test_url_to_self
479
+ @request.path_parameters = {:controller => 'admin/users', :action => 'index'}
480
+ verify_generate 'admin/users', {}
481
+ end
482
+ end
483
+
484
+ #require '../assertions/action_pack_assertions.rb'
485
+ class AssertionRoutingTests < Test::Unit::TestCase
486
+ def test_assert_routing
487
+ ActionController::Routing::Routes.reload rescue nil
488
+ assert_routing('content', {:controller => 'content', :action => 'index'})
489
+ end
490
+ end