actionpack 1.12.5 → 1.13.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 (179) hide show
  1. data/CHANGELOG +517 -15
  2. data/MIT-LICENSE +1 -1
  3. data/README +18 -20
  4. data/Rakefile +7 -4
  5. data/examples/address_book_controller.rb +3 -3
  6. data/examples/blog_controller.cgi +3 -3
  7. data/examples/debate_controller.cgi +5 -5
  8. data/lib/action_controller.rb +2 -2
  9. data/lib/action_controller/assertions.rb +73 -311
  10. data/lib/action_controller/{deprecated_assertions.rb → assertions/deprecated_assertions.rb} +32 -8
  11. data/lib/action_controller/assertions/dom_assertions.rb +25 -0
  12. data/lib/action_controller/assertions/model_assertions.rb +12 -0
  13. data/lib/action_controller/assertions/response_assertions.rb +140 -0
  14. data/lib/action_controller/assertions/routing_assertions.rb +82 -0
  15. data/lib/action_controller/assertions/selector_assertions.rb +571 -0
  16. data/lib/action_controller/assertions/tag_assertions.rb +117 -0
  17. data/lib/action_controller/base.rb +334 -163
  18. data/lib/action_controller/benchmarking.rb +3 -6
  19. data/lib/action_controller/caching.rb +83 -22
  20. data/lib/action_controller/cgi_ext/cgi_ext.rb +0 -7
  21. data/lib/action_controller/cgi_ext/cgi_methods.rb +167 -173
  22. data/lib/action_controller/cgi_ext/raw_post_data_fix.rb +43 -22
  23. data/lib/action_controller/cgi_process.rb +50 -27
  24. data/lib/action_controller/components.rb +21 -25
  25. data/lib/action_controller/cookies.rb +10 -9
  26. data/lib/action_controller/{dependencies.rb → deprecated_dependencies.rb} +9 -27
  27. data/lib/action_controller/filters.rb +448 -225
  28. data/lib/action_controller/flash.rb +24 -20
  29. data/lib/action_controller/helpers.rb +2 -5
  30. data/lib/action_controller/integration.rb +40 -16
  31. data/lib/action_controller/layout.rb +11 -8
  32. data/lib/action_controller/macros/auto_complete.rb +3 -2
  33. data/lib/action_controller/macros/in_place_editing.rb +3 -2
  34. data/lib/action_controller/mime_responds.rb +41 -29
  35. data/lib/action_controller/mime_type.rb +68 -10
  36. data/lib/action_controller/pagination.rb +4 -3
  37. data/lib/action_controller/request.rb +22 -14
  38. data/lib/action_controller/rescue.rb +25 -22
  39. data/lib/action_controller/resources.rb +302 -0
  40. data/lib/action_controller/response.rb +20 -2
  41. data/lib/action_controller/response.rb.rej +17 -0
  42. data/lib/action_controller/routing.rb +1165 -567
  43. data/lib/action_controller/scaffolding.rb +30 -31
  44. data/lib/action_controller/session/active_record_store.rb +2 -0
  45. data/lib/action_controller/session/drb_store.rb +4 -0
  46. data/lib/action_controller/session/mem_cache_store.rb +4 -0
  47. data/lib/action_controller/session_management.rb +6 -9
  48. data/lib/action_controller/status_codes.rb +89 -0
  49. data/lib/action_controller/streaming.rb +6 -15
  50. data/lib/action_controller/templates/rescues/_request_and_response.rhtml +5 -5
  51. data/lib/action_controller/templates/rescues/diagnostics.rhtml +2 -2
  52. data/lib/action_controller/templates/rescues/routing_error.rhtml +4 -4
  53. data/lib/action_controller/templates/rescues/template_error.rhtml +1 -1
  54. data/lib/action_controller/templates/scaffolds/list.rhtml +1 -1
  55. data/lib/action_controller/test_process.rb +52 -30
  56. data/lib/action_controller/url_rewriter.rb +63 -29
  57. data/lib/action_controller/vendor/html-scanner/html/document.rb +1 -0
  58. data/lib/action_controller/vendor/html-scanner/html/node.rb +3 -4
  59. data/lib/action_controller/vendor/html-scanner/html/selector.rb +822 -0
  60. data/lib/action_controller/verification.rb +22 -11
  61. data/lib/action_pack.rb +1 -1
  62. data/lib/action_pack/version.rb +2 -2
  63. data/lib/action_view.rb +1 -1
  64. data/lib/action_view/base.rb +46 -43
  65. data/lib/action_view/compiled_templates.rb +1 -1
  66. data/lib/action_view/helpers/active_record_helper.rb +54 -17
  67. data/lib/action_view/helpers/asset_tag_helper.rb +97 -46
  68. data/lib/action_view/helpers/capture_helper.rb +1 -1
  69. data/lib/action_view/helpers/date_helper.rb +258 -136
  70. data/lib/action_view/helpers/debug_helper.rb +1 -1
  71. data/lib/action_view/helpers/deprecated_helper.rb +34 -0
  72. data/lib/action_view/helpers/form_helper.rb +75 -35
  73. data/lib/action_view/helpers/form_options_helper.rb +7 -5
  74. data/lib/action_view/helpers/form_tag_helper.rb +44 -6
  75. data/lib/action_view/helpers/java_script_macros_helper.rb +59 -46
  76. data/lib/action_view/helpers/javascript_helper.rb +71 -10
  77. data/lib/action_view/helpers/javascripts/controls.js +41 -23
  78. data/lib/action_view/helpers/javascripts/dragdrop.js +105 -76
  79. data/lib/action_view/helpers/javascripts/effects.js +293 -163
  80. data/lib/action_view/helpers/javascripts/prototype.js +897 -389
  81. data/lib/action_view/helpers/javascripts/prototype.js.rej +561 -0
  82. data/lib/action_view/helpers/number_helper.rb +111 -65
  83. data/lib/action_view/helpers/prototype_helper.rb +84 -109
  84. data/lib/action_view/helpers/scriptaculous_helper.rb +5 -0
  85. data/lib/action_view/helpers/tag_helper.rb +69 -16
  86. data/lib/action_view/helpers/text_helper.rb +149 -112
  87. data/lib/action_view/helpers/url_helper.rb +200 -107
  88. data/lib/action_view/template_error.rb +66 -42
  89. data/test/abstract_unit.rb +4 -2
  90. data/test/active_record_unit.rb +84 -56
  91. data/test/activerecord/active_record_assertions_test.rb +26 -18
  92. data/test/activerecord/active_record_store_test.rb +4 -36
  93. data/test/activerecord/pagination_test.rb +1 -6
  94. data/test/controller/action_pack_assertions_test.rb +230 -113
  95. data/test/controller/addresses_render_test.rb +2 -6
  96. data/test/controller/assert_select_test.rb +576 -0
  97. data/test/controller/base_test.rb +73 -3
  98. data/test/controller/caching_test.rb +228 -0
  99. data/test/controller/capture_test.rb +12 -10
  100. data/test/controller/cgi_test.rb +89 -12
  101. data/test/controller/components_test.rb +24 -2
  102. data/test/controller/content_type_test.rb +139 -0
  103. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  104. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  105. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  106. data/test/controller/cookie_test.rb +33 -25
  107. data/test/controller/deprecated_instance_variables_test.rb +48 -0
  108. data/test/controller/deprecation/deprecated_base_methods_test.rb +60 -0
  109. data/test/controller/fake_controllers.rb +0 -1
  110. data/test/controller/filters_test.rb +301 -16
  111. data/test/controller/flash_test.rb +19 -2
  112. data/test/controller/helper_test.rb +2 -2
  113. data/test/controller/integration_test.rb +154 -0
  114. data/test/controller/layout_test.rb +115 -1
  115. data/test/controller/mime_responds_test.rb +94 -0
  116. data/test/controller/mime_type_test.rb +9 -0
  117. data/test/controller/new_render_test.rb +161 -11
  118. data/test/controller/raw_post_test.rb +52 -15
  119. data/test/controller/redirect_test.rb +27 -14
  120. data/test/controller/render_test.rb +76 -29
  121. data/test/controller/request_test.rb +55 -4
  122. data/test/controller/resources_test.rb +274 -0
  123. data/test/controller/routing_test.rb +1533 -824
  124. data/test/controller/selector_test.rb +628 -0
  125. data/test/controller/send_file_test.rb +9 -1
  126. data/test/controller/session_management_test.rb +51 -0
  127. data/test/controller/test_test.rb +113 -29
  128. data/test/controller/url_rewriter_test.rb +86 -17
  129. data/test/controller/verification_test.rb +19 -17
  130. data/test/controller/webservice_test.rb +0 -7
  131. data/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +1 -0
  132. data/test/fixtures/content_type/render_default_for_rhtml.rhtml +1 -0
  133. data/test/fixtures/content_type/render_default_for_rjs.rjs +1 -0
  134. data/test/fixtures/content_type/render_default_for_rxml.rxml +1 -0
  135. data/test/fixtures/deprecated_instance_variables/_cookies_ivar.rhtml +1 -0
  136. data/test/fixtures/deprecated_instance_variables/_cookies_method.rhtml +1 -0
  137. data/test/fixtures/deprecated_instance_variables/_flash_ivar.rhtml +1 -0
  138. data/test/fixtures/deprecated_instance_variables/_flash_method.rhtml +1 -0
  139. data/test/fixtures/deprecated_instance_variables/_headers_ivar.rhtml +1 -0
  140. data/test/fixtures/deprecated_instance_variables/_headers_method.rhtml +1 -0
  141. data/test/fixtures/deprecated_instance_variables/_params_ivar.rhtml +1 -0
  142. data/test/fixtures/deprecated_instance_variables/_params_method.rhtml +1 -0
  143. data/test/fixtures/deprecated_instance_variables/_request_ivar.rhtml +1 -0
  144. data/test/fixtures/deprecated_instance_variables/_request_method.rhtml +1 -0
  145. data/test/fixtures/deprecated_instance_variables/_response_ivar.rhtml +1 -0
  146. data/test/fixtures/deprecated_instance_variables/_response_method.rhtml +1 -0
  147. data/test/fixtures/deprecated_instance_variables/_session_ivar.rhtml +1 -0
  148. data/test/fixtures/deprecated_instance_variables/_session_method.rhtml +1 -0
  149. data/test/fixtures/multipart/binary_file +0 -0
  150. data/test/fixtures/public/javascripts/application.js +1 -0
  151. data/test/fixtures/test/_hello.rxml +1 -0
  152. data/test/fixtures/test/hello_world_container.rxml +3 -0
  153. data/test/fixtures/topic.rb +2 -2
  154. data/test/template/active_record_helper_test.rb +83 -12
  155. data/test/template/asset_tag_helper_test.rb +75 -95
  156. data/test/template/compiled_templates_test.rb +1 -0
  157. data/test/template/date_helper_test.rb +873 -181
  158. data/test/template/deprecated_helper_test.rb +36 -0
  159. data/test/template/deprecated_instance_variables_test.rb +43 -0
  160. data/test/template/form_helper_test.rb +77 -1
  161. data/test/template/form_options_helper_test.rb +4 -0
  162. data/test/template/form_tag_helper_test.rb +66 -2
  163. data/test/template/java_script_macros_helper_test.rb +4 -1
  164. data/test/template/javascript_helper_test.rb +29 -0
  165. data/test/template/number_helper_test.rb +63 -27
  166. data/test/template/prototype_helper_test.rb +77 -34
  167. data/test/template/tag_helper_test.rb +34 -6
  168. data/test/template/text_helper_test.rb +69 -34
  169. data/test/template/url_helper_test.rb +168 -16
  170. data/test/testing_sandbox.rb +7 -22
  171. metadata +66 -20
  172. data/filler.txt +0 -50
  173. data/lib/action_controller/code_generation.rb +0 -235
  174. data/lib/action_controller/vendor/xml_simple.rb +0 -1019
  175. data/test/controller/caching_filestore.rb +0 -74
  176. data/test/fixtures/application_root/app/controllers/a_class_that_contains_a_controller/poorly_placed_controller.rb +0 -7
  177. data/test/fixtures/application_root/app/controllers/module_that_holds_controllers/nested_controller.rb +0 -3
  178. data/test/fixtures/application_root/app/models/a_class_that_contains_a_controller.rb +0 -7
  179. data/test/fixtures/dont_load.rb +0 -3
