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
@@ -35,6 +35,16 @@ class FlashTest < Test::Unit::TestCase
35
35
  render :inline => "hello"
36
36
  end
37
37
 
38
+ def use_flash_after_reset_session
39
+ flash["that"] = "hello"
40
+ @flashy_that = flash["that"]
41
+ reset_session
42
+ @flashy_that_reset = flash["that"]
43
+ flash["this"] = "good-bye"
44
+ @flashy_this = flash["this"]
45
+ render :inline => "hello"
46
+ end
47
+
38
48
  def rescue_action(e)
39
49
  raise unless ActionController::MissingTemplate === e
40
50
  end
@@ -60,7 +70,7 @@ class FlashTest < Test::Unit::TestCase
60
70
  def test_keep_flash
61
71
  get :set_flash
62
72
 
63
- get :use_flash_and_keep_it
73
+ assert_deprecated(/keep_flash/) { get :use_flash_and_keep_it }
64
74
  assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
65
75
  assert_equal "hello", @response.template.assigns["flashy"]
66
76
 
@@ -82,4 +92,11 @@ class FlashTest < Test::Unit::TestCase
82
92
  assert_nil @response.template.assigns["flash_copy"]["foo"]
83
93
  assert_nil @response.template.assigns["flashy"]
84
94
  end
85
- end
95
+
96
+ def test_flash_after_reset_session
97
+ get :use_flash_after_reset_session
98
+ assert_equal "hello", @response.template.assigns["flashy_that"]
99
+ assert_equal "good-bye", @response.template.assigns["flashy_this"]
100
+ assert_nil @response.template.assigns["flashy_that_reset"]
101
+ end
102
+ end
@@ -77,8 +77,8 @@ class HelperTest < Test::Unit::TestCase
77
77
 
78
78
  def test_declare_missing_file_from_helper
79
79
  require 'broken_helper'
80
- rescue LoadError => e
81
- assert_nil /\bbroken_helper\b/.match(e.to_s)[1]
80
+ rescue LoadError => e
81
+ assert_nil(/\bbroken_helper\b/.match(e.to_s)[1])
82
82
  end
83
83
 
84
84
  def test_helper_block
