halorgium-actionpack 3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGELOG +5179 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +409 -0
  4. data/lib/abstract_controller.rb +16 -0
  5. data/lib/abstract_controller/base.rb +158 -0
  6. data/lib/abstract_controller/callbacks.rb +113 -0
  7. data/lib/abstract_controller/exceptions.rb +12 -0
  8. data/lib/abstract_controller/helpers.rb +151 -0
  9. data/lib/abstract_controller/layouts.rb +250 -0
  10. data/lib/abstract_controller/localized_cache.rb +49 -0
  11. data/lib/abstract_controller/logger.rb +61 -0
  12. data/lib/abstract_controller/rendering_controller.rb +188 -0
  13. data/lib/action_controller.rb +72 -0
  14. data/lib/action_controller/base.rb +168 -0
  15. data/lib/action_controller/caching.rb +80 -0
  16. data/lib/action_controller/caching/actions.rb +163 -0
  17. data/lib/action_controller/caching/fragments.rb +116 -0
  18. data/lib/action_controller/caching/pages.rb +154 -0
  19. data/lib/action_controller/caching/sweeping.rb +97 -0
  20. data/lib/action_controller/deprecated.rb +4 -0
  21. data/lib/action_controller/deprecated/integration_test.rb +2 -0
  22. data/lib/action_controller/deprecated/performance_test.rb +1 -0
  23. data/lib/action_controller/dispatch/dispatcher.rb +57 -0
  24. data/lib/action_controller/metal.rb +129 -0
  25. data/lib/action_controller/metal/benchmarking.rb +73 -0
  26. data/lib/action_controller/metal/compatibility.rb +145 -0
  27. data/lib/action_controller/metal/conditional_get.rb +86 -0
  28. data/lib/action_controller/metal/configuration.rb +28 -0
  29. data/lib/action_controller/metal/cookies.rb +105 -0
  30. data/lib/action_controller/metal/exceptions.rb +55 -0
  31. data/lib/action_controller/metal/filter_parameter_logging.rb +77 -0
  32. data/lib/action_controller/metal/flash.rb +162 -0
  33. data/lib/action_controller/metal/head.rb +27 -0
  34. data/lib/action_controller/metal/helpers.rb +115 -0
  35. data/lib/action_controller/metal/hide_actions.rb +47 -0
  36. data/lib/action_controller/metal/http_authentication.rb +312 -0
  37. data/lib/action_controller/metal/layouts.rb +171 -0
  38. data/lib/action_controller/metal/mime_responds.rb +317 -0
  39. data/lib/action_controller/metal/rack_convenience.rb +27 -0
  40. data/lib/action_controller/metal/redirector.rb +22 -0
  41. data/lib/action_controller/metal/render_options.rb +103 -0
  42. data/lib/action_controller/metal/rendering_controller.rb +57 -0
  43. data/lib/action_controller/metal/request_forgery_protection.rb +108 -0
  44. data/lib/action_controller/metal/rescuable.rb +13 -0
  45. data/lib/action_controller/metal/responder.rb +200 -0
  46. data/lib/action_controller/metal/session.rb +15 -0
  47. data/lib/action_controller/metal/session_management.rb +45 -0
  48. data/lib/action_controller/metal/streaming.rb +188 -0
  49. data/lib/action_controller/metal/testing.rb +39 -0
  50. data/lib/action_controller/metal/url_for.rb +41 -0
  51. data/lib/action_controller/metal/verification.rb +130 -0
  52. data/lib/action_controller/middleware.rb +38 -0
  53. data/lib/action_controller/notifications.rb +10 -0
  54. data/lib/action_controller/polymorphic_routes.rb +183 -0
  55. data/lib/action_controller/record_identifier.rb +91 -0
  56. data/lib/action_controller/testing/process.rb +111 -0
  57. data/lib/action_controller/testing/test_case.rb +345 -0
  58. data/lib/action_controller/translation.rb +13 -0
  59. data/lib/action_controller/url_rewriter.rb +204 -0
  60. data/lib/action_controller/vendor/html-scanner.rb +16 -0
  61. data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
  62. data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
  63. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +176 -0
  64. data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
  65. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
  66. data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
  67. data/lib/action_dispatch.rb +70 -0
  68. data/lib/action_dispatch/http/headers.rb +33 -0
  69. data/lib/action_dispatch/http/mime_type.rb +231 -0
  70. data/lib/action_dispatch/http/mime_types.rb +23 -0
  71. data/lib/action_dispatch/http/request.rb +539 -0
  72. data/lib/action_dispatch/http/response.rb +290 -0
  73. data/lib/action_dispatch/http/status_codes.rb +42 -0
  74. data/lib/action_dispatch/http/utils.rb +20 -0
  75. data/lib/action_dispatch/middleware/callbacks.rb +50 -0
  76. data/lib/action_dispatch/middleware/params_parser.rb +79 -0
  77. data/lib/action_dispatch/middleware/rescue.rb +26 -0
  78. data/lib/action_dispatch/middleware/session/abstract_store.rb +208 -0
  79. data/lib/action_dispatch/middleware/session/cookie_store.rb +235 -0
  80. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +47 -0
  81. data/lib/action_dispatch/middleware/show_exceptions.rb +143 -0
  82. data/lib/action_dispatch/middleware/stack.rb +116 -0
  83. data/lib/action_dispatch/middleware/static.rb +44 -0
  84. data/lib/action_dispatch/middleware/string_coercion.rb +29 -0
  85. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +24 -0
  86. data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +26 -0
  87. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +10 -0
  88. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +29 -0
  89. data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +2 -0
  90. data/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +10 -0
  91. data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +21 -0
  92. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb +2 -0
  93. data/lib/action_dispatch/routing.rb +381 -0
  94. data/lib/action_dispatch/routing/deprecated_mapper.rb +878 -0
  95. data/lib/action_dispatch/routing/mapper.rb +327 -0
  96. data/lib/action_dispatch/routing/route.rb +49 -0
  97. data/lib/action_dispatch/routing/route_set.rb +497 -0
  98. data/lib/action_dispatch/testing/assertions.rb +8 -0
  99. data/lib/action_dispatch/testing/assertions/dom.rb +35 -0
  100. data/lib/action_dispatch/testing/assertions/model.rb +19 -0
  101. data/lib/action_dispatch/testing/assertions/response.rb +145 -0
  102. data/lib/action_dispatch/testing/assertions/routing.rb +144 -0
  103. data/lib/action_dispatch/testing/assertions/selector.rb +639 -0
  104. data/lib/action_dispatch/testing/assertions/tag.rb +123 -0
  105. data/lib/action_dispatch/testing/integration.rb +504 -0
  106. data/lib/action_dispatch/testing/performance_test.rb +15 -0
  107. data/lib/action_dispatch/testing/test_request.rb +83 -0
  108. data/lib/action_dispatch/testing/test_response.rb +131 -0
  109. data/lib/action_pack.rb +24 -0
  110. data/lib/action_pack/version.rb +9 -0
  111. data/lib/action_view.rb +58 -0
  112. data/lib/action_view/base.rb +308 -0
  113. data/lib/action_view/context.rb +44 -0
  114. data/lib/action_view/erb/util.rb +48 -0
  115. data/lib/action_view/helpers.rb +62 -0
  116. data/lib/action_view/helpers/active_model_helper.rb +306 -0
  117. data/lib/action_view/helpers/ajax_helper.rb +68 -0
  118. data/lib/action_view/helpers/asset_tag_helper.rb +830 -0
  119. data/lib/action_view/helpers/atom_feed_helper.rb +198 -0
  120. data/lib/action_view/helpers/cache_helper.rb +39 -0
  121. data/lib/action_view/helpers/capture_helper.rb +168 -0
  122. data/lib/action_view/helpers/date_helper.rb +988 -0
  123. data/lib/action_view/helpers/debug_helper.rb +38 -0
  124. data/lib/action_view/helpers/form_helper.rb +1102 -0
  125. data/lib/action_view/helpers/form_options_helper.rb +600 -0
  126. data/lib/action_view/helpers/form_tag_helper.rb +495 -0
  127. data/lib/action_view/helpers/javascript_helper.rb +208 -0
  128. data/lib/action_view/helpers/number_helper.rb +311 -0
  129. data/lib/action_view/helpers/prototype_helper.rb +1309 -0
  130. data/lib/action_view/helpers/raw_output_helper.rb +9 -0
  131. data/lib/action_view/helpers/record_identification_helper.rb +20 -0
  132. data/lib/action_view/helpers/record_tag_helper.rb +58 -0
  133. data/lib/action_view/helpers/sanitize_helper.rb +259 -0
  134. data/lib/action_view/helpers/scriptaculous_helper.rb +226 -0
  135. data/lib/action_view/helpers/tag_helper.rb +151 -0
  136. data/lib/action_view/helpers/text_helper.rb +594 -0
  137. data/lib/action_view/helpers/translation_helper.rb +39 -0
  138. data/lib/action_view/helpers/url_helper.rb +639 -0
  139. data/lib/action_view/locale/en.yml +117 -0
  140. data/lib/action_view/paths.rb +80 -0
  141. data/lib/action_view/render/partials.rb +342 -0
  142. data/lib/action_view/render/rendering.rb +134 -0
  143. data/lib/action_view/safe_buffer.rb +28 -0
  144. data/lib/action_view/template/error.rb +101 -0
  145. data/lib/action_view/template/handler.rb +36 -0
  146. data/lib/action_view/template/handlers.rb +52 -0
  147. data/lib/action_view/template/handlers/builder.rb +17 -0
  148. data/lib/action_view/template/handlers/erb.rb +53 -0
  149. data/lib/action_view/template/handlers/rjs.rb +18 -0
  150. data/lib/action_view/template/resolver.rb +165 -0
  151. data/lib/action_view/template/template.rb +131 -0
  152. data/lib/action_view/template/text.rb +38 -0
  153. data/lib/action_view/test_case.rb +163 -0
  154. metadata +236 -0
