actionpack 4.1.16 → 4.2.0.beta1

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 (99) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +163 -690
  3. data/README.rdoc +7 -2
  4. data/lib/abstract_controller/base.rb +16 -6
  5. data/lib/abstract_controller/callbacks.rb +28 -51
  6. data/lib/abstract_controller/helpers.rb +0 -3
  7. data/lib/abstract_controller/railties/routes_helpers.rb +3 -3
  8. data/lib/abstract_controller/rendering.rb +1 -7
  9. data/lib/abstract_controller/url_for.rb +1 -1
  10. data/lib/action_controller.rb +1 -0
  11. data/lib/action_controller/base.rb +2 -1
  12. data/lib/action_controller/caching.rb +1 -1
  13. data/lib/action_controller/caching/fragments.rb +7 -1
  14. data/lib/action_controller/log_subscriber.rb +26 -25
  15. data/lib/action_controller/metal.rb +11 -7
  16. data/lib/action_controller/metal/conditional_get.rb +31 -6
  17. data/lib/action_controller/metal/etag_with_template_digest.rb +50 -0
  18. data/lib/action_controller/metal/force_ssl.rb +1 -1
  19. data/lib/action_controller/metal/head.rb +2 -0
  20. data/lib/action_controller/metal/http_authentication.rb +3 -15
  21. data/lib/action_controller/metal/instrumentation.rb +4 -7
  22. data/lib/action_controller/metal/live.rb +57 -6
  23. data/lib/action_controller/metal/mime_responds.rb +17 -227
  24. data/lib/action_controller/metal/redirecting.rb +14 -8
  25. data/lib/action_controller/metal/renderers.rb +19 -3
  26. data/lib/action_controller/metal/rendering.rb +2 -6
  27. data/lib/action_controller/metal/request_forgery_protection.rb +75 -7
  28. data/lib/action_controller/metal/streaming.rb +1 -1
  29. data/lib/action_controller/metal/strong_parameters.rb +111 -11
  30. data/lib/action_controller/metal/url_for.rb +11 -12
  31. data/lib/action_controller/model_naming.rb +1 -1
  32. data/lib/action_controller/railtie.rb +4 -0
  33. data/lib/action_controller/test_case.rb +87 -75
  34. data/lib/action_dispatch/http/cache.rb +1 -1
  35. data/lib/action_dispatch/http/filter_parameters.rb +2 -2
  36. data/lib/action_dispatch/http/headers.rb +43 -9
  37. data/lib/action_dispatch/http/mime_negotiation.rb +10 -4
  38. data/lib/action_dispatch/http/mime_type.rb +2 -16
  39. data/lib/action_dispatch/http/parameter_filter.rb +1 -1
  40. data/lib/action_dispatch/http/parameters.rb +11 -26
  41. data/lib/action_dispatch/http/request.rb +30 -10
  42. data/lib/action_dispatch/http/response.rb +52 -17
  43. data/lib/action_dispatch/http/upload.rb +3 -8
  44. data/lib/action_dispatch/http/url.rb +87 -70
  45. data/lib/action_dispatch/journey/formatter.rb +18 -17
  46. data/lib/action_dispatch/journey/gtg/builder.rb +3 -3
  47. data/lib/action_dispatch/journey/gtg/simulator.rb +10 -7
  48. data/lib/action_dispatch/journey/gtg/transition_table.rb +18 -26
  49. data/lib/action_dispatch/journey/nfa/dot.rb +2 -2
  50. data/lib/action_dispatch/journey/nfa/simulator.rb +1 -1
  51. data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -5
  52. data/lib/action_dispatch/journey/nodes/node.rb +4 -0
  53. data/lib/action_dispatch/journey/parser.rb +52 -60
  54. data/lib/action_dispatch/journey/parser.y +11 -10
  55. data/lib/action_dispatch/journey/path/pattern.rb +16 -19
  56. data/lib/action_dispatch/journey/route.rb +3 -18
  57. data/lib/action_dispatch/journey/router.rb +34 -65
  58. data/lib/action_dispatch/journey/router/strexp.rb +9 -6
  59. data/lib/action_dispatch/journey/routes.rb +0 -4
  60. data/lib/action_dispatch/journey/visitors.rb +81 -92
  61. data/lib/action_dispatch/journey/visualizer/index.html.erb +2 -2
  62. data/lib/action_dispatch/middleware/cookies.rb +27 -31
  63. data/lib/action_dispatch/middleware/debug_exceptions.rb +32 -3
  64. data/lib/action_dispatch/middleware/exception_wrapper.rb +19 -17
  65. data/lib/action_dispatch/middleware/flash.rb +7 -4
  66. data/lib/action_dispatch/middleware/public_exceptions.rb +13 -8
  67. data/lib/action_dispatch/middleware/remote_ip.rb +3 -3
  68. data/lib/action_dispatch/middleware/request_id.rb +1 -1
  69. data/lib/action_dispatch/middleware/session/cookie_store.rb +1 -1
  70. data/lib/action_dispatch/middleware/show_exceptions.rb +1 -0
  71. data/lib/action_dispatch/middleware/static.rb +22 -23
  72. data/lib/action_dispatch/middleware/templates/rescues/_source.erb +22 -18
  73. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +36 -8
  74. data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +2 -8
  75. data/lib/action_dispatch/middleware/templates/rescues/{diagnostics.erb → diagnostics.html.erb} +0 -0
  76. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
  77. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +6 -0
  78. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -24
  79. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +0 -1
  80. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +119 -63
  81. data/lib/action_dispatch/routing/endpoint.rb +10 -0
  82. data/lib/action_dispatch/routing/inspector.rb +4 -11
  83. data/lib/action_dispatch/routing/mapper.rb +399 -278
  84. data/lib/action_dispatch/routing/polymorphic_routes.rb +190 -78
  85. data/lib/action_dispatch/routing/redirection.rb +10 -12
  86. data/lib/action_dispatch/routing/route_set.rb +224 -177
  87. data/lib/action_dispatch/routing/url_for.rb +9 -4
  88. data/lib/action_dispatch/testing/assertions.rb +11 -7
  89. data/lib/action_dispatch/testing/assertions/dom.rb +2 -26
  90. data/lib/action_dispatch/testing/assertions/response.rb +2 -7
  91. data/lib/action_dispatch/testing/assertions/routing.rb +9 -9
  92. data/lib/action_dispatch/testing/assertions/selector.rb +2 -429
  93. data/lib/action_dispatch/testing/assertions/tag.rb +2 -134
  94. data/lib/action_dispatch/testing/integration.rb +15 -18
  95. data/lib/action_dispatch/testing/test_request.rb +1 -1
  96. data/lib/action_dispatch/testing/test_response.rb +5 -1
  97. data/lib/action_pack/gem_version.rb +3 -3
  98. metadata +57 -15
  99. data/lib/action_controller/metal/responder.rb +0 -297
