actionpack 3.2.13 → 3.2.14.rc1

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.

@@ -1,9 +1,97 @@
1
- ## unreleased ##
1
+ ## Rails 3.2.14.rc1 (Jul 8, 2013) ##
2
2
 
3
- * No changes.
3
+ * Merge `:action` from routing scope and assign endpoint if both `:controller`
4
+ and `:action` are present. The endpoint assignment only occurs if there is
5
+ no `:to` present in the options hash so should only affect routes using the
6
+ shorthand syntax (i.e. endpoint is inferred from the the path).
7
+
8
+ Fixes #9856
9
+
10
+ *Yves Senn*, *Andrew White*
11
+
12
+ * Always escape the result of `link_to_unless` method.
13
+
14
+ Before:
15
+
16
+ link_to_unless(true, '<b>Showing</b>', 'github.com')
17
+ # => "<b>Showing</b>"
18
+
19
+ After:
20
+
21
+ link_to_unless(true, '<b>Showing</b>', 'github.com')
22
+ # => "&lt;b&gt;Showing&lt;/b&gt;"
23
+
24
+ *dtaniwaki*
25
+
26
+ * Use a case insensitive URI Regexp for #asset_path.
27
+
28
+ This fix a problem where the same asset path using different case are generating
29
+ different URIs.
30
+
31
+ Before:
32
+
33
+ image_tag("HTTP://google.com")
34
+ # => "<img alt=\"Google\" src=\"/assets/HTTP://google.com\" />"
35
+ image_tag("http://google.com")
36
+ # => "<img alt=\"Google\" src=\"http://google.com\" />"
37
+
38
+ After:
39
+
40
+ image_tag("HTTP://google.com")
41
+ # => "<img alt=\"Google\" src=\"HTTP://google.com\" />"
42
+ image_tag("http://google.com")
43
+ # => "<img alt=\"Google\" src=\"http://google.com\" />"
44
+
45
+ *David Celis + Rafael Mendonça França*
46
+
47
+ * Fix explicit names on multiple file fields. If a file field tag has
48
+ the multiple option, it is turned into an array field (appending `[]`),
49
+ but if an explicit name is passed to `file_field` the `[]` is not
50
+ appended.
51
+ Fixes #9830.
52
+
53
+ *Ryan McGeary*
54
+
55
+ * Fix assets loading performance in 3.2.13.
56
+
57
+ Issue #8756 uses Sprockets for resolving files that already exist on disk,
58
+ for those files their extensions don't need to be rewritten.
59
+
60
+ Fixes #9803.
61
+
62
+ *Fred Wu*
63
+
64
+ * Fix `ActionController#action_missing` not being called.
65
+ Fixes #9799.
66
+
67
+ *Janko Luin*
68
+
69
+ * `ActionView::Helpers::NumberHelper#number_to_human` returns the number unaltered when
70
+ the units hash does not contain the needed key, e.g. when the number provided is less
71
+ than the largest key provided.
72
+
73
+ Examples:
74
+
75
+ number_to_human(123, units: {}) # => 123
76
+ number_to_human(123, units: { thousand: 'k' }) # => 123
77
+
78
+ Fixes #9269.
79
+ Backport #9347.
80
+
81
+ *Michael Hoffman*
82
+
83
+ * Include I18n locale fallbacks in view lookup.
84
+ Fixes GH#3512.
85
+
86
+ *Juan Barreneche*
87
+
88
+ * Fix `ActionDispatch::Request#formats` when the Accept request-header is an
89
+ empty string. Fix #7774 [Backport #8977, #9541]
90
+
91
+ *Soylent + Maxime Réty*
4
92
 
5
93
 
6
- ## Rails 3.2.13 (Feb 17, 2013) ##
94
+ ## Rails 3.2.13 (Mar 18, 2013) ##
7
95
 
8
96
  * Fix incorrectly appended square brackets to a multiple select box
9
97
  if an explicit name has been given and it already ends with "[]".
@@ -43,7 +131,7 @@
43
131
 
44
132
  *Sergey Nartimov*
45
133
 