@@ -0,0 +1,154 @@
1
+ require File.dirname(__FILE__) + '/../abstract_unit'
2
+
3
+ $:.unshift File.dirname(__FILE__) + '/../../../railties/lib'
4
+ require 'action_controller/integration'
5
+
6
+ begin # rescue LoadError
7
+ require 'mocha'
8
+ require 'stubba'
9
+
10
+ # Stub process for testing.
11
+ module ActionController
12
+ module Integration
13
+ class Session
14
+ def process
15
+ end
16
+
17
+ def generic_url_rewriter
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class SessionTest < Test::Unit::TestCase
24
+ def setup
25
+ @session = ActionController::Integration::Session.new
26
+ end
27
+ def test_https_bang_works_and_sets_truth_by_default
28
+ assert !@session.https?
29
+ @session.https!
30
+ assert @session.https?
31
+ @session.https! false
32
+ assert !@session.https?
33
+ end
34
+
35
+ def test_host!
36
+ assert_not_equal "glu.ttono.us", @session.host
37
+ @session.host! "rubyonrails.com"
38
+ assert_equal "rubyonrails.com", @session.host
39
+ end
40
+
41
+ def test_follow_redirect_raises_when_no_redirect
42
+ @session.stubs(:redirect?).returns(false)
43
+ assert_raise(RuntimeError) { @session.follow_redirect! }
44
+ end
45
+
46
+ def test_follow_redirect_calls_get_and_returns_status
47
+ @session.stubs(:redirect?).returns(true)
48
+ @session.stubs(:headers).returns({"location" => ["www.google.com"]})
49
+ @session.stubs(:status).returns(200)
50
+ @session.expects(:get)
51
+ assert_equal 200, @session.follow_redirect!
52
+ end
53
+
54
+ def test_get_via_redirect
55
+ path = "/somepath"; args = {:id => '1'}
56
+
57
+ @session.expects(:get).with(path,args)
58
+
59
+ redirects = [true, true, false]
60
+ @session.stubs(:redirect?).returns(lambda { redirects.shift })
61
+ @session.expects(:follow_redirect!).times(2)
62
+
63
+ @session.stubs(:status).returns(200)
64
+ assert_equal 200, @session.get_via_redirect(path, args)
65
+ end
66
+
67
+ def test_post_via_redirect
68
+ path = "/somepath"; args = {:id => '1'}
69
+
70
+ @session.expects(:post).with(path,args)
71
+
72
+ redirects = [true, true, false]
73
+ @session.stubs(:redirect?).returns(lambda { redirects.shift })
74
+ @session.expects(:follow_redirect!).times(2)
75
+
76
+ @session.stubs(:status).returns(200)
77
+ assert_equal 200, @session.post_via_redirect(path, args)
78
+ end
79
+
80
+ def test_url_for_with_controller
81
+ options = {:action => 'show'}
82
+ mock_controller = mock()
83
+ mock_controller.expects(:url_for).with(options).returns('/show')
84
+ @session.stubs(:controller).returns(mock_controller)
85
+ assert_equal '/show', @session.url_for(options)
86
+ end
87
+
88
+ def test_url_for_without_controller
89
+ options = {:action => 'show'}
90
+ mock_rewriter = mock()
91
+ mock_rewriter.expects(:rewrite).with(options).returns('/show')
92
+ @session.stubs(:generic_url_rewriter).returns(mock_rewriter)
93
+ @session.stubs(:controller).returns(nil)
94
+ assert_equal '/show', @session.url_for(options)
95
+ end
96
+
97
+ def test_redirect_bool_with_status_in_300s
98
+ @session.stubs(:status).returns 301
99
+ assert @session.redirect?
100
+ end
101
+
102
+ def test_redirect_bool_with_status_in_200s
103
+ @session.stubs(:status).returns 200
104
+ assert !@session.redirect?
105
+ end
106
+
107
+ def test_get
108
+ path = "/index"; params = "blah"; headers = {:location => 'blah'}
109
+ @session.expects(:process).with(:get,path,params,headers)
110
+ @session.get(path,params,headers)
111
+ end
112
+
113
+ def test_post
114
+ path = "/index"; params = "blah"; headers = {:location => 'blah'}
115
+ @session.expects(:process).with(:post,path,params,headers)
116
+ @session.post(path,params,headers)
117
+ end
118
+
119
+ def test_put
120
+ path = "/index"; params = "blah"; headers = {:location => 'blah'}
121
+ @session.expects(:process).with(:put,path,params,headers)
122
+ @session.put(path,params,headers)
123
+ end
124
+
125
+ def test_delete
126
+ path = "/index"; params = "blah"; headers = {:location => 'blah'}
127
+ @session.expects(:process).with(:delete,path,params,headers)
128
+ @session.delete(path,params,headers)
129
+ end
130
+
131
+ def test_head
132
+ path = "/index"; params = "blah"; headers = {:location => 'blah'}
133
+ @session.expects(:process).with(:head,path,params,headers)
134
+ @session.head(path,params,headers)
135
+ end
136
+
137
+ def test_xml_http_request
138
+ path = "/index"; params = "blah"; headers = {:location => 'blah'}
139
+ headers_after_xhr = headers.merge(
140
+ "X-Requested-With" => "XMLHttpRequest",
141
+ "Accept" => "text/javascript, text/html, application/xml, text/xml, */*"
142
+ )
143
+ @session.expects(:post).with(path,params,headers_after_xhr)
144
+ @session.xml_http_request(path,params,headers)
145
+ end
146
+ end
147
+
148
+ # TODO
149
+ # class MockCGITest < Test::Unit::TestCase
150
+ # end
151
+
152
+ rescue LoadError
153
+ $stderr.puts "Skipping integration tests. `gem install mocha` and try again."
154
+ end
@@ -70,4 +70,118 @@ class LayoutAutoDiscoveryTest < Test::Unit::TestCase
70
70
  assert_equal 'layouts/controller_name_space/nested', @controller.active_layout
71
71
  assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body
72
72
  end