@@ -63,6 +63,14 @@ class SendFileTest < Test::Unit::TestCase
63
63
  assert_equal file_data, response.body
64
64
  end
65
65
 
66
+ def test_headers_after_send_shouldnt_include_charset
67
+ response = process('data')
68
+ assert_equal "application/octet-stream", response.headers["Content-Type"]
69
+
70
+ response = process('file')
71
+ assert_equal "application/octet-stream", response.headers["Content-Type"]
72
+ end
73
+
66
74
  # Test that send_file_headers! is setting the correct HTTP headers.
67
75
  def test_send_file_headers!
68
76
  options = {
@@ -97,7 +105,7 @@ class SendFileTest < Test::Unit::TestCase
97
105
  define_method "test_send_#{method}_status" do
98
106
  @controller.options = { :stream => false, :status => 500 }
99
107
  assert_nothing_raised { assert_not_nil process(method) }
100
- assert_equal '500', @controller.headers['Status']
108
+ assert_equal '500 Internal Server Error', @controller.headers['Status']
101
109
  end
102
110
 
103
111
  define_method "test_default_send_#{method}_status" do
@@ -44,6 +44,49 @@ class SessionManagementTest < Test::Unit::TestCase
44
44
  end
45
45
  end
46
46
 
47
+ class AssociationCachingTestController < ActionController::Base
48
+ class ObjectWithAssociationCache
49
+ def initialize
50
+ @cached_associations = false
51
+ end
52
+
53
+ def fetch_associations
54
+ @cached_associations = true
55
+ end
56
+
57
+ def clear_association_cache
58
+ @cached_associations = false
59
+ end
60
+
61
+ def has_cached_associations?
62
+ @cached_associations
63
+ end
64
+ end
65
+
66
+ def show
67
+ session[:object] = ObjectWithAssociationCache.new
68
+ session[:object].fetch_associations
69
+ if session[:object].has_cached_associations?
70
+ render :text => "has cached associations"
71
+ else
72
+ render :text => "does not have cached associations"
73
+ end
74
+ end
75
+
76
+ def tell
77
+ if session[:object]
78
+ if session[:object].has_cached_associations?
79
+ render :text => "has cached associations"
80
+ else
81
+ render :text => "does not have cached associations"
82
+ end
83
+ else
84
+ render :text => "there is no object"
85
+ end
86
+ end
87
+ end
88
+
89
+
47
90
  def setup
48
91
  @request, @response = ActionController::TestRequest.new,
49
92
  ActionController::TestResponse.new
@@ -91,4 +134,12 @@ class SessionManagementTest < Test::Unit::TestCase
91
134
  assert_equal CGI::Session::ActiveRecordStore, ActionController::Base.session_store
92
135
  end
93
136
  end
137
+
138
+ def test_process_cleanup_with_session_management_support
139
+ @controller = AssociationCachingTestController.new
140
+ get :show
141
+ assert_equal "has cached associations", @response.body
142
+ get :tell
143
+ assert_equal "does not have cached associations", @response.body
144
+ end
94
145
  end
@@ -15,8 +15,8 @@ class TestTest < Test::Unit::TestCase
15
15
 
16
16
  def test_params
17
17
  render :text => params.inspect
18
- end
19
-
18
+ end
19
+
20
20
  def test_uri
21
21
  render :text => request.request_uri
22
22
  end
@@ -41,7 +41,7 @@ class TestTest < Test::Unit::TestCase
41
41
  </html>
42
42
  HTML
43
43
  end
44
-
44
+
45
45
  def test_only_one_param
46
46
  render :text => (params[:left] && params[:right]) ? "EEP, Both here!" : "OK"
47
47
  end
@@ -49,7 +49,7 @@ HTML
49
49
  def test_remote_addr
50
50
  render :text => (request.remote_addr || "not specified")
51
51
  end
52
-
52
+
53
53
  def test_file_upload
54
54
  render :text => params[:file].size
55
55
  end
@@ -58,8 +58,20 @@ HTML
58
58
  redirect_to :generate_url, :id => 5
59
59
  end
60
60
 
61
+ def redirect_to_same_controller
62
+ redirect_to :controller => 'test', :action => 'test_uri', :id => 5
63
+ end
64
+
65
+ def redirect_to_different_controller
66
+ redirect_to :controller => 'fail', :id => 5
67
+ end
68
+
69
+ def create
70
+ headers['Location'] = 'created resource'
71
+ head :created
72
+ end
73
+
61
74
  private
62
-
63
75
  def rescue_action(e)
64
76
  raise e
65
77
  end
@@ -74,6 +86,7 @@ HTML
74
86
  @request = ActionController::TestRequest.new
75
87
  @response = ActionController::TestResponse.new
76
88
  ActionController::Routing::Routes.reload
89
+ ActionController::Routing.use_controllers! %w(content admin/user test_test/test)
77
90
  end
78
91
 
79
92
  def teardown
@@ -91,8 +104,8 @@ HTML
91
104
  def test_process_without_flash
92
105
  process :set_flash
93
106
  assert_equal '><', flash['test']
94
- end
95
-
107
+ end
108
+
96
109
  def test_process_with_flash
97
110
  process :set_flash, nil, nil, { "test" => "value" }
98
111
  assert_equal '>value<', flash['test']
@@ -112,12 +125,12 @@ HTML
112
125
  @request.set_REQUEST_URI "/explicit/uri"
113
126
  process :test_uri, :id => 7
114
127
  assert_equal "/explicit/uri", @response.body
115
- end
116
-
117
- def test_multiple_calls
128
+ end
129
+
130
+ def test_multiple_calls
118
131
  process :test_only_one_param, :left => true
119
132
  assert_equal "OK", @response.body
120
- process :test_only_one_param, :right => true
133
+ process :test_only_one_param, :right => true
121
134
  assert_equal "OK", @response.body
122
135
  end
123
136
 
@@ -207,6 +220,8 @@ HTML
207
220
 
208
221
  # there is a tag with 2 children
209
222
  assert_tag :children => { :count => 2 }
223
+ # in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
224
+ assert_tag :tag => 'ul', :children => { :count => 2 }
210
225
  # there is no tag with 4 children
211
226
  assert_no_tag :children => { :count => 4 }
212
227
  end
@@ -233,7 +248,7 @@ HTML
233
248
  process :test_html_output
234
249
 
235
250
  # there is a tag containing only one child with an id of 'foo'
236
- assert_tag :children => { :count => 1,
251
+ assert_tag :children => { :count => 1,
237
252
  :only => { :attributes => { :id => "foo" } } }
238
253
  # there is no tag containing only one 'li' child
239
254
  assert_no_tag :children => { :count => 1, :only => { :tag => "li" } }
@@ -243,11 +258,11 @@ HTML
243
258
  process :test_html_output
244
259
 
245
260
  # the output contains the string "Name"
246
- assert_tag :content => "Name"
261
+ assert_tag :content => /Name/
247
262
  # the output does not contain the string "test"
248
- assert_no_tag :content => "test"
263
+ assert_no_tag :content => /test/
249
264
  end
250
-
265
+
251
266
  def test_assert_tag_multiple
252
267
  process :test_html_output
253
268
 
@@ -267,7 +282,7 @@ HTML
267
282
 
268
283
  def test_assert_tag_children_without_content
269
284
  process :test_html_output
270
-
285
+
271
286
  # there is a form tag with an 'input' child which is a self closing tag
272
287
  assert_tag :tag => "form",
273
288
  :children => { :count => 1,
@@ -281,8 +296,29 @@ HTML
281
296
  :only => { :tag => "img" } } } }