46
- * Eagerly populate the http method loookup cache so local project inflections do
134
+ * Eagerly populate the http method lookup cache so local project inflections do
47
135
  not interfere with use of underscore method ( and we don't need locks )
48
136
 
49
137
  *Aditya Sanghi*
@@ -28,7 +28,7 @@ module ActionController
28
28
  end
29
29
 
30
30
  def visible_action?(action_name)
31
- action_methods.include?(action_name)
31
+ not hidden_actions.include?(action_name)
32
32
  end
33
33
 
34
34
  # Overrides AbstractController::Base#action_methods to remove any methods
@@ -20,7 +20,12 @@ module ActionController
20
20
 
21
21
  ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload|
22
22
  path = payload[:layout]
23
- @layouts[path] += 1
23
+ if path
24
+ @layouts[path] += 1
25
+ if path =~ /^layouts\/(.*)/
26
+ @layouts[$1] += 1
27
+ end
28
+ end
24
29
  end
25
30
 
26
31
  ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload|
@@ -56,6 +61,15 @@ module ActionController
56
61
  # # assert that the "new" view template was rendered
57
62
  # assert_template "new"
58
63
  #
64
+ # # assert that the layout 'admin' was rendered
65
+ # assert_template :layout => 'admin'
66
+ # assert_template :layout => 'layouts/admin'
67
+ # assert_template :layout => :admin
68
+ #
69
+ # # assert that no layout was rendered
70
+ # assert_template :layout => nil
71
+ # assert_template :layout => false
72
+ #
59
73
  # # assert that the "_customer" partial was rendered twice
60
74
  # assert_template :partial => '_customer', :count => 2
61
75
  #
@@ -88,17 +102,18 @@ module ActionController
88
102
  end
89
103
  end
90
104
  when Hash
91
- if expected_layout = options[:layout]
105
+ if options.key?(:layout)
106
+ expected_layout = options[:layout]
92
107
  msg = build_message(message,
93
108
  "expecting layout <?> but action rendered <?>",
94
109
  expected_layout, @layouts.keys)
95
110
 
96
111
  case expected_layout
97
- when String
98
- assert(@layouts.keys.include?(expected_layout), msg)
112
+ when String, Symbol
113
+ assert(@layouts.keys.include?(expected_layout.to_s), msg)
99
114
  when Regexp
100
115
  assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg)
101
- when nil
116
+ when nil, false
102
117
  assert(@layouts.empty?, msg)
103
118
  end
104
119
  end
@@ -125,7 +140,7 @@ module ActionController
125
140
  options[:partial], @partials.keys)
126
141
  assert(@partials.include?(expected_partial), msg)
127
142
  end
128
- else
143
+ elsif options.key?(:partial)
129
144
  assert @partials.empty?,
130
145
  "Expected no partials to be rendered"
131
146
  end
@@ -460,7 +475,7 @@ module ActionController
460
475
  parameters ||= {}
461
476
  controller_class_name = @controller.class.anonymous? ?
462
477
  "anonymous_controller" :
463
- @controller.class.name.underscore.sub(/_controller$/, '')
478
+ @controller.class.controller_path
464
479
 
465
480
  @request.assign_parameters(@routes, controller_class_name, action.to_s, parameters)
466
481
 