@@ -152,21 +152,26 @@ module ActionDispatch
152
152
  when nil
153
153
  _routes.url_for(url_options.symbolize_keys)
154
154
  when Hash
155
- _routes.url_for(options.symbolize_keys.reverse_merge!(url_options))
155
+ route_name = options.delete :use_route
156
+ _routes.url_for(options.symbolize_keys.reverse_merge!(url_options),
157
+ route_name)
156
158
  when String
157
159
  options
160
+ when Symbol
161
+ HelperMethodBuilder.url.handle_string_call self, options
158
162
  when Array
159
163
  polymorphic_url(options, options.extract_options!)
164
+ when Class
165
+ HelperMethodBuilder.url.handle_class_call self, options
160
166
  else
161
- polymorphic_url(options)
167
+ HelperMethodBuilder.url.handle_model_call self, options
162
168
  end
163
169
  end
164
170
 
165
171
  protected
166
172
 
167
173
  def optimize_routes_generation?
168
- return @_optimized_routes if defined?(@_optimized_routes)
169
- @_optimized_routes = _routes.optimize_routes_generation? && default_url_options.empty?
174
+ _routes.optimize_routes_generation? && default_url_options.empty?
170
175
  end
171
176
 
172
177
  def _with_routes(routes)
@@ -1,18 +1,22 @@
1
+ require 'rails-dom-testing'
2
+
1
3
  module ActionDispatch
2
4
  module Assertions
3
- autoload :DomAssertions, 'action_dispatch/testing/assertions/dom'
4
5
  autoload :ResponseAssertions, 'action_dispatch/testing/assertions/response'