282
297
  end
283
298
 
299
+ def test_assert_tag_attribute_matching
300
+ @response.body = '<input type="text" name="my_name">'
301
+ assert_tag :tag => 'input',
302
+ :attributes => { :name => /my/, :type => 'text' }
303
+ assert_no_tag :tag => 'input',
304
+ :attributes => { :name => 'my', :type => 'text' }
305
+ assert_no_tag :tag => 'input',
306
+ :attributes => { :name => /^my$/, :type => 'text' }
307
+ end
308
+
309
+ def test_assert_tag_content_matching
310
+ @response.body = "<p>hello world</p>"
311
+ assert_tag :tag => "p", :content => "hello world"
312
+ assert_tag :tag => "p", :content => /hello/
313
+ assert_no_tag :tag => "p", :content => "hello"
314
+ end
315
+
284
316
  def test_assert_generates
285
317
  assert_generates 'controller/action/5', :controller => 'controller', :action => 'action', :id => '5'
318
+ assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action"}
319
+ assert_generates 'controller/action/5', {:controller => "controller", :action => "action", :id => "5", :name => "bob"}, {}, {:name => "bob"}
320
+ assert_generates 'controller/action/7', {:id => "7", :name => "bob"}, {:controller => "controller", :action => "action"}, {:name => "bob"}
321
+ assert_generates 'controller/action/7', {:id => "7"}, {:controller => "controller", :action => "action", :name => "bob"}, {}
286
322
  end