@@ -0,0 +1,707 @@
1
+ require 'rack/session/abstract/id'
2
+ require 'active_support/core_ext/object/to_query'
3
+ require 'active_support/core_ext/module/anonymous'
4
+ require 'active_support/core_ext/hash/keys'
5
+
6
+ module ActionController
7
+ module TemplateAssertions
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ setup :setup_subscriptions
12
+ teardown :teardown_subscriptions
13
+ end
14
+
15
+ def setup_subscriptions
16
+ @_partials = Hash.new(0)
17
+ @_templates = Hash.new(0)
18
+ @_layouts = Hash.new(0)
19
+ @_files = Hash.new(0)
20
+
21
+ ActiveSupport::Notifications.subscribe("render_template.action_view") do |_name, _start, _finish, _id, payload|
22
+ path = payload[:layout]
23
+ if path
24
+ @_layouts[path] += 1
25
+ if path =~ /^layouts\/(.*)/
26
+ @_layouts[$1] += 1
27
+ end
28
+ end
29
+ end
30
+
31
+ ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload|
32
+ path = payload[:virtual_path]
33
+ next unless path
34
+ partial = path =~ /^.*\/_[^\/]*$/
35
+
36
+ if partial
37
+ @_partials[path] += 1
38
+ @_partials[path.split("/").last] += 1
39
+ end
40
+
41
+ @_templates[path] += 1
42
+ end
43
+
44
+ ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload|
45
+ next if payload[:virtual_path] # files don't have virtual path
46
+
47
+ path = payload[:identifier]
48
+ if path
49
+ @_files[path] += 1
50
+ @_files[path.split("/").last] += 1
51
+ end
52
+ end
53
+ end
54
+
55
+ def teardown_subscriptions
56
+ ActiveSupport::Notifications.unsubscribe("render_template.action_view")
57
+ ActiveSupport::Notifications.unsubscribe("!render_template.action_view")
58
+ end
59
+
60
+ def process(*args)
61
+ @_partials = Hash.new(0)
62
+ @_templates = Hash.new(0)
63
+ @_layouts = Hash.new(0)
64
+ super
65
+ end
66
+
67
+ # Asserts that the request was rendered with the appropriate template file or partials.
68
+ #
69
+ # # assert that the "new" view template was rendered
70
+ # assert_template "new"
71
+ #
72
+ # # assert that the exact template "admin/posts/new" was rendered
73
+ # assert_template %r{\Aadmin/posts/new\Z}
74
+ #
75
+ # # assert that the layout 'admin' was rendered
76
+ # assert_template layout: 'admin'
77
+ # assert_template layout: 'layouts/admin'
78
+ # assert_template layout: :admin
79
+ #
80
+ # # assert that no layout was rendered
81
+ # assert_template layout: nil
82
+ # assert_template layout: false
83
+ #
84
+ # # assert that the "_customer" partial was rendered twice
85
+ # assert_template partial: '_customer', count: 2
86
+ #
87
+ # # assert that no partials were rendered
88
+ # assert_template partial: false
89
+ #
90
+ # In a view test case, you can also assert that specific locals are passed
91
+ # to partials:
92
+ #
93
+ # # assert that the "_customer" partial was rendered with a specific object
94
+ # assert_template partial: '_customer', locals: { customer: @customer }
95
+ def assert_template(options = {}, message = nil)
96
+ # Force body to be read in case the template is being streamed.
97
+ response.body
98
+
99
+ case options
100
+ when NilClass, Regexp, String, Symbol
101
+ options = options.to_s if Symbol === options
102
+ rendered = @_templates
103
+ msg = message || sprintf("expecting <%s> but rendering with <%s>",
104
+ options.inspect, rendered.keys)
105
+ matches_template =
106
+ case options
107
+ when String
108
+ !options.empty? && rendered.any? do |t, num|
109
+ options_splited = options.split(File::SEPARATOR)
110
+ t_splited = t.split(File::SEPARATOR)
111
+ t_splited.last(options_splited.size) == options_splited
112
+ end
113
+ when Regexp
114
+ rendered.any? { |t,num| t.match(options) }
115
+ when NilClass
116
+ rendered.blank?
117
+ end
118
+ assert matches_template, msg
119
+ when Hash
120
+ options.assert_valid_keys(:layout, :partial, :locals, :count, :file)
121
+
122
+ if options.key?(:layout)
123
+ expected_layout = options[:layout]
124
+ msg = message || sprintf("expecting layout <%s> but action rendered <%s>",
125
+ expected_layout, @_layouts.keys)
126
+
127
+ case expected_layout
128
+ when String, Symbol
129
+ assert_includes @_layouts.keys, expected_layout.to_s, msg
130
+ when Regexp
131
+ assert(@_layouts.keys.any? {|l| l =~ expected_layout }, msg)
132
+ when nil, false
133
+ assert(@_layouts.empty?, msg)
134
+ end
135
+ end
136
+
137
+ if options[:file]
138
+ assert_includes @_files.keys, options[:file]
139
+ end
140
+
141
+ if expected_partial = options[:partial]
142
+ if expected_locals = options[:locals]
143
+ if defined?(@_rendered_views)
144
+ view = expected_partial.to_s.sub(/^_/, '').sub(/\/_(?=[^\/]+\z)/, '/')
145
+
146
+ partial_was_not_rendered_msg = "expected %s to be rendered but it was not." % view
147
+ assert_includes @_rendered_views.rendered_views, view, partial_was_not_rendered_msg
148
+
149
+ msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
150
+ expected_locals,
151
+ @_rendered_views.locals_for(view)]
152
+ assert(@_rendered_views.view_rendered?(view, options[:locals]), msg)
153
+ else
154
+ warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
155
+ end
156
+ elsif expected_count = options[:count]
157
+ actual_count = @_partials[expected_partial]
158
+ msg = message || sprintf("expecting %s to be rendered %s time(s) but rendered %s time(s)",
159
+ expected_partial, expected_count, actual_count)
160
+ assert(actual_count == expected_count.to_i, msg)
161
+ else
162
+ msg = message || sprintf("expecting partial <%s> but action rendered <%s>",
163
+ options[:partial], @_partials.keys)
164
+ assert_includes @_partials, expected_partial, msg
165
+ end
166
+ elsif options.key?(:partial)
167
+ assert @_partials.empty?,
168
+ "Expected no partials to be rendered"
169
+ end
170
+ else
171
+ raise ArgumentError, "assert_template only accepts a String, Symbol, Hash, Regexp, or nil"
172
+ end
173
+ end
174
+ end
175
+
176
+ class TestRequest < ActionDispatch::TestRequest #:nodoc:
177
+ DEFAULT_ENV = ActionDispatch::TestRequest::DEFAULT_ENV.dup
178
+ DEFAULT_ENV.delete 'PATH_INFO'
179
+
180
+ def initialize(env = {})
181
+ super
182
+
183
+ self.session = TestSession.new
184
+ self.session_options = TestSession::DEFAULT_OPTIONS.merge(:id => SecureRandom.hex(16))
185
+ end
186
+
187
+ def assign_parameters(routes, controller_path, action, parameters = {})
188
+ parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
189
+ extra_keys = routes.extra_keys(parameters)
190
+ non_path_parameters = get? ? query_parameters : request_parameters
191
+ parameters.each do |key, value|
192
+ if value.is_a?(Array) && (value.frozen? || value.any?(&:frozen?))
193
+ value = value.map{ |v| v.duplicable? ? v.dup : v }
194
+ elsif value.is_a?(Hash) && (value.frozen? || value.any?{ |k,v| v.frozen? })
195
+ value = Hash[value.map{ |k,v| [k, v.duplicable? ? v.dup : v] }]
196
+ elsif value.frozen? && value.duplicable?
197
+ value = value.dup
198
+ end
199
+
200
+ if extra_keys.include?(key.to_sym)
201
+ non_path_parameters[key] = value
202
+ else
203
+ if value.is_a?(Array)
204
+ value = value.map(&:to_param)
205
+ else
206
+ value = value.to_param
207
+ end
208
+
209
+ path_parameters[key.to_s] = value
210
+ end
211
+ end
212
+
213
+ # Clear the combined params hash in case it was already referenced.
214
+ @env.delete("action_dispatch.request.parameters")
215
+
216
+ params = self.request_parameters.dup
217
+ %w(controller action only_path).each do |k|
218
+ params.delete(k)
219
+ params.delete(k.to_sym)
220
+ end
221
+ data = params.to_query
222
+
223
+ @env['CONTENT_LENGTH'] = data.length.to_s
224
+ @env['rack.input'] = StringIO.new(data)
225
+ end
226
+
227
+ def recycle!
228
+ @formats = nil
229
+ @env.delete_if { |k, v| k =~ /^(action_dispatch|rack)\.request/ }
230
+ @env.delete_if { |k, v| k =~ /^action_dispatch\.rescue/ }
231
+ @symbolized_path_params = nil
232
+ @method = @request_method = nil
233
+ @fullpath = @ip = @remote_ip = @protocol = nil
234
+ @env['action_dispatch.request.query_parameters'] = {}
235
+ @set_cookies ||= {}
236
+ @set_cookies.update(Hash[cookie_jar.instance_variable_get("@set_cookies").map{ |k,o| [k,o[:value]] }])
237
+ deleted_cookies = cookie_jar.instance_variable_get("@delete_cookies")
238
+ @set_cookies.reject!{ |k,v| deleted_cookies.include?(k) }
239
+ cookie_jar.update(rack_cookies)
240
+ cookie_jar.update(cookies)
241
+ cookie_jar.update(@set_cookies)
242
+ cookie_jar.recycle!
243
+ end
244
+
245
+ private
246
+
247
+ def default_env
248
+ DEFAULT_ENV
249
+ end
250
+ end
251
+
252
+ class TestResponse < ActionDispatch::TestResponse
253
+ def recycle!
254
+ initialize
255
+ end
256
+ end
257
+
258
+ # Methods #destroy and #load! are overridden to avoid calling methods on the
259
+ # @store object, which does not exist for the TestSession class.
260
+ class TestSession < Rack::Session::Abstract::SessionHash #:nodoc:
261
+ DEFAULT_OPTIONS = Rack::Session::Abstract::ID::DEFAULT_OPTIONS
262
+
263
+ def initialize(session = {})
264
+ super(nil, nil)
265
+ @id = SecureRandom.hex(16)
266
+ @data = stringify_keys(session)
267
+ @loaded = true
268
+ end
269
+
270
+ def exists?
271
+ true
272
+ end
273
+
274
+ def keys
275
+ @data.keys
276
+ end
277
+
278
+ def values
279
+ @data.values
280
+ end
281
+
282
+ def destroy
283
+ clear
284
+ end
285
+
286
+ private
287
+
288
+ def load!
289
+ @id
290
+ end
291
+ end
292
+
293
+ # Superclass for ActionController functional tests. Functional tests allow you to
294
+ # test a single controller action per test method. This should not be confused with
295
+ # integration tests (see ActionDispatch::IntegrationTest), which are more like
296
+ # "stories" that can involve multiple controllers and multiple actions (i.e. multiple
297
+ # different HTTP requests).
298
+ #
299
+ # == Basic example
300
+ #
301
+ # Functional tests are written as follows:
302
+ # 1. First, one uses the +get+, +post+, +patch+, +put+, +delete+ or +head+ method to simulate
303
+ # an HTTP request.
304
+ # 2. Then, one asserts whether the current state is as expected. "State" can be anything:
305
+ # the controller's HTTP response, the database contents, etc.
306
+ #
307
+ # For example:
308
+ #
309
+ # class BooksControllerTest < ActionController::TestCase
310
+ # def test_create
311
+ # # Simulate a POST response with the given HTTP parameters.
312
+ # post(:create, book: { title: "Love Hina" })
313
+ #
314
+ # # Assert that the controller tried to redirect us to
315
+ # # the created book's URI.
316
+ # assert_response :found
317
+ #
318
+ # # Assert that the controller really put the book in the database.
319
+ # assert_not_nil Book.find_by(title: "Love Hina")
320
+ # end
321
+ # end
322
+ #
323
+ # You can also send a real document in the simulated HTTP request.
324
+ #
325
+ # def test_create
326
+ # json = {book: { title: "Love Hina" }}.to_json
327
+ # post :create, json
328
+ # end
329
+ #
330
+ # == Special instance variables
331
+ #
332
+ # ActionController::TestCase will also automatically provide the following instance
333
+ # variables for use in the tests:
334
+ #
335
+ # <b>@controller</b>::
336
+ # The controller instance that will be tested.
337
+ # <b>@request</b>::
338
+ # An ActionController::TestRequest, representing the current HTTP
339
+ # request. You can modify this object before sending the HTTP request. For example,
340
+ # you might want to set some session properties before sending a GET request.
341
+ # <b>@response</b>::
342
+ # An ActionController::TestResponse object, representing the response
343
+ # of the last HTTP response. In the above example, <tt>@response</tt> becomes valid
344
+ # after calling +post+. If the various assert methods are not sufficient, then you
345
+ # may use this object to inspect the HTTP response in detail.
346
+ #
347
+ # (Earlier versions of \Rails required each functional test to subclass
348
+ # Test::Unit::TestCase and define @controller, @request, @response in +setup+.)
349
+ #
350
+ # == Controller is automatically inferred
351
+ #
352
+ # ActionController::TestCase will automatically infer the controller under test
353
+ # from the test class name. If the controller cannot be inferred from the test
354
+ # class name, you can explicitly set it with +tests+.
355
+ #
356
+ # class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
357
+ # tests WidgetController
358
+ # end
359
+ #
360
+ # == \Testing controller internals
361
+ #
362
+ # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions
363
+ # can be used against. These collections are:
364
+ #
365
+ # * assigns: Instance variables assigned in the action that are available for the view.
366
+ # * session: Objects being saved in the session.
367
+ # * flash: The flash objects currently in the session.
368
+ # * cookies: \Cookies being sent to the user on this request.
369
+ #
370
+ # These collections can be used just like any other hash:
371
+ #
372
+ # assert_not_nil assigns(:person) # makes sure that a @person instance variable was set
373
+ # assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
374
+ # assert flash.empty? # makes sure that there's nothing in the flash
375
+ #
376
+ # For historic reasons, the assigns hash uses string-based keys. So <tt>assigns[:person]</tt> won't work, but <tt>assigns["person"]</tt> will. To
377
+ # appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing.
378
+ # So <tt>assigns(:person)</tt> will work just like <tt>assigns["person"]</tt>, but again, <tt>assigns[:person]</tt> will not work.
379
+ #
380
+ # On top of the collections, you have the complete url that a given action redirected to available in <tt>redirect_to_url</tt>.
381
+ #
382
+ # For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another
383
+ # action call which can then be asserted against.
384
+ #
385
+ # == Manipulating session and cookie variables
386
+ #
387
+ # Sometimes you need to set up the session and cookie variables for a test.
388
+ # To do this just assign a value to the session or cookie collection:
389
+ #
390
+ # session[:key] = "value"
391
+ # cookies[:key] = "value"
392
+ #
393
+ # To clear the cookies for a test just clear the cookie collection:
394
+ #
395
+ # cookies.clear
396
+ #
397
+ # == \Testing named routes
398
+ #
399
+ # If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case.
400
+ #
401
+ # assert_redirected_to page_url(title: 'foo')
402
+ class TestCase < ActiveSupport::TestCase
403
+ module Behavior
404
+ extend ActiveSupport::Concern
405
+ include ActionDispatch::TestProcess
406
+ include ActiveSupport::Testing::ConstantLookup
407
+
408
+ attr_reader :response, :request
409
+
410
+ module ClassMethods
411
+
412
+ # Sets the controller class name. Useful if the name can't be inferred from test class.
413
+ # Normalizes +controller_class+ before using.
414
+ #
415
+ # tests WidgetController
416
+ # tests :widget
417
+ # tests 'widget'
418
+ def tests(controller_class)
419
+ case controller_class
420
+ when String, Symbol
421
+ self.controller_class = "#{controller_class.to_s.camelize}Controller".constantize
422
+ when Class
423
+ self.controller_class = controller_class
424
+ else
425
+ raise ArgumentError, "controller class must be a String, Symbol, or Class"
426
+ end
427
+ end
428
+
429
+ def controller_class=(new_class)
430
+ prepare_controller_class(new_class) if new_class
431
+ self._controller_class = new_class
432
+ end
433
+
434
+ def controller_class
435
+ if current_controller_class = self._controller_class
436
+ current_controller_class
437
+ else
438
+ self.controller_class = determine_default_controller_class(name)
439
+ end
440
+ end
441
+
442
+ def determine_default_controller_class(name)
443
+ determine_constant_from_test_name(name) do |constant|
444
+ Class === constant && constant < ActionController::Metal
445
+ end
446
+ end
447
+
448
+ def prepare_controller_class(new_class)
449
+ new_class.send :include, ActionController::TestCase::RaiseActionExceptions
450
+ end
451
+
452
+ end
453
+
454
+ # Simulate a GET request with the given parameters.
455
+ #
456
+ # - +action+: The controller action to call.
457
+ # - +parameters+: The HTTP parameters that you want to pass. This may
458
+ # be +nil+, a hash, or a string that is appropriately encoded
459
+ # (<tt>application/x-www-form-urlencoded</tt> or <tt>multipart/form-data</tt>).
460
+ # - +session+: A hash of parameters to store in the session. This may be +nil+.
461
+ # - +flash+: A hash of parameters to store in the flash. This may be +nil+.
462
+ #
463
+ # You can also simulate POST, PATCH, PUT, DELETE, HEAD, and OPTIONS requests with
464
+ # +post+, +patch+, +put+, +delete+, +head+, and +options+.
465
+ #
466
+ # Note that the request method is not verified. The different methods are
467
+ # available to make the tests more expressive.
468
+ def get(action, *args)
469
+ process(action, "GET", *args)
470
+ end
471
+
472
+ # Simulate a POST request with the given parameters and set/volley the response.
473
+ # See +get+ for more details.
474
+ def post(action, *args)
475
+ process(action, "POST", *args)
476
+ end
477
+
478
+ # Simulate a PATCH request with the given parameters and set/volley the response.
479
+ # See +get+ for more details.
480
+ def patch(action, *args)
481
+ process(action, "PATCH", *args)
482
+ end
483
+
484
+ # Simulate a PUT request with the given parameters and set/volley the response.
485
+ # See +get+ for more details.
486
+ def put(action, *args)
487
+ process(action, "PUT", *args)
488
+ end
489
+
490
+ # Simulate a DELETE request with the given parameters and set/volley the response.
491
+ # See +get+ for more details.
492
+ def delete(action, *args)
493
+ process(action, "DELETE", *args)
494
+ end
495
+
496
+ <<<<<<< HEAD
497
+ # Simulate a HEAD request with the given parameters and set/volley the response.
498
+ # See +get+ for more details.
499
+ def head(action, *args)
500
+ process(action, "HEAD", *args)
501
+ end
502
+
503
+ # Simulate a OPTIONS request with the given parameters and set/volley the response.
504
+ # See +get+ for more details.
505
+ def options(action, *args)
506
+ process(action, "OPTIONS", *args)
507
+ =======
508
+ # Executes a request simulating HEAD HTTP method and set/volley the response
509
+ def head(action, parameters = nil, session = nil, flash = nil)
510
+ process(action, "HEAD", parameters, session, flash)
511
+ >>>>>>> parent of 0303c23... Add the options method to action_controller testcase.
512
+ end
513
+
514
+ def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
515
+ @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
516
+ @request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
517
+ __send__(request_method, action, parameters, session, flash).tap do
518
+ @request.env.delete 'HTTP_X_REQUESTED_WITH'
519
+ @request.env.delete 'HTTP_ACCEPT'
520
+ end
521
+ end
522
+ alias xhr :xml_http_request
523
+
524
+ def paramify_values(hash_or_array_or_value)
525
+ case hash_or_array_or_value
526
+ when Hash
527
+ Hash[hash_or_array_or_value.map{|key, value| [key, paramify_values(value)] }]
528
+ when Array
529
+ hash_or_array_or_value.map {|i| paramify_values(i)}
530
+ when Rack::Test::UploadedFile, ActionDispatch::Http::UploadedFile
531
+ hash_or_array_or_value
532
+ else
533
+ hash_or_array_or_value.to_param
534
+ end
535
+ end
536
+
537
+ def process(action, http_method = 'GET', *args)
538
+ check_required_ivars
539
+ http_method, args = handle_old_process_api(http_method, args, caller)
540
+
541
+ if args.first.is_a?(String) && http_method != 'HEAD'
542
+ @request.env['RAW_POST_DATA'] = args.shift
543
+ end
544
+
545
+ parameters, session, flash = args
546
+
547
+ # Ensure that numbers and symbols passed as params are converted to
548
+ # proper params, as is the case when engaging rack.
549
+ parameters = paramify_values(parameters) if html_format?(parameters)
550
+
551
+ @html_document = nil
552
+
553
+ unless @controller.respond_to?(:recycle!)
554
+ @controller.extend(Testing::Functional)
555
+ @controller.class.class_eval { include Testing }
556
+ end
557
+
558
+ @request.recycle!
559
+ @response.recycle!
560
+ @controller.recycle!
561
+
562
+ @request.env['REQUEST_METHOD'] = http_method
563
+
564
+ parameters ||= {}
565
+ controller_class_name = @controller.class.anonymous? ?
566
+ "anonymous" :
567
+ @controller.class.controller_path
568
+
569
+ @request.assign_parameters(@routes, controller_class_name, action.to_s, parameters)
570
+
571
+ @request.session.update(session) if session
572
+ @request.flash.update(flash || {})
573
+
574
+ @controller.request = @request
575
+ @controller.response = @response
576
+
577
+ build_request_uri(action, parameters)
578
+
579
+ name = @request.parameters[:action]
580
+
581
+ @controller.process(name)
582
+
583
+ if cookies = @request.env['action_dispatch.cookies']
584
+ cookies.write(@response)
585
+ end
586
+ @response.prepare!
587
+
588
+ @assigns = @controller.respond_to?(:view_assigns) ? @controller.view_assigns : {}
589
+ @request.session['flash'] = @request.flash.to_session_value
590
+ @request.session.delete('flash') if @request.session['flash'].blank?
591
+ @response
592
+ end
593
+
594
+ def setup_controller_request_and_response
595
+ @request = build_request
596
+ @response = build_response
597
+ @response.request = @request
598
+
599
+ @controller = nil unless defined? @controller
600
+
601
+ if klass = self.class.controller_class
602
+ unless @controller
603
+ begin
604
+ @controller = klass.new
605
+ rescue
606
+ warn "could not construct controller #{klass}" if $VERBOSE
607
+ end
608
+ end
609
+ end
610
+
611
+ if @controller
612
+ @controller.request = @request
613
+ @controller.params = {}
614
+ end
615
+ end
616
+
617
+ def build_request
618
+ TestRequest.new
619
+ end
620
+
621
+ def build_response
622
+ TestResponse.new
623
+ end
624
+
625
+ included do
626
+ include ActionController::TemplateAssertions
627
+ include ActionDispatch::Assertions
628
+ class_attribute :_controller_class
629
+ setup :setup_controller_request_and_response
630
+ end
631
+
632
+ private
633
+ def check_required_ivars
634
+ # Sanity check for required instance variables so we can give an
635
+ # understandable error message.
636
+ [:@routes, :@controller, :@request, :@response].each do |iv_name|
637
+ if !instance_variable_defined?(iv_name) || instance_variable_get(iv_name).nil?
638
+ raise "#{iv_name} is nil: make sure you set it in your test's setup method."
639
+ end
640
+ end
641
+ end
642
+
643
+ def handle_old_process_api(http_method, args, callstack)
644
+ # 4.0: Remove this method.
645
+ if http_method.is_a?(Hash)
646
+ ActiveSupport::Deprecation.warn("TestCase#process now expects the HTTP method as second argument: process(action, http_method, params, session, flash)", callstack)
647
+ args.unshift(http_method)
648
+ http_method = args.last.is_a?(String) ? args.last : "GET"
649
+ end
650
+
651
+ [http_method, args]
652
+ end
653
+
654
+ def build_request_uri(action, parameters)
655
+ unless @request.env["PATH_INFO"]
656
+ options = @controller.respond_to?(:url_options) ? @controller.__send__(:url_options).merge(parameters) : parameters
657
+ options.update(
658
+ :only_path => true,
659
+ :action => action,
660
+ :relative_url_root => nil,
661
+ :_recall => @request.symbolized_path_parameters)
662
+
663
+ url, query_string = @routes.url_for(options).split("?", 2)
664
+
665
+ @request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
666
+ @request.env["PATH_INFO"] = url
667
+ @request.env["QUERY_STRING"] = query_string || ""
668
+ end
669
+ end
670
+
671
+ def html_format?(parameters)
672
+ return true unless parameters.is_a?(Hash)
673
+ Mime.fetch(parameters[:format]) { Mime['html'] }.html?
674
+ end
675
+ end
676
+
677
+ # When the request.remote_addr remains the default for testing, which is 0.0.0.0, the exception is simply raised inline
678
+ # (skipping the regular exception handling from rescue_action). If the request.remote_addr is anything else, the regular
679
+ # rescue_action process takes place. This means you can test your rescue_action code by setting remote_addr to something else
680
+ # than 0.0.0.0.
681
+ #
682
+ # The exception is stored in the exception accessor for further inspection.
683
+ module RaiseActionExceptions
684
+ def self.included(base) #:nodoc:
685
+ unless base.method_defined?(:exception) && base.method_defined?(:exception=)
686
+ base.class_eval do
687
+ attr_accessor :exception
688
+ protected :exception, :exception=
689
+ end
690
+ end
691
+ end
692
+
693
+ protected
694
+ def rescue_action_without_handler(e)
695
+ self.exception = e
696
+
697
+ if request.remote_addr == "0.0.0.0"
698
+ raise(e)
699
+ else
700
+ super(e)
701
+ end
702
+ end
703
+ end
704
+
705
+ include Behavior
706
+ end
707
+ end