@@ -0,0 +1,8 @@
1
+ module ActionDispatch
2
+ module Assertions
3
+ %w(response selector tag dom routing model).each do |kind|
4
+ require "action_dispatch/testing/assertions/#{kind}"
5
+ include const_get("#{kind.camelize}Assertions")
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ module ActionDispatch
2
+ module Assertions
3
+ module DomAssertions
4
+ # Test two HTML strings for equivalency (e.g., identical up to reordering of attributes)
5
+ #
6
+ # ==== Examples
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
+ #
11
+ def assert_dom_equal(expected, actual, message = "")
12
+ expected_dom = HTML::Document.new(expected).root
13
+ actual_dom = HTML::Document.new(actual).root
14
+ full_message = build_message(message, "<?> expected to be == to\n<?>.", expected_dom.to_s, actual_dom.to_s)
15
+
16
+ assert_block(full_message) { expected_dom == actual_dom }
17
+ end
18
+
19
+ # The negated form of +assert_dom_equivalent+.
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # # assert that the referenced method does not generate the specified HTML string
24
+ # assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
25
+ #
26
+ def assert_dom_not_equal(expected, actual, message = "")
27
+ expected_dom = HTML::Document.new(expected).root
28
+ actual_dom = HTML::Document.new(actual).root
29
+ full_message = build_message(message, "<?> expected to be != to\n<?>.", expected_dom.to_s, actual_dom.to_s)
30
+
31
+ assert_block(full_message) { expected_dom != actual_dom }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,19 @@
1
+ module ActionDispatch
2
+ module Assertions
3
+ module ModelAssertions
4
+ # Ensures that the passed record is valid by Active Record standards and
5
+ # returns any error messages if it is not.
6
+ #
7
+ # ==== Examples
8
+ #
9
+ # # assert that a newly created record is valid
10
+ # model = Model.new
11
+ # assert_valid(model)
12
+ #
13
+ def assert_valid(record)
14
+ ::ActiveSupport::Deprecation.warn("assert_valid is deprecated. Use assert record.valid? instead", caller)
15
+ assert record.valid?, record.errors.full_messages.join("\n")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,145 @@
1
+ module ActionDispatch
2
+ module Assertions
3
+ # A small suite of assertions that test responses from Rails applications.
4
+ module ResponseAssertions
5
+ # Asserts that the response is one of the following types:
6
+ #
7
+ # * <tt>:success</tt> - Status code was 200
8
+ # * <tt>:redirect</tt> - Status code was in the 300-399 range
9
+ # * <tt>:missing</tt> - Status code was 404
10
+ # * <tt>:error</tt> - Status code was in the 500-599 range
11
+ #
12
+ # You can also pass an explicit status number like assert_response(501)
13
+ # or its symbolic equivalent assert_response(:not_implemented).
14
+ # See ActionDispatch::StatusCodes for a full list.
15
+ #
16
+ # ==== Examples
17
+ #
18
+ # # assert that the response was a redirection
19
+ # assert_response :redirect
20
+ #
21
+ # # assert that the response code was status code 401 (unauthorized)
22
+ # assert_response 401
23
+ #
24
+ def assert_response(type, message = nil)
25
+ validate_request!
26
+
27
+ if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
28
+ assert_block("") { true } # to count the assertion
29
+ elsif type.is_a?(Fixnum) && @response.response_code == type
30
+ assert_block("") { true } # to count the assertion
31
+ elsif type.is_a?(Symbol) && @response.response_code == ActionDispatch::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
32
+ assert_block("") { true } # to count the assertion
33
+ else
34
+ assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
35
+ end
36
+ end
37
+
38
+ # Assert that the redirection options passed in match those of the redirect called in the latest action.
39
+ # This match can be partial, such that assert_redirected_to(:controller => "weblog") will also
40
+ # match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.
41
+ #
42
+ # ==== Examples
43
+ #
44
+ # # assert that the redirection was to the "index" action on the WeblogController
45
+ # assert_redirected_to :controller => "weblog", :action => "index"
46
+ #
47
+ # # assert that the redirection was to the named route login_url
48
+ # assert_redirected_to login_url
49
+ #
50
+ # # assert that the redirection was to the url for @customer
51
+ # assert_redirected_to @customer
52
+ #
53
+ def assert_redirected_to(options = {}, message=nil)
54
+ validate_request!
55
+
56
+ assert_response(:redirect, message)
57
+ return true if options == @response.location
58
+
59
+ redirected_to_after_normalisation = normalize_argument_to_redirection(@response.location)
60
+ options_after_normalisation = normalize_argument_to_redirection(options)
61
+
62
+ if redirected_to_after_normalisation != options_after_normalisation
63
+ flunk "Expected response to be a redirect to <#{options_after_normalisation}> but was a redirect to <#{redirected_to_after_normalisation}>"
64
+ end
65
+ end
66
+
67
+ # Asserts that the request was rendered with the appropriate template file or partials
68
+ #
69
+ # ==== Examples
70
+ #
71
+ # # assert that the "new" view template was rendered
72
+ # assert_template "new"
73
+ #
74
+ # # assert that the "_customer" partial was rendered twice
75
+ # assert_template :partial => '_customer', :count => 2
76
+ #
77
+ # # assert that no partials were rendered
78
+ # assert_template :partial => false
79
+ #
80
+ def assert_template(options = {}, message = nil)
81
+ validate_request!
82
+
83
+ case options
84
+ when NilClass, String
85
+ rendered = (@controller.template.rendered[:template] || []).map { |t| t.identifier }
86
+ msg = build_message(message,
87
+ "expecting <?> but rendering with <?>",
88
+ options, rendered.join(', '))
89
+ assert_block(msg) do
90
+ if options.nil?
91
+ @controller.template.rendered[:template].blank?
92
+ else
93
+ rendered.any? { |t| t.match(options) }
94
+ end
95
+ end
96
+ when Hash
97
+ if expected_partial = options[:partial]
98
+ partials = @controller.template.rendered[:partials]
99
+ if expected_count = options[:count]
100
+ found = partials.detect { |p, _| p.identifier.match(expected_partial) }
101
+ actual_count = found.nil? ? 0 : found.second
102
+ msg = build_message(message,
103
+ "expecting ? to be rendered ? time(s) but rendered ? time(s)",
104
+ expected_partial, expected_count, actual_count)
105
+ assert(actual_count == expected_count.to_i, msg)
106
+ else
107
+ msg = build_message(message,
108
+ "expecting partial <?> but action rendered <?>",
109
+ options[:partial], partials.keys)
110
+ assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg)
111
+ end
112
+ else
113
+ assert @controller.template.rendered[:partials].empty?,
114
+ "Expected no partials to be rendered"
115
+ end
116
+ end
117
+ end
118
+
119
+ private
120
+ # Proxy to to_param if the object will respond to it.
121
+ def parameterize(value)
122
+ value.respond_to?(:to_param) ? value.to_param : value
123
+ end
124
+
125
+ def normalize_argument_to_redirection(fragment)
126
+ after_routing = @controller.url_for(fragment)
127
+ if after_routing =~ %r{^\w+://.*}
128
+ after_routing
129
+ else
130
+ # FIXME - this should probably get removed.
131
+ if after_routing.first != '/'
132
+ after_routing = '/' + after_routing
133
+ end
134
+ @request.protocol + @request.host_with_port + after_routing
135
+ end
136
+ end
137
+
138
+ def validate_request!
139
+ unless @request.is_a?(ActionDispatch::Request)
140
+ raise ArgumentError, "@request must be an ActionDispatch::Request"
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,144 @@
1
+ require 'active_support/core_ext/hash/diff'
2
+
3
+ module ActionDispatch
4
+ module Assertions
5
+ # Suite of assertions to test routes generated by Rails and the handling of requests made to them.
6
+ module RoutingAssertions
7
+ # Asserts that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash)
8
+ # match +path+. Basically, it asserts that Rails recognizes the route given by +expected_options+.
9
+ #
10
+ # Pass a hash in the second argument (+path+) to specify the request method. This is useful for routes
11
+ # requiring a specific HTTP method. The hash should contain a :path with the incoming request path
12
+ # and a :method containing the required HTTP verb.
13
+ #
14
+ # # assert that POSTing to /items will call the create action on ItemsController
15
+ # assert_recognizes {:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post}
16
+ #
17
+ # You can also pass in +extras+ with a hash containing URL parameters that would normally be in the query string. This can be used
18
+ # to assert that values in the query string string will end up in the params hash correctly. To test query strings you must use the
19
+ # extras argument, appending the query string on the path directly will not work. For example:
20
+ #
21
+ # # assert that a path of '/items/list/1?view=print' returns the correct options
22
+ # assert_recognizes {:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" }
23
+ #
24
+ # The +message+ parameter allows you to pass in an error message that is displayed upon failure.
25
+ #
26
+ # ==== Examples
27
+ # # Check the default route (i.e., the index action)
28
+ # assert_recognizes {:controller => 'items', :action => 'index'}, 'items'
29
+ #
30
+ # # Test a specific action
31
+ # assert_recognizes {:controller => 'items', :action => 'list'}, 'items/list'
32
+ #
33
+ # # Test an action with a parameter
34
+ # assert_recognizes {:controller => 'items', :action => 'destroy', :id => '1'}, 'items/destroy/1'
35
+ #
36
+ # # Test a custom route
37
+ # assert_recognizes {:controller => 'items', :action => 'show', :id => '1'}, 'view/item1'
38
+ #
39
+ # # Check a Simply RESTful generated route
40
+ # assert_recognizes list_items_url, 'items/list'
41
+ def assert_recognizes(expected_options, path, extras={}, message=nil)
42
+ if path.is_a? Hash
43
+ request_method = path[:method]
44
+ path = path[:path]
45
+ else
46
+ request_method = nil
47
+ end
48
+
49
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
50
+ request = recognized_request_for(path, request_method)
51
+
52
+ expected_options = expected_options.clone
53
+ extras.each_key { |key| expected_options.delete key } unless extras.nil?
54
+
55
+ expected_options.stringify_keys!
56
+ routing_diff = expected_options.diff(request.path_parameters)
57
+ msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
58
+ request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
59
+ assert_block(msg) { request.path_parameters == expected_options }
60
+ end
61
+
62
+ # Asserts that the provided options can be used to generate the provided path. This is the inverse of +assert_recognizes+.
63
+ # The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in
64
+ # a query string. The +message+ parameter allows you to specify a custom error message for assertion failures.
65
+ #
66
+ # The +defaults+ parameter is unused.
67
+ #
68
+ # ==== Examples
69
+ # # Asserts that the default action is generated for a route with no action
70
+ # assert_generates "/items", :controller => "items", :action => "index"
71
+ #
72
+ # # Tests that the list action is properly routed
73
+ # assert_generates "/items/list", :controller => "items", :action => "list"
74
+ #
75
+ # # Tests the generation of a route with a parameter
76
+ # assert_generates "/items/list/1", { :controller => "items", :action => "list", :id => "1" }
77
+ #
78
+ # # Asserts that the generated route gives us our custom route
79
+ # assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
80
+ def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
81
+ expected_path = "/#{expected_path}" unless expected_path[0] == ?/
82
+ # Load routes.rb if it hasn't been loaded.
83
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
84
+
85
+ generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, defaults)
86
+ found_extras = options.reject {|k, v| ! extra_keys.include? k}
87
+
88
+ msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
89
+ assert_block(msg) { found_extras == extras }
90
+
91
+ msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
92
+ expected_path)
93
+ assert_block(msg) { expected_path == generated_path }
94
+ end
95
+
96
+ # Asserts that path and options match both ways; in other words, it verifies that <tt>path</tt> generates
97
+ # <tt>options</tt> and then that <tt>options</tt> generates <tt>path</tt>. This essentially combines +assert_recognizes+
98
+ # and +assert_generates+ into one step.
99
+ #
100
+ # The +extras+ hash allows you to specify options that would normally be provided as a query string to the action. The
101
+ # +message+ parameter allows you to specify a custom error message to display upon failure.
102
+ #
103
+ # ==== Examples
104
+ # # Assert a basic route: a controller with the default action (index)
105
+ # assert_routing '/home', :controller => 'home', :action => 'index'
106
+ #
107
+ # # Test a route generated with a specific controller, action, and parameter (id)
108
+ # assert_routing '/entries/show/23', :controller => 'entries', :action => 'show', id => 23
109
+ #
110
+ # # Assert a basic route (controller + default action), with an error message if it fails
111
+ # assert_routing '/store', { :controller => 'store', :action => 'index' }, {}, {}, 'Route for store index not generated properly'
112
+ #
113
+ # # Tests a route, providing a defaults hash
114
+ # assert_routing 'controller/action/9', {:id => "9", :item => "square"}, {:controller => "controller", :action => "action"}, {}, {:item => "square"}
115
+ #
116
+ # # Tests a route with a HTTP method
117
+ # assert_routing { :method => 'put', :path => '/product/321' }, { :controller => "product", :action => "update", :id => "321" }
118
+ def assert_routing(path, options, defaults={}, extras={}, message=nil)
119
+ assert_recognizes(options, path, extras, message)
120
+
121
+ controller, default_controller = options[:controller], defaults[:controller]
122
+ if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
123
+ options[:controller] = "/#{controller}"
124
+ end
125
+
126
+ assert_generates(path.is_a?(Hash) ? path[:path] : path, options, defaults, extras, message)
127
+ end
128
+
129
+ private
130
+ # Recognizes the route for a given path.
131
+ def recognized_request_for(path, request_method = nil)
132
+ path = "/#{path}" unless path.first == '/'
133
+
134
+ # Assume given controller
135
+ request = ActionController::TestRequest.new
136
+ request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
137
+ request.path = path
138
+
139
+ ActionController::Routing::Routes.recognize(request)
140
+ request
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,639 @@
1
+ #--
2
+ # Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
3
+ # Under MIT and/or CC By license.
4
+ #++
5
+
6
+ module ActionDispatch
7
+ module Assertions
8
+ unless const_defined?(:NO_STRIP)
9
+ NO_STRIP = %w{pre script style textarea}
10
+ end
11
+
12
+ # Adds the +assert_select+ method for use in Rails functional
13
+ # test cases, which can be used to make assertions on the response HTML of a controller
14
+ # action. You can also call +assert_select+ within another +assert_select+ to
15
+ # make assertions on elements selected by the enclosing assertion.
16
+ #
17
+ # Use +css_select+ to select elements without making an assertions, either
18
+ # from the response HTML or elements selected by the enclosing assertion.
19
+ #
20
+ # In addition to HTML responses, you can make the following assertions:
21
+ # * +assert_select_rjs+ - Assertions on HTML content of RJS update and insertion operations.
22
+ # * +assert_select_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
23
+ # * +assert_select_email+ - Assertions on the HTML body of an e-mail.
24
+ #
25
+ # Also see HTML::Selector to learn how to use selectors.
26
+ module SelectorAssertions
27
+ # :call-seq:
28
+ # css_select(selector) => array
29
+ # css_select(element, selector) => array
30
+ #
31
+ # Select and return all matching elements.
32
+ #
33
+ # If called with a single argument, uses that argument as a selector
34
+ # to match all elements of the current page. Returns an empty array
35
+ # if no match is found.
36
+ #
37
+ # If called with two arguments, uses the first argument as the base
38
+ # element and the second argument as the selector. Attempts to match the
39
+ # base element and any of its children. Returns an empty array if no
40
+ # match is found.
41
+ #
42
+ # The selector may be a CSS selector expression (String), an expression
43
+ # with substitution values (Array) or an HTML::Selector object.
44
+ #
45
+ # ==== Examples
46
+ # # Selects all div tags
47
+ # divs = css_select("div")
48
+ #
49
+ # # Selects all paragraph tags and does something interesting
50
+ # pars = css_select("p")
51
+ # pars.each do |par|
52
+ # # Do something fun with paragraphs here...
53
+ # end
54
+ #
55
+ # # Selects all list items in unordered lists
56
+ # items = css_select("ul>li")
57
+ #
58
+ # # Selects all form tags and then all inputs inside the form
59
+ # forms = css_select("form")
60
+ # forms.each do |form|
61
+ # inputs = css_select(form, "input")
62
+ # ...
63
+ # end
64
+ #
65
+ def css_select(*args)
66
+ # See assert_select to understand what's going on here.
67
+ arg = args.shift
68
+
69
+ if arg.is_a?(HTML::Node)
70
+ root = arg
71
+ arg = args.shift
72
+ elsif arg == nil
73
+ 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?"
74
+ elsif @selected
75
+ matches = []
76
+
77
+ @selected.each do |selected|
78
+ subset = css_select(selected, HTML::Selector.new(arg.dup, args.dup))
79
+ subset.each do |match|
80
+ matches << match unless matches.any? { |m| m.equal?(match) }
81
+ end
82
+ end
83
+
84
+ return matches
85
+ else
86
+ root = response_from_page_or_rjs
87
+ end
88
+
89
+ case arg
90
+ when String
91
+ selector = HTML::Selector.new(arg, args)
92
+ when Array
93
+ selector = HTML::Selector.new(*arg)
94
+ when HTML::Selector
95
+ selector = arg
96
+ else raise ArgumentError, "Expecting a selector as the first argument"
97
+ end
98
+
99
+ selector.select(root)
100
+ end
101
+
102
+ # :call-seq:
103
+ # assert_select(selector, equality?, message?)
104
+ # assert_select(element, selector, equality?, message?)
105
+ #
106
+ # An assertion that selects elements and makes one or more equality tests.
107
+ #
108
+ # If the first argument is an element, selects all matching elements
109
+ # starting from (and including) that element and all its children in
110
+ # depth-first order.
111
+ #
112
+ # If no element if specified, calling +assert_select+ selects from the
113
+ # response HTML unless +assert_select+ is called from within an +assert_select+ block.
114
+ #
115
+ # When called with a block +assert_select+ passes an array of selected elements
116
+ # to the block. Calling +assert_select+ from the block, with no element specified,
117
+ # runs the assertion on the complete set of elements selected by the enclosing assertion.
118
+ # Alternatively the array may be iterated through so that +assert_select+ can be called
119
+ # separately for each element.
120
+ #
121
+ #
122
+ # ==== Example
123
+ # If the response contains two ordered lists, each with four list elements then:
124
+ # assert_select "ol" do |elements|
125
+ # elements.each do |element|
126
+ # assert_select element, "li", 4
127
+ # end
128
+ # end
129
+ #
130
+ # will pass, as will:
131
+ # assert_select "ol" do
132
+ # assert_select "li", 8
133
+ # end
134
+ #
135
+ # The selector may be a CSS selector expression (String), an expression
136
+ # with substitution values, or an HTML::Selector object.
137
+ #
138
+ # === Equality Tests
139
+ #
140
+ # The equality test may be one of the following:
141
+ # * <tt>true</tt> - Assertion is true if at least one element selected.
142
+ # * <tt>false</tt> - Assertion is true if no element selected.
143
+ # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
144
+ # one element matches the string or regular expression.
145
+ # * <tt>Integer</tt> - Assertion is true if exactly that number of
146
+ # elements are selected.
147
+ # * <tt>Range</tt> - Assertion is true if the number of selected
148
+ # elements fit the range.
149
+ # If no equality test specified, the assertion is true if at least one
150
+ # element selected.
151
+ #
152
+ # To perform more than one equality tests, use a hash with the following keys:
153
+ # * <tt>:text</tt> - Narrow the selection to elements that have this text
154
+ # value (string or regexp).
155
+ # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
156
+ # content (string or regexp).
157
+ # * <tt>:count</tt> - Assertion is true if the number of selected elements
158
+ # is equal to this value.
159
+ # * <tt>:minimum</tt> - Assertion is true if the number of selected
160
+ # elements is at least this value.
161
+ # * <tt>:maximum</tt> - Assertion is true if the number of selected
162
+ # elements is at most this value.
163
+ #
164
+ # If the method is called with a block, once all equality tests are
165
+ # evaluated the block is called with an array of all matched elements.
166
+ #
167
+ # ==== Examples
168
+ #
169
+ # # At least one form element
170
+ # assert_select "form"
171
+ #
172
+ # # Form element includes four input fields
173
+ # assert_select "form input", 4
174
+ #
175
+ # # Page title is "Welcome"
176
+ # assert_select "title", "Welcome"
177
+ #
178
+ # # Page title is "Welcome" and there is only one title element
179
+ # assert_select "title", {:count=>1, :text=>"Welcome"},
180
+ # "Wrong title or more than one title element"
181
+ #
182
+ # # Page contains no forms
183
+ # assert_select "form", false, "This page must contain no forms"
184
+ #
185
+ # # Test the content and style
186
+ # assert_select "body div.header ul.menu"
187
+ #
188
+ # # Use substitution values
189
+ # assert_select "ol>li#?", /item-\d+/
190
+ #
191
+ # # All input fields in the form have a name
192
+ # assert_select "form input" do
193
+ # assert_select "[name=?]", /.+/ # Not empty
194
+ # end
195
+ def assert_select(*args, &block)
196
+ # Start with optional element followed by mandatory selector.
197
+ arg = args.shift
198
+
199
+ if arg.is_a?(HTML::Node)
200
+ # First argument is a node (tag or text, but also HTML root),
201
+ # so we know what we're selecting from.
202
+ root = arg
203
+ arg = args.shift
204
+ elsif arg == nil
205
+ # This usually happens when passing a node/element that
206
+ # happens to be nil.
207
+ 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?"
208
+ elsif @selected
209
+ root = HTML::Node.new(nil)
210
+ root.children.concat @selected
211
+ else
212
+ # Otherwise just operate on the response document.
213
+ root = response_from_page_or_rjs
214
+ end
215
+
216
+ # First or second argument is the selector: string and we pass
217
+ # all remaining arguments. Array and we pass the argument. Also
218
+ # accepts selector itself.
219
+ case arg
220
+ when String
221
+ selector = HTML::Selector.new(arg, args)
222
+ when Array
223
+ selector = HTML::Selector.new(*arg)
224
+ when HTML::Selector
225
+ selector = arg
226
+ else raise ArgumentError, "Expecting a selector as the first argument"
227
+ end
228
+
229
+ # Next argument is used for equality tests.
230
+ equals = {}
231
+ case arg = args.shift
232
+ when Hash
233
+ equals = arg
234
+ when String, Regexp
235
+ equals[:text] = arg
236
+ when Integer
237
+ equals[:count] = arg
238
+ when Range
239
+ equals[:minimum] = arg.begin
240
+ equals[:maximum] = arg.end
241
+ when FalseClass
242
+ equals[:count] = 0
243
+ when NilClass, TrueClass
244
+ equals[:minimum] = 1
245
+ else raise ArgumentError, "I don't understand what you're trying to match"
246
+ end
247
+
248
+ # By default we're looking for at least one match.
249
+ if equals[:count]
250
+ equals[:minimum] = equals[:maximum] = equals[:count]
251
+ else
252
+ equals[:minimum] = 1 unless equals[:minimum]
253
+ end
254
+
255
+ # Last argument is the message we use if the assertion fails.
256
+ message = args.shift
257
+ #- message = "No match made with selector #{selector.inspect}" unless message
258
+ if args.shift
259
+ raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type"
260
+ end
261
+
262
+ matches = selector.select(root)
263
+ # If text/html, narrow down to those elements that match it.
264
+ content_mismatch = nil
265
+ if match_with = equals[:text]
266
+ matches.delete_if do |match|
267
+ text = ""
268
+ text.force_encoding(match_with.encoding) if text.respond_to?(:force_encoding)
269
+ stack = match.children.reverse
270
+ while node = stack.pop
271
+ if node.tag?
272
+ stack.concat node.children.reverse
273
+ else
274
+ content = node.content
275
+ content.force_encoding(match_with.encoding) if content.respond_to?(:force_encoding)
276
+ text << content
277
+ end
278
+ end
279
+ text.strip! unless NO_STRIP.include?(match.name)
280
+ unless match_with.is_a?(Regexp) ? (text =~ match_with) : (text == match_with.to_s)
281
+ content_mismatch ||= build_message(message, "<?> expected but was\n<?>.", match_with, text)
282
+ true
283
+ end
284
+ end
285
+ elsif match_with = equals[:html]
286
+ matches.delete_if do |match|
287
+ html = match.children.map(&:to_s).join
288
+ html.strip! unless NO_STRIP.include?(match.name)
289
+ unless match_with.is_a?(Regexp) ? (html =~ match_with) : (html == match_with.to_s)
290
+ content_mismatch ||= build_message(message, "<?> expected but was\n<?>.", match_with, html)
291
+ true
292
+ end
293
+ end
294
+ end
295
+ # Expecting foo found bar element only if found zero, not if
296
+ # found one but expecting two.
297
+ message ||= content_mismatch if matches.empty?
298
+ # Test minimum/maximum occurrence.
299
+ min, max = equals[:minimum], equals[:maximum]
300
+ message = message || %(Expected #{count_description(min, max)} matching "#{selector.to_s}", found #{matches.size}.)
301
+ assert matches.size >= min, message if min
302
+ assert matches.size <= max, message if max
303
+
304
+ # If a block is given call that block. Set @selected to allow
305
+ # nested assert_select, which can be nested several levels deep.
306
+ if block_given? && !matches.empty?
307
+ begin
308
+ in_scope, @selected = @selected, matches
309
+ yield matches
310
+ ensure
311
+ @selected = in_scope
312
+ end
313
+ end
314
+
315
+ # Returns all matches elements.
316
+ matches
317
+ end
318
+
319
+ def count_description(min, max) #:nodoc:
320
+ pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')}
321
+
322
+ if min && max && (max != min)
323
+ "between #{min} and #{max} elements"
324
+ elsif min && !(min == 1 && max == 1)
325
+ "at least #{min} #{pluralize['element', min]}"
326
+ elsif max
327
+ "at most #{max} #{pluralize['element', max]}"
328
+ end
329
+ end
330
+
331
+ # :call-seq:
332
+ # assert_select_rjs(id?) { |elements| ... }
333
+ # assert_select_rjs(statement, id?) { |elements| ... }
334
+ # assert_select_rjs(:insert, position, id?) { |elements| ... }
335
+ #
336
+ # Selects content from the RJS response.
337
+ #
338
+ # === Narrowing down
339
+ #
340
+ # With no arguments, asserts that one or more elements are updated or
341
+ # inserted by RJS statements.
342
+ #
343
+ # Use the +id+ argument to narrow down the assertion to only statements
344
+ # that update or insert an element with that identifier.
345
+ #
346
+ # Use the first argument to narrow down assertions to only statements
347
+ # of that type. Possible values are <tt>:replace</tt>, <tt>:replace_html</tt>,
348
+ # <tt>:show</tt>, <tt>:hide</tt>, <tt>:toggle</tt>, <tt>:remove</tta>,
349
+ # <tt>:insert_html</tt> and <tt>:redirect</tt>.
350
+ #
351
+ # Use the argument <tt>:insert</tt> followed by an insertion position to narrow
352
+ # down the assertion to only statements that insert elements in that
353
+ # position. Possible values are <tt>:top</tt>, <tt>:bottom</tt>, <tt>:before</tt>
354
+ # and <tt>:after</tt>.
355
+ #
356
+ # Use the argument <tt>:redirect</tt> follwed by a path to check that an statement
357
+ # which redirects to the specified path is generated.
358
+ #
359
+ # Using the <tt>:remove</tt> statement, you will be able to pass a block, but it will
360
+ # be ignored as there is no HTML passed for this statement.
361
+ #
362
+ # === Using blocks
363
+ #
364
+ # Without a block, +assert_select_rjs+ merely asserts that the response
365
+ # contains one or more RJS statements that replace or update content.
366
+ #
367
+ # With a block, +assert_select_rjs+ also selects all elements used in
368
+ # these statements and passes them to the block. Nested assertions are
369
+ # supported.
370
+ #
371
+ # Calling +assert_select_rjs+ with no arguments and using nested asserts
372
+ # asserts that the HTML content is returned by one or more RJS statements.
373
+ # Using +assert_select+ directly makes the same assertion on the content,
374
+ # but without distinguishing whether the content is returned in an HTML
375
+ # or JavaScript.
376
+ #
377
+ # ==== Examples
378
+ #
379
+ # # Replacing the element foo.
380
+ # # page.replace 'foo', ...
381
+ # assert_select_rjs :replace, "foo"
382
+ #
383
+ # # Replacing with the chained RJS proxy.
384
+ # # page[:foo].replace ...
385
+ # assert_select_rjs :chained_replace, 'foo'
386
+ #
387
+ # # Inserting into the element bar, top position.
388
+ # assert_select_rjs :insert, :top, "bar"
389
+ #
390
+ # # Remove the element bar
391
+ # assert_select_rjs :remove, "bar"
392
+ #
393
+ # # Changing the element foo, with an image.
394
+ # assert_select_rjs "foo" do
395
+ # assert_select "img[src=/images/logo.gif""
396
+ # end
397
+ #
398
+ # # RJS inserts or updates a list with four items.
399
+ # assert_select_rjs do
400
+ # assert_select "ol>li", 4
401
+ # end
402
+ #
403
+ # # The same, but shorter.
404
+ # assert_select "ol>li", 4
405
+ #
406
+ # # Checking for a redirect.
407
+ # assert_select_rjs :redirect, root_path
408
+ def assert_select_rjs(*args, &block)
409
+ rjs_type = args.first.is_a?(Symbol) ? args.shift : nil
410
+ id = args.first.is_a?(String) ? args.shift : nil
411
+
412
+ # If the first argument is a symbol, it's the type of RJS statement we're looking
413
+ # for (update, replace, insertion, etc). Otherwise, we're looking for just about
414
+ # any RJS statement.
415
+ if rjs_type
416
+ if rjs_type == :insert
417
+ position = args.shift
418
+ id = args.shift
419
+ insertion = "insert_#{position}".to_sym
420
+ raise ArgumentError, "Unknown RJS insertion type #{position}" unless RJS_STATEMENTS[insertion]
421
+ statement = "(#{RJS_STATEMENTS[insertion]})"
422
+ else
423
+ raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type]
424
+ statement = "(#{RJS_STATEMENTS[rjs_type]})"
425
+ end
426
+ else
427
+ statement = "#{RJS_STATEMENTS[:any]}"
428
+ end
429
+
430
+ # Next argument we're looking for is the element identifier. If missing, we pick
431
+ # any element, otherwise we replace it in the statement.
432
+ pattern = Regexp.new(
433
+ id ? statement.gsub(RJS_ANY_ID, "\"#{id}\"") : statement
434
+ )
435
+
436
+ # Duplicate the body since the next step involves destroying it.
437
+ matches = nil
438
+ case rjs_type
439
+ when :remove, :show, :hide, :toggle
440
+ matches = @response.body.match(pattern)
441
+ else
442
+ @response.body.gsub(pattern) do |match|
443
+ html = unescape_rjs(match)
444
+ matches ||= []
445
+ matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? }
446
+ ""
447
+ end
448
+ end
449
+
450
+ if matches
451
+ assert_block("") { true } # to count the assertion
452
+ if block_given? && !([:remove, :show, :hide, :toggle].include? rjs_type)
453
+ begin
454
+ in_scope, @selected = @selected, matches
455
+ yield matches
456
+ ensure
457
+ @selected = in_scope
458
+ end
459
+ end
460
+ matches
461
+ else
462
+ # RJS statement not found.
463
+ case rjs_type
464
+ when :remove, :show, :hide, :toggle
465
+ flunk_message = "No RJS statement that #{rjs_type.to_s}s '#{id}' was rendered."
466
+ else
467
+ flunk_message = "No RJS statement that replaces or inserts HTML content."
468
+ end
469
+ flunk args.shift || flunk_message
470
+ end
471
+ end
472
+
473
+ # :call-seq:
474
+ # assert_select_encoded(element?) { |elements| ... }
475
+ #
476
+ # Extracts the content of an element, treats it as encoded HTML and runs
477
+ # nested assertion on it.
478
+ #
479
+ # You typically call this method within another assertion to operate on
480
+ # all currently selected elements. You can also pass an element or array
481
+ # of elements.
482
+ #
483
+ # The content of each element is un-encoded, and wrapped in the root
484
+ # element +encoded+. It then calls the block with all un-encoded elements.
485
+ #
486
+ # ==== Examples
487
+ # # Selects all bold tags from within the title of an ATOM feed's entries (perhaps to nab a section name prefix)
488
+ # assert_select_feed :atom, 1.0 do
489
+ # # Select each entry item and then the title item
490
+ # assert_select "entry>title" do
491
+ # # Run assertions on the encoded title elements
492
+ # assert_select_encoded do
493
+ # assert_select "b"
494
+ # end
495
+ # end
496
+ # end
497
+ #
498
+ #
499
+ # # Selects all paragraph tags from within the description of an RSS feed
500
+ # assert_select_feed :rss, 2.0 do
501
+ # # Select description element of each feed item.
502
+ # assert_select "channel>item>description" do
503
+ # # Run assertions on the encoded elements.
504
+ # assert_select_encoded do
505
+ # assert_select "p"
506
+ # end
507
+ # end
508
+ # end
509
+ def assert_select_encoded(element = nil, &block)
510
+ case element
511
+ when Array
512
+ elements = element
513
+ when HTML::Node
514
+ elements = [element]
515
+ when nil
516
+ unless elements = @selected
517
+ raise ArgumentError, "First argument is optional, but must be called from a nested assert_select"
518
+ end
519
+ else
520
+ raise ArgumentError, "Argument is optional, and may be node or array of nodes"
521
+ end
522
+
523
+ fix_content = lambda do |node|
524
+ # Gets around a bug in the Rails 1.1 HTML parser.
525
+ node.content.gsub(/<!\[CDATA\[(.*)(\]\]>)?/m) { CGI.escapeHTML($1) }
526
+ end
527
+
528
+ selected = elements.map do |element|
529
+ text = element.children.select{ |c| not c.tag? }.map{ |c| fix_content[c] }.join
530
+ root = HTML::Document.new(CGI.unescapeHTML("<encoded>#{text}</encoded>")).root
531
+ css_select(root, "encoded:root", &block)[0]
532
+ end
533
+
534
+ begin
535
+ old_selected, @selected = @selected, selected
536
+ assert_select ":root", &block
537
+ ensure
538
+ @selected = old_selected
539
+ end
540
+ end
541
+
542
+ # :call-seq:
543
+ # assert_select_email { }
544
+ #
545
+ # Extracts the body of an email and runs nested assertions on it.
546
+ #
547
+ # You must enable deliveries for this assertion to work, use:
548
+ # ActionMailer::Base.perform_deliveries = true
549
+ #
550
+ # ==== Examples
551
+ #
552
+ # assert_select_email do
553
+ # assert_select "h1", "Email alert"
554
+ # end
555
+ #
556
+ # assert_select_email do
557
+ # items = assert_select "ol>li"
558
+ # items.each do
559
+ # # Work with items here...
560
+ # end
561
+ # end
562
+ #
563
+ def assert_select_email(&block)
564
+ deliveries = ActionMailer::Base.deliveries
565
+ assert !deliveries.empty?, "No e-mail in delivery list"
566
+
567
+ for delivery in deliveries
568
+ for part in delivery.parts
569
+ if part["Content-Type"].to_s =~ /^text\/html\W/
570
+ root = HTML::Document.new(part.body).root
571
+ assert_select root, ":root", &block
572
+ end
573
+ end
574
+ end
575
+ end
576
+
577
+ protected
578
+ unless const_defined?(:RJS_STATEMENTS)
579
+ RJS_PATTERN_HTML = "\"((\\\\\"|[^\"])*)\""
580
+ RJS_ANY_ID = "\"([^\"])*\""
581
+ RJS_STATEMENTS = {
582
+ :chained_replace => "\\$\\(#{RJS_ANY_ID}\\)\\.replace\\(#{RJS_PATTERN_HTML}\\)",
583
+ :chained_replace_html => "\\$\\(#{RJS_ANY_ID}\\)\\.update\\(#{RJS_PATTERN_HTML}\\)",
584
+ :replace_html => "Element\\.update\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)",
585
+ :replace => "Element\\.replace\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)",
586
+ :redirect => "window.location.href = #{RJS_ANY_ID}"
587
+ }
588
+ [:remove, :show, :hide, :toggle].each do |action|
589
+ RJS_STATEMENTS[action] = "Element\\.#{action}\\(#{RJS_ANY_ID}\\)"
590
+ end
591
+ RJS_INSERTIONS = ["top", "bottom", "before", "after"]
592
+ RJS_INSERTIONS.each do |insertion|
593
+ RJS_STATEMENTS["insert_#{insertion}".to_sym] = "Element.insert\\(#{RJS_ANY_ID}, \\{ #{insertion}: #{RJS_PATTERN_HTML} \\}\\)"
594
+ end
595
+ RJS_STATEMENTS[:insert_html] = "Element.insert\\(#{RJS_ANY_ID}, \\{ (#{RJS_INSERTIONS.join('|')}): #{RJS_PATTERN_HTML} \\}\\)"
596
+ RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")
597
+ RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
598
+ end
599
+
600
+ # +assert_select+ and +css_select+ call this to obtain the content in the HTML
601
+ # page, or from all the RJS statements, depending on the type of response.
602
+ def response_from_page_or_rjs()
603
+ content_type = @response.content_type
604
+
605
+ if content_type && Mime::JS =~ content_type
606
+ body = @response.body.dup
607
+ root = HTML::Node.new(nil)
608
+
609
+ while true
610
+ next if body.sub!(RJS_STATEMENTS[:any]) do |match|
611
+ html = unescape_rjs(match)
612
+ matches = HTML::Document.new(html).root.children.select { |n| n.tag? }
613
+ root.children.concat matches
614
+ ""
615
+ end
616
+ break
617
+ end
618
+
619
+ root
620
+ else
621
+ html_document.root
622
+ end
623
+ end
624
+
625
+ # Unescapes a RJS string.
626
+ def unescape_rjs(rjs_string)
627
+ # RJS encodes double quotes and line breaks.
628
+ unescaped= rjs_string.gsub('\"', '"')
629
+ unescaped.gsub!(/\\\//, '/')
630
+ unescaped.gsub!('\n', "\n")
631
+ unescaped.gsub!('\076', '>')
632
+ unescaped.gsub!('\074', '<')
633
+ # RJS encodes non-ascii characters.
634
+ unescaped.gsub!(RJS_PATTERN_UNICODE_ESCAPED_CHAR) {|u| [$1.hex].pack('U*')}
635
+ unescaped
636
+ end
637
+ end
638
+ end
639
+ end