287
323
 
288
324
  def test_assert_routing
@@ -310,11 +346,11 @@ HTML
310
346
 
311
347
  def test_array_path_parameter_handled_properly
312
348
  with_routing do |set|
313
- set.draw do
314
- set.connect 'file/*path', :controller => 'test_test/test', :action => 'test_params'
315
- set.connect ':controller/:action/:id'
349
+ set.draw do |map|
350
+ map.connect 'file/*path', :controller => 'test_test/test', :action => 'test_params'
351
+ map.connect ':controller/:action/:id'
316
352
  end
317
-
353
+
318
354
  get :test_params, :path => ['hello', 'world']
319
355
  assert_equal ['hello', 'world'], @request.path_parameters['path']
320
356
  assert_equal 'hello/world', @request.path_parameters['path'].to_s
@@ -333,13 +369,13 @@ HTML
333
369
  def test_with_routing_places_routes_back
334
370
  assert ActionController::Routing::Routes
335
371
  routes_id = ActionController::Routing::Routes.object_id
336
-
372
+
337
373
  begin
338
374
  with_routing { raise 'fail' }
339
375
  fail 'Should not be here.'
340
376
  rescue RuntimeError
341
377
  end
342
-
378
+
343
379
  assert ActionController::Routing::Routes
344
380
  assert_equal routes_id, ActionController::Routing::Routes.object_id