5
6
  autoload :RoutingAssertions, 'action_dispatch/testing/assertions/routing'
6
- autoload :SelectorAssertions, 'action_dispatch/testing/assertions/selector'
7
- autoload :TagAssertions, 'action_dispatch/testing/assertions/tag'
8
7
 
9
8
  extend ActiveSupport::Concern
10
9
 
11
- include DomAssertions
12
10
  include ResponseAssertions
13
11
  include RoutingAssertions
14
- include SelectorAssertions
15
- include TagAssertions
12
+ include Rails::Dom::Testing::Assertions
13
+
14
+ def html_document
15
+ @html_document ||= if @response.content_type =~ /xml$/
16
+ Nokogiri::XML::Document.parse(@response.body)
17
+ else
18
+ Nokogiri::HTML::Document.parse(@response.body)
19
+ end
20
+ end
16
21
  end
17
22
  end
18
-
@@ -1,27 +1,3 @@
1
- require 'action_view/vendor/html-scanner'
1
+ require 'active_support/deprecation'
2
2
 
3
- module ActionDispatch
4
- module Assertions
5
- module DomAssertions
6
- # \Test two HTML strings for equivalency (e.g., identical up to reordering of attributes)
7
- #
8
- # # assert that the referenced method generates the appropriate HTML string
9
- # assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
10
- def assert_dom_equal(expected, actual, message = nil)
11
- expected_dom = HTML::Document.new(expected).root
12
- actual_dom = HTML::Document.new(actual).root
13
- assert_equal expected_dom, actual_dom, message
14
- end
15
-
16
- # The negated form of +assert_dom_equivalent+.
17
- #
18
- # # assert that the referenced method does not generate the specified HTML string
19
- # assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
20
- def assert_dom_not_equal(expected, actual, message = nil)
21
- expected_dom = HTML::Document.new(expected).root
22
- actual_dom = HTML::Document.new(actual).root
23
- assert_not_equal expected_dom, actual_dom, message
24
- end
25
- end
26
- end
27
- end
3
+ ActiveSupport::Deprecation.warn("ActionDispatch::Assertions::DomAssertions has been extracted to the rails-dom-testing gem.")
@@ -73,13 +73,8 @@ module ActionDispatch
73
73
  if Regexp === fragment
74
74
  fragment
75
75
  else
76
- handle = @controller || Class.new(ActionController::Metal) do
77
- include ActionController::Redirecting
78
- def initialize(request)
79
- @_request = request
80
- end
81
- end.new(@request)
82
- handle._compute_redirect_to_location(fragment)
76
+ handle = @controller || ActionController::Redirecting
77
+ handle._compute_redirect_to_location(@request, fragment)
83
78
  end
84
79
  end
85
80
  end
@@ -38,7 +38,7 @@ module ActionDispatch
38
38
  # # Test a custom route
39
39
  # assert_recognizes({controller: 'items', action: 'show', id: '1'}, 'view/item1')
40
40
  def assert_recognizes(expected_options, path, extras={}, msg=nil)
41
- request = recognized_request_for(path, extras, msg)
41
+ request = recognized_request_for(path, extras)
42
42
 
43
43
  expected_options = expected_options.clone
44
44
 
@@ -69,9 +69,9 @@ module ActionDispatch
69
69
  #
70
70
  # # Asserts that the generated route gives us our custom route
71
71
  # assert_generates "changesets/12", { controller: 'scm', action: 'show_diff', revision: "12" }
