actionpack 4.0.1 → 4.2.11.1
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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +402 -1173
- data/MIT-LICENSE +1 -1
- data/README.rdoc +7 -7
- data/lib/abstract_controller/base.rb +39 -7
- data/lib/abstract_controller/callbacks.rb +32 -53
- data/lib/abstract_controller/collector.rb +11 -1
- data/lib/abstract_controller/helpers.rb +26 -16
- data/lib/abstract_controller/railties/routes_helpers.rb +3 -3
- data/lib/abstract_controller/rendering.rb +57 -127
- data/lib/abstract_controller/url_for.rb +1 -1
- data/lib/abstract_controller.rb +1 -2
- data/lib/action_controller/base.rb +19 -10
- data/lib/action_controller/caching/fragments.rb +7 -1
- data/lib/action_controller/caching.rb +2 -12
- data/lib/action_controller/log_subscriber.rb +29 -20
- data/lib/action_controller/metal/conditional_get.rb +37 -12
- data/lib/action_controller/metal/data_streaming.rb +1 -1
- data/lib/action_controller/metal/etag_with_template_digest.rb +50 -0
- data/lib/action_controller/metal/exceptions.rb +1 -1
- data/lib/action_controller/metal/flash.rb +17 -0
- data/lib/action_controller/metal/force_ssl.rb +2 -2
- data/lib/action_controller/metal/head.rb +8 -6
- data/lib/action_controller/metal/helpers.rb +6 -2
- data/lib/action_controller/metal/http_authentication.rb +45 -23
- data/lib/action_controller/metal/instrumentation.rb +9 -6
- data/lib/action_controller/metal/live.rb +173 -20
- data/lib/action_controller/metal/mime_responds.rb +127 -232
- data/lib/action_controller/metal/params_wrapper.rb +16 -9
- data/lib/action_controller/metal/rack_delegation.rb +1 -1
- data/lib/action_controller/metal/redirecting.rb +34 -26
- data/lib/action_controller/metal/renderers.rb +39 -12
- data/lib/action_controller/metal/rendering.rb +41 -14
- data/lib/action_controller/metal/request_forgery_protection.rb +147 -19
- data/lib/action_controller/metal/streaming.rb +19 -21
- data/lib/action_controller/metal/strong_parameters.rb +166 -22
- data/lib/action_controller/metal/testing.rb +0 -1
- data/lib/action_controller/metal/url_for.rb +11 -12
- data/lib/action_controller/metal.rb +14 -8
- data/lib/action_controller/model_naming.rb +1 -1
- data/lib/action_controller/railtie.rb +5 -1
- data/lib/action_controller/test_case.rb +160 -94
- data/lib/action_controller.rb +2 -18
- data/lib/action_dispatch/http/cache.rb +5 -4
- data/lib/action_dispatch/http/filter_parameters.rb +2 -2
- data/lib/action_dispatch/http/filter_redirect.rb +5 -4
- data/lib/action_dispatch/http/headers.rb +46 -10
- data/lib/action_dispatch/http/mime_negotiation.rb +31 -4
- data/lib/action_dispatch/http/mime_type.rb +25 -26
- data/lib/action_dispatch/http/mime_types.rb +1 -0
- data/lib/action_dispatch/http/parameter_filter.rb +1 -1
- data/lib/action_dispatch/http/parameters.rb +25 -41
- data/lib/action_dispatch/http/request.rb +49 -32
- data/lib/action_dispatch/http/response.rb +127 -25
- data/lib/action_dispatch/http/upload.rb +9 -21
- data/lib/action_dispatch/http/url.rb +97 -70
- data/lib/action_dispatch/journey/formatter.rb +35 -19
- data/lib/action_dispatch/journey/gtg/builder.rb +3 -3
- data/lib/action_dispatch/journey/gtg/simulator.rb +10 -7
- data/lib/action_dispatch/journey/gtg/transition_table.rb +23 -33
- data/lib/action_dispatch/journey/nfa/dot.rb +2 -2
- data/lib/action_dispatch/journey/nfa/simulator.rb +1 -1
- data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -5
- data/lib/action_dispatch/journey/nodes/node.rb +4 -0
- data/lib/action_dispatch/journey/parser.rb +51 -59
- data/lib/action_dispatch/journey/parser.y +12 -10
- data/lib/action_dispatch/journey/path/pattern.rb +16 -19
- data/lib/action_dispatch/journey/route.rb +8 -19
- data/lib/action_dispatch/journey/router/strexp.rb +9 -6
- data/lib/action_dispatch/journey/router/utils.rb +54 -18
- data/lib/action_dispatch/journey/router.rb +53 -75
- data/lib/action_dispatch/journey/routes.rb +4 -0
- data/lib/action_dispatch/journey/scanner.rb +5 -5
- data/lib/action_dispatch/journey/visitors.rb +81 -60
- data/lib/action_dispatch/journey/visualizer/fsm.css +0 -4
- data/lib/action_dispatch/journey/visualizer/index.html.erb +2 -2
- data/lib/action_dispatch/middleware/callbacks.rb +7 -7
- data/lib/action_dispatch/middleware/cookies.rb +119 -43
- data/lib/action_dispatch/middleware/debug_exceptions.rb +32 -13
- data/lib/action_dispatch/middleware/exception_wrapper.rb +60 -20
- data/lib/action_dispatch/middleware/flash.rb +37 -24
- data/lib/action_dispatch/middleware/params_parser.rb +2 -2
- data/lib/action_dispatch/middleware/public_exceptions.rb +12 -3
- data/lib/action_dispatch/middleware/reloader.rb +11 -2
- data/lib/action_dispatch/middleware/remote_ip.rb +40 -54
- data/lib/action_dispatch/middleware/request_id.rb +1 -1
- data/lib/action_dispatch/middleware/session/cache_store.rb +3 -3
- data/lib/action_dispatch/middleware/session/cookie_store.rb +8 -7
- data/lib/action_dispatch/middleware/show_exceptions.rb +6 -2
- data/lib/action_dispatch/middleware/ssl.rb +10 -7
- data/lib/action_dispatch/middleware/static.rb +79 -23
- data/lib/action_dispatch/middleware/templates/rescues/{_request_and_response.erb → _request_and_response.html.erb} +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.erb +21 -19
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +52 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/{diagnostics.erb → diagnostics.html.erb} +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +6 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/{routing_error.erb → routing_error.html.erb} +3 -1
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
- data/lib/action_dispatch/middleware/templates/rescues/{unknown_action.erb → unknown_action.html.erb} +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +120 -64
- data/lib/action_dispatch/railtie.rb +5 -2
- data/lib/action_dispatch/request/session.rb +12 -0
- data/lib/action_dispatch/request/utils.rb +35 -0
- data/lib/action_dispatch/routing/endpoint.rb +10 -0
- data/lib/action_dispatch/routing/inspector.rb +11 -17
- data/lib/action_dispatch/routing/mapper.rb +519 -312
- data/lib/action_dispatch/routing/polymorphic_routes.rb +204 -79
- data/lib/action_dispatch/routing/redirection.rb +51 -26
- data/lib/action_dispatch/routing/route_set.rb +331 -206
- data/lib/action_dispatch/routing/routes_proxy.rb +5 -4
- data/lib/action_dispatch/routing/url_for.rb +19 -5
- data/lib/action_dispatch/routing.rb +9 -6
- data/lib/action_dispatch/testing/assertions/dom.rb +2 -26
- data/lib/action_dispatch/testing/assertions/response.rb +9 -15
- data/lib/action_dispatch/testing/assertions/routing.rb +22 -22
- data/lib/action_dispatch/testing/assertions/selector.rb +2 -429
- data/lib/action_dispatch/testing/assertions/tag.rb +2 -134
- data/lib/action_dispatch/testing/assertions.rb +11 -7
- data/lib/action_dispatch/testing/integration.rb +31 -29
- data/lib/action_dispatch/testing/test_request.rb +1 -1
- data/lib/action_dispatch/testing/test_response.rb +1 -5
- data/lib/action_dispatch.rb +5 -8
- data/lib/action_pack/gem_version.rb +15 -0
- data/lib/action_pack/version.rb +4 -7
- data/lib/action_pack.rb +1 -1
- metadata +77 -159
- data/lib/abstract_controller/layouts.rb +0 -423
- data/lib/abstract_controller/view_paths.rb +0 -96
- data/lib/action_controller/deprecated/integration_test.rb +0 -5
- data/lib/action_controller/deprecated.rb +0 -7
- data/lib/action_controller/metal/responder.rb +0 -287
- data/lib/action_controller/record_identifier.rb +0 -31
- data/lib/action_controller/vendor/html-scanner.rb +0 -5
- data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +0 -24
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +0 -7
- data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +0 -43
- data/lib/action_view/base.rb +0 -201
- data/lib/action_view/buffers.rb +0 -49
- data/lib/action_view/context.rb +0 -36
- data/lib/action_view/dependency_tracker.rb +0 -93
- data/lib/action_view/digestor.rb +0 -113
- data/lib/action_view/flows.rb +0 -76
- data/lib/action_view/helpers/active_model_helper.rb +0 -49
- data/lib/action_view/helpers/asset_tag_helper.rb +0 -320
- data/lib/action_view/helpers/asset_url_helper.rb +0 -355
- data/lib/action_view/helpers/atom_feed_helper.rb +0 -203
- data/lib/action_view/helpers/cache_helper.rb +0 -196
- data/lib/action_view/helpers/capture_helper.rb +0 -216
- data/lib/action_view/helpers/controller_helper.rb +0 -25
- data/lib/action_view/helpers/csrf_helper.rb +0 -30
- data/lib/action_view/helpers/date_helper.rb +0 -1083
- data/lib/action_view/helpers/debug_helper.rb +0 -39
- data/lib/action_view/helpers/form_helper.rb +0 -1880
- data/lib/action_view/helpers/form_options_helper.rb +0 -838
- data/lib/action_view/helpers/form_tag_helper.rb +0 -785
- data/lib/action_view/helpers/javascript_helper.rb +0 -117
- data/lib/action_view/helpers/number_helper.rb +0 -441
- data/lib/action_view/helpers/output_safety_helper.rb +0 -38
- data/lib/action_view/helpers/record_tag_helper.rb +0 -106
- data/lib/action_view/helpers/rendering_helper.rb +0 -90
- data/lib/action_view/helpers/sanitize_helper.rb +0 -256
- data/lib/action_view/helpers/tag_helper.rb +0 -173
- data/lib/action_view/helpers/tags/base.rb +0 -148
- data/lib/action_view/helpers/tags/check_box.rb +0 -64
- data/lib/action_view/helpers/tags/checkable.rb +0 -16
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +0 -44
- data/lib/action_view/helpers/tags/collection_helpers.rb +0 -84
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +0 -36
- data/lib/action_view/helpers/tags/collection_select.rb +0 -28
- data/lib/action_view/helpers/tags/color_field.rb +0 -25
- data/lib/action_view/helpers/tags/date_field.rb +0 -13
- data/lib/action_view/helpers/tags/date_select.rb +0 -72
- data/lib/action_view/helpers/tags/datetime_field.rb +0 -22
- data/lib/action_view/helpers/tags/datetime_local_field.rb +0 -19
- data/lib/action_view/helpers/tags/datetime_select.rb +0 -8
- data/lib/action_view/helpers/tags/email_field.rb +0 -8
- data/lib/action_view/helpers/tags/file_field.rb +0 -8
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +0 -29
- data/lib/action_view/helpers/tags/hidden_field.rb +0 -8
- data/lib/action_view/helpers/tags/label.rb +0 -66
- data/lib/action_view/helpers/tags/month_field.rb +0 -13
- data/lib/action_view/helpers/tags/number_field.rb +0 -18
- data/lib/action_view/helpers/tags/password_field.rb +0 -12
- data/lib/action_view/helpers/tags/radio_button.rb +0 -31
- data/lib/action_view/helpers/tags/range_field.rb +0 -8
- data/lib/action_view/helpers/tags/search_field.rb +0 -24
- data/lib/action_view/helpers/tags/select.rb +0 -40
- data/lib/action_view/helpers/tags/tel_field.rb +0 -8
- data/lib/action_view/helpers/tags/text_area.rb +0 -18
- data/lib/action_view/helpers/tags/text_field.rb +0 -29
- data/lib/action_view/helpers/tags/time_field.rb +0 -13
- data/lib/action_view/helpers/tags/time_select.rb +0 -8
- data/lib/action_view/helpers/tags/time_zone_select.rb +0 -20
- data/lib/action_view/helpers/tags/url_field.rb +0 -8
- data/lib/action_view/helpers/tags/week_field.rb +0 -13
- data/lib/action_view/helpers/tags.rb +0 -39
- data/lib/action_view/helpers/text_helper.rb +0 -443
- data/lib/action_view/helpers/translation_helper.rb +0 -107
- data/lib/action_view/helpers/url_helper.rb +0 -635
- data/lib/action_view/helpers.rb +0 -58
- data/lib/action_view/locale/en.yml +0 -56
- data/lib/action_view/log_subscriber.rb +0 -30
- data/lib/action_view/lookup_context.rb +0 -241
- data/lib/action_view/model_naming.rb +0 -12
- data/lib/action_view/path_set.rb +0 -77
- data/lib/action_view/railtie.rb +0 -43
- data/lib/action_view/record_identifier.rb +0 -84
- data/lib/action_view/renderer/abstract_renderer.rb +0 -47
- data/lib/action_view/renderer/partial_renderer.rb +0 -492
- data/lib/action_view/renderer/renderer.rb +0 -50
- data/lib/action_view/renderer/streaming_template_renderer.rb +0 -103
- data/lib/action_view/renderer/template_renderer.rb +0 -96
- data/lib/action_view/routing_url_for.rb +0 -107
- data/lib/action_view/tasks/dependencies.rake +0 -17
- data/lib/action_view/template/error.rb +0 -138
- data/lib/action_view/template/handlers/builder.rb +0 -26
- data/lib/action_view/template/handlers/erb.rb +0 -146
- data/lib/action_view/template/handlers/raw.rb +0 -11
- data/lib/action_view/template/handlers.rb +0 -53
- data/lib/action_view/template/resolver.rb +0 -326
- data/lib/action_view/template/text.rb +0 -34
- data/lib/action_view/template/types.rb +0 -57
- data/lib/action_view/template.rb +0 -339
- data/lib/action_view/test_case.rb +0 -270
- data/lib/action_view/testing/resolvers.rb +0 -50
- data/lib/action_view/vendor/html-scanner/html/document.rb +0 -68
- data/lib/action_view/vendor/html-scanner/html/node.rb +0 -532
- data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +0 -188
- data/lib/action_view/vendor/html-scanner/html/selector.rb +0 -830
- data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +0 -107
- data/lib/action_view/vendor/html-scanner/html/version.rb +0 -11
- data/lib/action_view/vendor/html-scanner.rb +0 -20
- data/lib/action_view.rb +0 -93
@@ -1,430 +1,3 @@
|
|
1
|
-
require '
|
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 matches.size, count, 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 extracted to the rails-dom-testing gem.")
|
@@ -1,135 +1,3 @@
|
|
1
|
-
require '
|
1
|
+
require 'active_support/deprecation'
|
2
2
|
|
3
|
-
|
4
|
-
module Assertions
|
5
|
-
# Pair of assertions to testing elements in the HTML output of the response.
|
6
|
-
module TagAssertions
|
7
|
-
# Asserts that there is a tag/node/element in the body of the response
|
8
|
-
# that meets all of the given conditions. The +conditions+ parameter must
|
9
|
-
# be a hash of any of the following keys (all are optional):
|
10
|
-
#
|
11
|
-
# * <tt>:tag</tt>: the node type must match the corresponding value
|
12
|
-
# * <tt>:attributes</tt>: a hash. The node's attributes must match the
|
13
|
-
# corresponding values in the hash.
|
14
|
-
# * <tt>:parent</tt>: a hash. The node's parent must match the
|
15
|
-
# corresponding hash.
|
16
|
-
# * <tt>:child</tt>: a hash. At least one of the node's immediate children
|
17
|
-
# must meet the criteria described by the hash.
|
18
|
-
# * <tt>:ancestor</tt>: a hash. At least one of the node's ancestors must
|
19
|
-
# meet the criteria described by the hash.
|
20
|
-
# * <tt>:descendant</tt>: a hash. At least one of the node's descendants
|
21
|
-
# must meet the criteria described by the hash.
|
22
|
-
# * <tt>:sibling</tt>: a hash. At least one of the node's siblings must
|
23
|
-
# meet the criteria described by the hash.
|
24
|
-
# * <tt>:after</tt>: a hash. The node must be after any sibling meeting
|
25
|
-
# the criteria described by the hash, and at least one sibling must match.
|
26
|
-
# * <tt>:before</tt>: a hash. The node must be before any sibling meeting
|
27
|
-
# the criteria described by the hash, and at least one sibling must match.
|
28
|
-
# * <tt>:children</tt>: a hash, for counting children of a node. Accepts
|
29
|
-
# the keys:
|
30
|
-
# * <tt>:count</tt>: either a number or a range which must equal (or
|
31
|
-
# include) the number of children that match.
|
32
|
-
# * <tt>:less_than</tt>: the number of matching children must be less
|
33
|
-
# than this number.
|
34
|
-
# * <tt>:greater_than</tt>: the number of matching children must be
|
35
|
-
# greater than this number.
|
36
|
-
# * <tt>:only</tt>: another hash consisting of the keys to use
|
37
|
-
# to match on the children, and only matching children will be
|
38
|
-
# counted.
|
39
|
-
# * <tt>:content</tt>: the textual content of the node must match the
|
40
|
-
# given value. This will not match HTML tags in the body of a
|
41
|
-
# tag--only text.
|
42
|
-
#
|
43
|
-
# Conditions are matched using the following algorithm:
|
44
|
-
#
|
45
|
-
# * if the condition is a string, it must be a substring of the value.
|
46
|
-
# * if the condition is a regexp, it must match the value.
|
47
|
-
# * if the condition is a number, the value must match number.to_s.
|
48
|
-
# * if the condition is +true+, the value must not be +nil+.
|
49
|
-
# * if the condition is +false+ or +nil+, the value must be +nil+.
|
50
|
-
#
|
51
|
-
# # Assert that there is a "span" tag
|
52
|
-
# assert_tag tag: "span"
|
53
|
-
#
|
54
|
-
# # Assert that there is a "span" tag with id="x"
|
55
|
-
# assert_tag tag: "span", attributes: { id: "x" }
|
56
|
-
#
|
57
|
-
# # Assert that there is a "span" tag using the short-hand
|
58
|
-
# assert_tag :span
|
59
|
-
#
|
60
|
-
# # Assert that there is a "span" tag with id="x" using the short-hand
|
61
|
-
# assert_tag :span, attributes: { id: "x" }
|
62
|
-
#
|
63
|
-
# # Assert that there is a "span" inside of a "div"
|
64
|
-
# assert_tag tag: "span", parent: { tag: "div" }
|
65
|
-
#
|
66
|
-
# # Assert that there is a "span" somewhere inside a table
|
67
|
-
# assert_tag tag: "span", ancestor: { tag: "table" }
|
68
|
-
#
|
69
|
-
# # Assert that there is a "span" with at least one "em" child
|
70
|
-
# assert_tag tag: "span", child: { tag: "em" }
|
71
|
-
#
|
72
|
-
# # Assert that there is a "span" containing a (possibly nested)
|
73
|
-
# # "strong" tag.
|
74
|
-
# assert_tag tag: "span", descendant: { tag: "strong" }
|
75
|
-
#
|
76
|
-
# # Assert that there is a "span" containing between 2 and 4 "em" tags
|
77
|
-
# # as immediate children
|
78
|
-
# assert_tag tag: "span",
|
79
|
-
# children: { count: 2..4, only: { tag: "em" } }
|
80
|
-
#
|
81
|
-
# # Get funky: assert that there is a "div", with an "ul" ancestor
|
82
|
-
# # and an "li" parent (with "class" = "enum"), and containing a
|
83
|
-
# # "span" descendant that contains text matching /hello world/
|
84
|
-
# assert_tag tag: "div",
|
85
|
-
# ancestor: { tag: "ul" },
|
86
|
-
# parent: { tag: "li",
|
87
|
-
# attributes: { class: "enum" } },
|
88
|
-
# descendant: { tag: "span",
|
89
|
-
# child: /hello world/ }
|
90
|
-
#
|
91
|
-
# <b>Please note</b>: +assert_tag+ and +assert_no_tag+ only work
|
92
|
-
# with well-formed XHTML. They recognize a few tags as implicitly self-closing
|
93
|
-
# (like br and hr and such) but will not work correctly with tags
|
94
|
-
# that allow optional closing tags (p, li, td). <em>You must explicitly
|
95
|
-
# close all of your tags to use these assertions.</em>
|
96
|
-
def assert_tag(*opts)
|
97
|
-
opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
|
98
|
-
tag = find_tag(opts)
|
99
|
-
assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
|
100
|
-
end
|
101
|
-
|
102
|
-
# Identical to +assert_tag+, but asserts that a matching tag does _not_
|
103
|
-
# exist. (See +assert_tag+ for a full discussion of the syntax.)
|
104
|
-
#
|
105
|
-
# # Assert that there is not a "div" containing a "p"
|
106
|
-
# assert_no_tag tag: "div", descendant: { tag: "p" }
|
107
|
-
#
|
108
|
-
# # Assert that an unordered list is empty
|
109
|
-
# assert_no_tag tag: "ul", descendant: { tag: "li" }
|
110
|
-
#
|
111
|
-
# # Assert that there is not a "p" tag with between 1 to 3 "img" tags
|
112
|
-
# # as immediate children
|
113
|
-
# assert_no_tag tag: "p",
|
114
|
-
# children: { count: 1..3, only: { tag: "img" } }
|
115
|
-
def assert_no_tag(*opts)
|
116
|
-
opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
|
117
|
-
tag = find_tag(opts)
|
118
|
-
assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
|
119
|
-
end
|
120
|
-
|
121
|
-
def find_tag(conditions)
|
122
|
-
html_document.find(conditions)
|
123
|
-
end
|
124
|
-
|
125
|
-
def find_all_tag(conditions)
|
126
|
-
html_document.find_all(conditions)
|
127
|
-
end
|
128
|
-
|
129
|
-
def html_document
|
130
|
-
xml = @response.content_type =~ /xml$/
|
131
|
-
@html_document ||= HTML::Document.new(@response.body, false, xml)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
3
|
+
ActiveSupport::Deprecation.warn('`ActionDispatch::Assertions::TagAssertions` has been extracted to the rails-dom-testing gem.')
|
@@ -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
|
15
|
-
|
12
|
+
include Rails::Dom::Testing::Assertions
|
13
|
+
|
14
|
+
def html_document
|
15
|
+
@html_document ||= if @response.content_type.to_s =~ /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
|
-
|