345
381
  end
@@ -380,32 +416,80 @@ HTML
380
416
  end
381
417
  end
382
418
  end
383
-
419
+
384
420
  FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
385
-
421
+
386
422
  def test_test_uploaded_file
387
423
  filename = 'mona_lisa.jpg'
388
424
  path = "#{FILES_DIR}/#{filename}"
389
425
  content_type = 'image/png'
390
-
426
+
391
427
  file = ActionController::TestUploadedFile.new(path, content_type)
392
428
  assert_equal filename, file.original_filename
393
429
  assert_equal content_type, file.content_type
394
430
  assert_equal file.path, file.local_path
395
431
  assert_equal File.read(path), file.read
396
432
  end
397
-
433
+
398
434
  def test_fixture_file_upload
399
435
  post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")
400
436
  assert_equal 159528, @response.body
401
437
  end
402
-
438
+
403
439
  def test_test_uploaded_file_exception_when_file_doesnt_exist
404
440
  assert_raise(RuntimeError) { ActionController::TestUploadedFile.new('non_existent_file') }
405
441
  end
406
442
 
407
443
  def test_assert_redirected_to_symbol
408
- get :redirect_to_symbol
409
- assert_redirected_to :generate_url
444
+ with_foo_routing do |set|
445
+ assert_deprecated(/generate_url.*redirect_to/) do
446
+ get :redirect_to_symbol
447
+ end
448
+ assert_response :redirect
449
+ assert_redirected_to :generate_url
450
+ end
451
+ end
452
+
453
+ def test_assert_follow_redirect_to_same_controller
454
+ with_foo_routing do |set|
455
+ get :redirect_to_same_controller
456
+ assert_response :redirect
457
+ assert_redirected_to :controller => 'test_test/test', :action => 'test_uri', :id => 5
458
+ assert_nothing_raised { follow_redirect }
459
+ end
460
+ end
461
+
462
+ def test_assert_follow_redirect_to_different_controller
463
+ with_foo_routing do |set|
464
+ get :redirect_to_different_controller
465
+ assert_response :redirect
466
+ assert_redirected_to :controller => 'fail', :id => 5
467
+ assert_raise(RuntimeError) { follow_redirect }
468
+ end
410
469
  end