72
- def assert_generates(expected_path, options, defaults={}, extras={}, message=nil)
72
+ def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
73
73
  if expected_path =~ %r{://}
74
- fail_on(URI::InvalidURIError, message) do
74
+ fail_on(URI::InvalidURIError) do
75
75
  uri = URI.parse(expected_path)
76
76
  expected_path = uri.path.to_s.empty? ? "/" : uri.path
77
77
  end
@@ -165,7 +165,7 @@ module ActionDispatch
165
165
 
166
166
  # ROUTES TODO: These assertions should really work in an integration context
167
167
  def method_missing(selector, *args, &block)
168
- if defined?(@controller) && @controller && @routes && @routes.named_routes.helpers.include?(selector)
168
+ if defined?(@controller) && @controller && @routes && @routes.named_routes.route_defined?(selector)
169
169
  @controller.send(selector, *args, &block)
170
170
  else
171
171
  super
@@ -174,7 +174,7 @@ module ActionDispatch
174
174
 
175
175
  private
176
176
  # Recognizes the route for a given path.
177
- def recognized_request_for(path, extras = {}, msg)
177
+ def recognized_request_for(path, extras = {})
178
178
  if path.is_a?(Hash)
179
179
  method = path[:method]
180
180
  path = path[:path]
@@ -186,7 +186,7 @@ module ActionDispatch
186
186
  request = ActionController::TestRequest.new
187
187
 
188
188
  if path =~ %r{://}
189
- fail_on(URI::InvalidURIError, msg) do
189
+ fail_on(URI::InvalidURIError) do
190
190
  uri = URI.parse(path)
191
191
  request.env["rack.url_scheme"] = uri.scheme || "http"
192
192
  request.host = uri.host if uri.host
@@ -200,7 +200,7 @@ module ActionDispatch
200
200
 
201
201
  request.request_method = method if method
202
202
 
203
- params = fail_on(ActionController::RoutingError, msg) do
203
+ params = fail_on(ActionController::RoutingError) do
204
204
  @routes.recognize_path(path, { :method => method, :extras => extras })
205
205
  end
206
206
  request.path_parameters = params.with_indifferent_access
@@ -208,10 +208,10 @@ module ActionDispatch
208
208
  request
209
209
  end
210
210
 
211
- def fail_on(exception_class, message)
211
+ def fail_on(exception_class)
212
212
  yield
213
213
  rescue exception_class => e
214
- raise Minitest::Assertion, message || e.message
214
+ raise Minitest::Assertion, e.message
215
215
  end
216
216
  end
217
217
  end
@@ -1,430 +1,3 @@
1
- require 'action_view/vendor/html-scanner'
2
- require 'active_support/core_ext/object/inclusion'
1
+ require 'active_support/deprecation'
3
2
 
4
- #--
5
- # Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
6
- # Under MIT and/or CC By license.
7
- #++
8
-
9
- module ActionDispatch
10
- module Assertions
11
- NO_STRIP = %w{pre script style textarea}
12
-
13
- # Adds the +assert_select+ method for use in Rails functional
14
- # test cases, which can be used to make assertions on the response HTML of a controller
15
- # action. You can also call +assert_select+ within another +assert_select+ to
16
- # make assertions on elements selected by the enclosing assertion.
17
- #
18
- # Use +css_select+ to select elements without making an assertions, either
19
- # from the response HTML or elements selected by the enclosing assertion.
20
- #
21
- # In addition to HTML responses, you can make the following assertions:
22
- #
23
- # * +assert_select_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
24
- # * +assert_select_email+ - Assertions on the HTML body of an e-mail.
25
- #
26
- # Also see HTML::Selector to learn how to use selectors.
27
- module SelectorAssertions
28
- # Select and return all matching elements.
29
- #
30
- # If called with a single argument, uses that argument as a selector
31
- # to match all elements of the current page. Returns an empty array
32
- # if no match is found.
33
- #
34
- # If called with two arguments, uses the first argument as the base
35
- # element and the second argument as the selector. Attempts to match the
36
- # base element and any of its children. Returns an empty array if no
37
- # match is found.
38
- #
39
- # The selector may be a CSS selector expression (String), an expression
40
- # with substitution values (Array) or an HTML::Selector object.
41
- #
42
- # # Selects all div tags
43
- # divs = css_select("div")
44
- #
45
- # # Selects all paragraph tags and does something interesting
46
- # pars = css_select("p")
47
- # pars.each do |par|
48
- # # Do something fun with paragraphs here...
49
- # end
50
- #
51
- # # Selects all list items in unordered lists
52
- # items = css_select("ul>li")
53
- #
54
- # # Selects all form tags and then all inputs inside the form
55
- # forms = css_select("form")
56
- # forms.each do |form|
57
- # inputs = css_select(form, "input")
58
- # ...
59
- # end
60
- def css_select(*args)
61
- # See assert_select to understand what's going on here.
62
- arg = args.shift
63
-
64
- if arg.is_a?(HTML::Node)
65
- root = arg
66
- arg = args.shift
67
- elsif arg == nil
68
- raise ArgumentError, "First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?"
69
- elsif defined?(@selected) && @selected
70
- matches = []
71
-
72
- @selected.each do |selected|
73
- subset = css_select(selected, HTML::Selector.new(arg.dup, args.dup))
74
- subset.each do |match|
75
- matches << match unless matches.any? { |m| m.equal?(match) }
76
- end
77
- end
78
-
79
- return matches
80
- else
81
- root = response_from_page
82
- end
83
-
84
- case arg
85
- when String
86
- selector = HTML::Selector.new(arg, args)
87
- when Array
88
- selector = HTML::Selector.new(*arg)
89
- when HTML::Selector
90
- selector = arg
91
- else raise ArgumentError, "Expecting a selector as the first argument"
92
- end
93
-
94
- selector.select(root)
95
- end
96
-
97
- # An assertion that selects elements and makes one or more equality tests.
98
- #
99
- # If the first argument is an element, selects all matching elements
100
- # starting from (and including) that element and all its children in
101
- # depth-first order.
102
- #
103
- # If no element if specified, calling +assert_select+ selects from the
104
- # response HTML unless +assert_select+ is called from within an +assert_select+ block.
105
- #
106
- # When called with a block +assert_select+ passes an array of selected elements
107
- # to the block. Calling +assert_select+ from the block, with no element specified,
108
- # runs the assertion on the complete set of elements selected by the enclosing assertion.
109
- # Alternatively the array may be iterated through so that +assert_select+ can be called
110
- # separately for each element.
111
- #
112
- #
113
- # ==== Example
114
- # If the response contains two ordered lists, each with four list elements then:
115
- # assert_select "ol" do |elements|
116
- # elements.each do |element|
117
- # assert_select element, "li", 4
118
- # end
119
- # end
120
- #
121
- # will pass, as will:
122
- # assert_select "ol" do
123
- # assert_select "li", 8
124
- # end
125
- #
126
- # The selector may be a CSS selector expression (String), an expression
127
- # with substitution values, or an HTML::Selector object.
128
- #
129
- # === Equality Tests
130
- #
131
- # The equality test may be one of the following:
132
- # * <tt>true</tt> - Assertion is true if at least one element selected.
133
- # * <tt>false</tt> - Assertion is true if no element selected.
134
- # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
135
- # one element matches the string or regular expression.
136
- # * <tt>Integer</tt> - Assertion is true if exactly that number of
137
- # elements are selected.
138
- # * <tt>Range</tt> - Assertion is true if the number of selected
139
- # elements fit the range.
140
- # If no equality test specified, the assertion is true if at least one
141
- # element selected.
142
- #
143
- # To perform more than one equality tests, use a hash with the following keys:
144
- # * <tt>:text</tt> - Narrow the selection to elements that have this text
145
- # value (string or regexp).
146
- # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
147
- # content (string or regexp).
148
- # * <tt>:count</tt> - Assertion is true if the number of selected elements
149
- # is equal to this value.
150
- # * <tt>:minimum</tt> - Assertion is true if the number of selected
151
- # elements is at least this value.
152
- # * <tt>:maximum</tt> - Assertion is true if the number of selected
153
- # elements is at most this value.
154
- #
155
- # If the method is called with a block, once all equality tests are
156
- # evaluated the block is called with an array of all matched elements.
157
- #
158
- # # At least one form element
159
- # assert_select "form"
160
- #
161
- # # Form element includes four input fields
162
- # assert_select "form input", 4
163
- #
164
- # # Page title is "Welcome"
165
- # assert_select "title", "Welcome"
166
- #
167
- # # Page title is "Welcome" and there is only one title element
168
- # assert_select "title", {count: 1, text: "Welcome"},
169
- # "Wrong title or more than one title element"
170
- #
171
- # # Page contains no forms
172
- # assert_select "form", false, "This page must contain no forms"
173
- #
174
- # # Test the content and style
175
- # assert_select "body div.header ul.menu"
176
- #
177
- # # Use substitution values
178
- # assert_select "ol>li#?", /item-\d+/
179
- #
180
- # # All input fields in the form have a name
181
- # assert_select "form input" do
182
- # assert_select "[name=?]", /.+/ # Not empty
183
- # end
184
- def assert_select(*args, &block)
185
- # Start with optional element followed by mandatory selector.
186
- arg = args.shift
187
- @selected ||= nil
188
-
189
- if arg.is_a?(HTML::Node)
190
- # First argument is a node (tag or text, but also HTML root),
191
- # so we know what we're selecting from.
192
- root = arg
193
- arg = args.shift
194
- elsif arg == nil
195
- # This usually happens when passing a node/element that
196
- # happens to be nil.
197
- raise ArgumentError, "First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?"
198
- elsif @selected
199
- root = HTML::Node.new(nil)
200
- root.children.concat @selected
201
- else
202
- # Otherwise just operate on the response document.
203
- root = response_from_page
204
- end
205
-
206
- # First or second argument is the selector: string and we pass
207
- # all remaining arguments. Array and we pass the argument. Also
208
- # accepts selector itself.
209
- case arg
210
- when String
211
- selector = HTML::Selector.new(arg, args)
212
- when Array
213
- selector = HTML::Selector.new(*arg)
214
- when HTML::Selector
215
- selector = arg
216
- else raise ArgumentError, "Expecting a selector as the first argument"
217
- end
218
-
219
- # Next argument is used for equality tests.
220
- equals = {}
221
- case arg = args.shift
222
- when Hash
223
- equals = arg
224
- when String, Regexp
225
- equals[:text] = arg
226
- when Integer
227
- equals[:count] = arg
228
- when Range
229
- equals[:minimum] = arg.begin
230
- equals[:maximum] = arg.end
231
- when FalseClass
232
- equals[:count] = 0
233
- when NilClass, TrueClass
234
- equals[:minimum] = 1
235
- else raise ArgumentError, "I don't understand what you're trying to match"
236
- end
237
-
238
- # By default we're looking for at least one match.
239
- if equals[:count]
240
- equals[:minimum] = equals[:maximum] = equals[:count]
241
- else
242
- equals[:minimum] = 1 unless equals[:minimum]
243
- end
244
-
245
- # Last argument is the message we use if the assertion fails.
246
- message = args.shift
247
- #- message = "No match made with selector #{selector.inspect}" unless message
248
- if args.shift
249
- raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type"
250
- end
251
-
252
- matches = selector.select(root)
253
- # If text/html, narrow down to those elements that match it.
254
- content_mismatch = nil
255
- if match_with = equals[:text]
256
- matches.delete_if do |match|
257
- text = ""
258
- stack = match.children.reverse
259
- while node = stack.pop
260
- if node.tag?
261
- stack.concat node.children.reverse
262
- else
263
- content = node.content
264
- text << content
265
- end
266
- end
267
- text.strip! unless NO_STRIP.include?(match.name)
268
- text.sub!(/\A\n/, '') if match.name == "textarea"
269
- unless match_with.is_a?(Regexp) ? (text =~ match_with) : (text == match_with.to_s)
270
- content_mismatch ||= sprintf("<%s> expected but was\n<%s>", match_with, text)
271
- true
272
- end
273
- end
274
- elsif match_with = equals[:html]
275
- matches.delete_if do |match|
276
- html = match.children.map(&:to_s).join
277
- html.strip! unless NO_STRIP.include?(match.name)
278
- unless match_with.is_a?(Regexp) ? (html =~ match_with) : (html == match_with.to_s)
279
- content_mismatch ||= sprintf("<%s> expected but was\n<%s>", match_with, html)
280
- true
281
- end
282
- end
283
- end
284
- # Expecting foo found bar element only if found zero, not if
285
- # found one but expecting two.
286
- message ||= content_mismatch if matches.empty?
287
- # Test minimum/maximum occurrence.
288
- min, max, count = equals[:minimum], equals[:maximum], equals[:count]
289
-
290
- # FIXME: minitest provides messaging when we use assert_operator,
291
- # so is this custom message really needed?
292
- message = message || %(Expected #{count_description(min, max, count)} matching "#{selector.to_s}", found #{matches.size})
293
- if count
294
- assert_equal count, matches.size, message
295
- else
296
- assert_operator matches.size, :>=, min, message if min
297
- assert_operator matches.size, :<=, max, message if max
298
- end
299
-
300
- # If a block is given call that block. Set @selected to allow
301
- # nested assert_select, which can be nested several levels deep.
302
- if block_given? && !matches.empty?
303
- begin
304
- in_scope, @selected = @selected, matches
305
- yield matches
306
- ensure
307
- @selected = in_scope
308
- end
309
- end
310
-
311
- # Returns all matches elements.
312
- matches
313
- end
314
-
315
- def count_description(min, max, count) #:nodoc:
316
- pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')}
317
-
318
- if min && max && (max != min)
319
- "between #{min} and #{max} elements"
320
- elsif min && max && max == min && count
321
- "exactly #{count} #{pluralize['element', min]}"
322
- elsif min && !(min == 1 && max == 1)
323
- "at least #{min} #{pluralize['element', min]}"
324
- elsif max
325
- "at most #{max} #{pluralize['element', max]}"
326
- end
327
- end
328
-
329
- # Extracts the content of an element, treats it as encoded HTML and runs
330
- # nested assertion on it.
331
- #
332
- # You typically call this method within another assertion to operate on
333
- # all currently selected elements. You can also pass an element or array
334
- # of elements.
335
- #
336
- # The content of each element is un-encoded, and wrapped in the root
337
- # element +encoded+. It then calls the block with all un-encoded elements.
338
- #
339
- # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
340
- # assert_select "feed[xmlns='http://www.w3.org/2005/Atom']" do
341
- # # Select each entry item and then the title item
342
- # assert_select "entry>title" do
343
- # # Run assertions on the encoded title elements
344
- # assert_select_encoded do
345
- # assert_select "b"
346
- # end
347
- # end
348
- # end
349
- #
350
- #
351
- # # Selects all paragraph tags from within the description of an RSS feed
352
- # assert_select "rss[version=2.0]" do
353
- # # Select description element of each feed item.
354
- # assert_select "channel>item>description" do
355
- # # Run assertions on the encoded elements.
356
- # assert_select_encoded do
357
- # assert_select "p"
358
- # end
359
- # end
360
- # end
361
- def assert_select_encoded(element = nil, &block)
362
- case element
363
- when Array
364
- elements = element
365
- when HTML::Node
366
- elements = [element]
367
- when nil
368
- unless elements = @selected
369
- raise ArgumentError, "First argument is optional, but must be called from a nested assert_select"
370
- end
371
- else
372
- raise ArgumentError, "Argument is optional, and may be node or array of nodes"
373
- end
374
-
375
- fix_content = lambda do |node|
376
- # Gets around a bug in the Rails 1.1 HTML parser.
377
- node.content.gsub(/<!\[CDATA\[(.*)(\]\]>)?/m) { Rack::Utils.escapeHTML($1) }
378
- end
379
-
380
- selected = elements.map do |elem|
381
- text = elem.children.select{ |c| not c.tag? }.map{ |c| fix_content[c] }.join
382
- root = HTML::Document.new(CGI.unescapeHTML("<encoded>#{text}</encoded>")).root
383
- css_select(root, "encoded:root", &block)[0]
384
- end
385
-
386
- begin
387
- old_selected, @selected = @selected, selected
388
- assert_select ":root", &block
389
- ensure
390
- @selected = old_selected
391
- end
392
- end
393
-
394
- # Extracts the body of an email and runs nested assertions on it.
395
- #
396
- # You must enable deliveries for this assertion to work, use:
397
- # ActionMailer::Base.perform_deliveries = true
398
- #
399
- # assert_select_email do
400
- # assert_select "h1", "Email alert"
401
- # end
402
- #
403
- # assert_select_email do
404
- # items = assert_select "ol>li"
405
- # items.each do
406
- # # Work with items here...
407
- # end
408
- # end
409
- def assert_select_email(&block)
410
- deliveries = ActionMailer::Base.deliveries
411
- assert !deliveries.empty?, "No e-mail in delivery list"
412
-
413
- deliveries.each do |delivery|
414
- (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
415
- if part["Content-Type"].to_s =~ /^text\/html\W/
416
- root = HTML::Document.new(part.body.to_s).root
417
- assert_select root, ":root", &block
418
- end
419
- end
420
- end
421
- end
422
-
423
- protected
424
- # +assert_select+ and +css_select+ call this to obtain the content in the HTML page.
425
- def response_from_page
426
- html_document.root
427
- end
428
- end
429
- end
430
- end
3
+ ActiveSupport::Deprecation.warn("ActionDispatch::Assertions::SelectorAssertions has been has been extracted to the rails-dom-testing gem.")