73
- end
73
+ end
74
+
75
+ class ExemptFromLayoutTest < Test::Unit::TestCase
76
+ def setup
77
+ @controller = LayoutTest.new
78
+ @request = ActionController::TestRequest.new
79
+ @response = ActionController::TestResponse.new
80
+ end
81
+
82
+ def test_rjs_exempt_from_layout
83
+ assert @controller.send(:template_exempt_from_layout?, 'test.rjs')
84
+ end
85
+
86
+ def test_rhtml_and_rxml_not_exempt_from_layout
87
+ assert !@controller.send(:template_exempt_from_layout?, 'test.rhtml')
88
+ assert !@controller.send(:template_exempt_from_layout?, 'test.rxml')
89
+ end
90
+
91
+ def test_other_extension_not_exempt_from_layout
92
+ assert !@controller.send(:template_exempt_from_layout?, 'test.random')
93
+ end
94
+
95
+ def test_add_extension_to_exempt_from_layout
96
+ ['rpdf', :rpdf].each do |ext|
97
+ assert_nothing_raised do
98
+ ActionController::Base.exempt_from_layout ext
99
+ end
100
+ assert @controller.send(:template_exempt_from_layout?, "test.#{ext}")
101
+ end
102
+ end
103
+
104
+ def test_add_regexp_to_exempt_from_layout
105
+ ActionController::Base.exempt_from_layout /\.rdoc/
106
+ assert @controller.send(:template_exempt_from_layout?, 'test.rdoc')
107
+ end
108
+
109
+ def test_rhtml_exempt_from_layout_status_should_prevent_layout_render
110
+ ActionController::Base.exempt_from_layout :rhtml
111
+ assert @controller.send(:template_exempt_from_layout?, 'test.rhtml')
112
+
113
+ get :hello
114
+ assert_equal 'hello.rhtml', @response.body
115
+ ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
116
+ end
117
+ end
118
+
119
+
120
+ class DefaultLayoutController < LayoutTest
121
+ end
122
+
123
+ class HasOwnLayoutController < LayoutTest
124
+ layout 'item'
125
+ end
126
+
127
+ class SetsLayoutInRenderController < LayoutTest
128
+ def hello
129
+ render :layout => 'third_party_template_library'
130
+ end
131
+ end
132
+
133
+ class RendersNoLayoutController < LayoutTest
134
+ def hello
135
+ render :layout => false
136
+ end
137
+ end
138
+
139
+ class LayoutSetInResponseTest < Test::Unit::TestCase
140
+ def setup
141
+ @request = ActionController::TestRequest.new
142
+ @response = ActionController::TestResponse.new
143
+ end
144
+
145
+ def test_layout_set_when_using_default_layout
146
+ @controller = DefaultLayoutController.new
147
+ get :hello
148
+ assert_equal 'layouts/layout_test', @response.layout
149
+ end
150
+
151
+ def test_layout_set_when_set_in_controller
152
+ @controller = HasOwnLayoutController.new
153
+ get :hello
154
+ assert_equal 'layouts/item', @response.layout
155
+ end
156
+
157
+ def test_layout_set_when_using_render
158
+ @controller = SetsLayoutInRenderController.new
159
+ get :hello
160
+ assert_equal 'layouts/third_party_template_library', @response.layout
161
+ end
162
+
163
+ def test_layout_is_not_set_when_none_rendered
164
+ @controller = RendersNoLayoutController.new
165
+ get :hello
166
+ assert_nil @response.layout
167
+ end
168
+ end
169
+
170
+
171
+ class SetsNonExistentLayoutFile < LayoutTest
172
+ layout "nofile.rhtml"
173
+ end
174
+
175
+ class LayoutExceptionRaised < Test::Unit::TestCase
176
+ def setup
177
+ @request = ActionController::TestRequest.new
178
+ @response = ActionController::TestResponse.new
179
+ end
180
+
181
+ def test_exception_raised_when_layout_file_not_found
182
+ @controller = SetsNonExistentLayoutFile.new
183
+ get :hello
184
+ @response.template.class.module_eval { attr_accessor :exception }
185
+ assert_equal ActionController::MissingTemplate, @response.template.exception.class
186
+ end
187
+ end
@@ -20,6 +20,13 @@ class RespondToController < ActionController::Base
20
20
  end
21
21
  end
22
22
 
23
+ def json_or_yaml
24
+ respond_to do |type|
25
+ type.json { render :text => "JSON" }
26
+ type.yaml { render :text => "YAML" }
27
+ end
28
+ end
29
+
23
30
  def html_or_xml
24
31
  respond_to do |type|