470
+
471
+ def test_redirect_url_only_cares_about_location_header
472
+ get :create
473
+ assert_response :created
474
+
475
+ # Redirect url doesn't care that it wasn't a :redirect response.
476
+ assert_equal 'created resource', @response.redirect_url
477
+ assert_equal @response.redirect_url, redirect_to_url
478
+
479
+ # Must be a :redirect response.
480
+ assert_raise(Test::Unit::AssertionFailedError) do
481
+ assert_redirected_to 'created resource'
482
+ end
483
+ end
484
+
485
+ protected
486
+ def with_foo_routing
487
+ with_routing do |set|
488
+ set.draw do |map|
489
+ map.generate_url 'foo', :controller => 'test'
490
+ map.connect ':controller/:action/:id'
491
+ end
492
+ yield set
493
+ end
494
+ end
411
495
  end
@@ -6,23 +6,6 @@ class UrlRewriterTests < Test::Unit::TestCase
6
6
  @params = {}
7
7
  @rewriter = ActionController::UrlRewriter.new(@request, @params)
8
8
  end
9
-
10
- def test_simple_build_query_string
11
- assert_query_equal '?x=1&y=2', @rewriter.send(:build_query_string, :x => '1', :y => '2')
12
- end
13
- def test_convert_ints_build_query_string
14
- assert_query_equal '?x=1&y=2', @rewriter.send(:build_query_string, :x => 1, :y => 2)
15
- end
16
- def test_escape_spaces_build_query_string
17
- assert_query_equal '?x=hello+world&y=goodbye+world', @rewriter.send(:build_query_string, :x => 'hello world', :y => 'goodbye world')
18
- end
19
- def test_expand_array_build_query_string
20
- assert_query_equal '?x[]=1&x[]=2', @rewriter.send(:build_query_string, :x => [1, 2])
21
- end
22
-
23
- def test_escape_spaces_build_query_string_selected_keys
24
- assert_query_equal '?x=hello+world', @rewriter.send(:build_query_string, {:x => 'hello world', :y => 'goodbye world'}, [:x])
25
- end
26
9
 
27
10
  def test_overwrite_params
28
11
  @params[:controller] = 'hi'
@@ -44,3 +27,89 @@ class UrlRewriterTests < Test::Unit::TestCase
44
27
  assert_equal(split_query_string(q1), split_query_string(q2))
45
28
  end
46
29
  end
30
+
31
+ class UrlWriterTests < Test::Unit::TestCase
32
+
33
+ class W
34
+ include ActionController::UrlWriter
35
+ end
36
+
37
+ def teardown
38
+ W.default_url_options.clear
39
+ end
40
+
41
+ def add_host!
42
+ W.default_url_options[:host] = 'www.basecamphq.com'
43
+ end
44
+
45
+ def test_exception_is_thrown_without_host
46
+ assert_raises RuntimeError do
47
+ W.new.url_for :controller => 'c', :action => 'a', :id => 'i'
48
+ end
49
+ end
50
+
51
+ def test_default_host
52
+ add_host!
53
+ assert_equal('http://www.basecamphq.com/c/a/i',
54
+ W.new.url_for(:controller => 'c', :action => 'a', :id => 'i')
55
+ )
56
+ end
57
+
58
+ def test_host_may_be_overridden
59
+ add_host!
60
+ assert_equal('http://37signals.basecamphq.com/c/a/i',
61
+ W.new.url_for(:host => '37signals.basecamphq.com', :controller => 'c', :action => 'a', :id => 'i')
62
+ )
63
+ end
64
+
65
+ def test_port
66
+ add_host!
67
+ assert_equal('http://www.basecamphq.com:3000/c/a/i',
68
+ W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :port => 3000)
69
+ )
70
+ end
71
+
72
+ def test_protocol
73
+ add_host!
74
+ assert_equal('https://www.basecamphq.com/c/a/i',
75
+ W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
76
+ )
77
+ end
78
+
79
+ def test_named_route
80
+ ActionController::Routing::Routes.draw do |map|
81
+ map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'
82
+ map.connect ':controller/:action/:id'
83
+ end
84
+
85
+ # We need to create a new class in order to install the new named route.
86
+ kls = Class.new { include ActionController::UrlWriter }
87
+ controller = kls.new
88
+ assert controller.respond_to?(:home_url)
89
+ assert_equal 'http://www.basecamphq.com/home/sweet/home/again',
90
+ controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
91
+
92
+ assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => 'alabama', :host => 'unused'))
93
+ ensure
94
+ ActionController::Routing::Routes.load!
95
+ end
96
+
97
+ def test_only_path
98
+ ActionController::Routing::Routes.draw do |map|
99
+ map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'
100
+ map.connect ':controller/:action/:id'
101
+ end
102
+
103
+ # We need to create a new class in order to install the new named route.
104
+ kls = Class.new { include ActionController::UrlWriter }
105
+ controller = kls.new
106
+ assert controller.respond_to?(:home_url)
107
+ assert_equal '/brave/new/world',
108
+ controller.send(:url_for, :controller => 'brave', :action => 'new', :id => 'world', :only_path => true)
109
+
110
+ assert_equal("/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'unused', :only_path => true))
111
+ ensure
112
+ ActionController::Routing::Routes.load!
113
+ end
114
+
115
+ end