25
32
  type.html { render :text => "HTML" }
@@ -61,6 +68,29 @@ class RespondToController < ActionController::Base
61
68
  type.all { render :text => "Nothing" }
62
69
  end
63
70
  end
71
+
72
+ def custom_constant_handling
73
+ Mime::Type.register("text/x-mobile", :mobile)
74
+
75
+ respond_to do |type|
76
+ type.html { render :text => "HTML" }
77
+ type.mobile { render :text => "Mobile" }
78
+ end
79
+
80
+ Mime.send :remove_const, :MOBILE
81
+ end
82
+
83
+ def custom_constant_handling_without_block
84
+ Mime::Type.register("text/x-mobile", :mobile)
85
+
86
+ respond_to do |type|
87
+ type.html { render :text => "HTML" }
88
+ type.mobile
89
+ end
90
+
91
+ Mime.send :remove_const, :MOBILE
92
+ end
93
+
64
94
 
65
95
  def handle_any
66
96
  respond_to do |type|
@@ -141,6 +171,27 @@ class MimeControllerTest < Test::Unit::TestCase
141
171
  assert_response 406
142
172
  end
143
173
 
174
+ def test_json_or_yaml
175
+ get :json_or_yaml
176
+ assert_equal 'JSON', @response.body
177
+
178
+ get :json_or_yaml, :format => 'json'
179
+ assert_equal 'JSON', @response.body
180
+
181
+ get :json_or_yaml, :format => 'yaml'
182
+ assert_equal 'YAML', @response.body
183
+
184
+ { 'YAML' => %w(text/yaml),
185
+ 'JSON' => %w(application/json text/x-json)
186
+ }.each do |body, content_types|
187
+ content_types.each do |content_type|
188
+ @request.env['HTTP_ACCEPT'] = content_type
189
+ get :json_or_yaml
190
+ assert_equal body, @response.body
191
+ end
192
+ end
193
+ end
194
+
144
195
  def test_js_or_anything
145
196
  @request.env["HTTP_ACCEPT"] = "text/javascript, */*"
146
197
  get :js_or_html
@@ -254,4 +305,47 @@ class MimeControllerTest < Test::Unit::TestCase
254
305
  xhr :get, :using_defaults
255
306
  assert_equal '$("body").visualEffect("highlight");', @response.body
256
307
  end
308
+
309
+ def test_custom_constant
310
+ get :custom_constant_handling, :format => "mobile"
311
+ assert_equal "Mobile", @response.body
312
+ end
313
+
314
+ def custom_constant_handling_without_block
315
+
316
+ assert_raised(ActionController::RenderError) do
317
+ get :custom_constant_handling, :format => "mobile"
318
+ end
319
+ end
320
+
321
+ def test_forced_format
322
+ get :html_xml_or_rss
323
+ assert_equal "HTML", @response.body
324
+
325
+ get :html_xml_or_rss, :format => "html"
326
+ assert_equal "HTML", @response.body
327
+
328
+ get :html_xml_or_rss, :format => "xml"
329
+ assert_equal "XML", @response.body
330
+
331
+ get :html_xml_or_rss, :format => "rss"
332
+ assert_equal "RSS", @response.body
333
+ end
334
+
335
+ def test_render_action_for_html
336
+ @controller.instance_eval do
337
+ def render(*args)
338
+ unless args.empty?
339
+ @action = args.first[:action]
340
+ end
341
+ response.body = @action
342
+ end
343
+ end
344
+
345
+ get :using_defaults
346
+ assert_equal "using_defaults", @response.body
347
+
348
+ get :using_defaults, :format => "xml"
349
+ assert_equal "using_defaults.rxml", @response.body
350
+ end
257
351
  end
@@ -21,4 +21,13 @@ class MimeTypeTest < Test::Unit::TestCase
21
21
  expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PLAIN, Mime::YAML, Mime::ALL]
22
22
  assert_equal expect, Mime::Type.parse(accept)
23
23
  end
24
+
25
+ def test_custom_type
26
+ Mime::Type.register("image/gif", :gif)
27
+ assert_nothing_raised do
28
+ Mime::GIF
29
+ assert_equal Mime::GIF, Mime::SET.last
30
+ end
31
+ Mime.send :remove_const, :GIF
32
+ end
24
33
  end