actionpack 7.1.5.1 → 8.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +308 -523
- data/README.rdoc +1 -1
- data/lib/abstract_controller/asset_paths.rb +6 -2
- data/lib/abstract_controller/base.rb +104 -105
- data/lib/abstract_controller/caching/fragments.rb +50 -53
- data/lib/abstract_controller/caching.rb +8 -3
- data/lib/abstract_controller/callbacks.rb +70 -62
- data/lib/abstract_controller/collector.rb +7 -7
- data/lib/abstract_controller/deprecator.rb +2 -0
- data/lib/abstract_controller/error.rb +2 -0
- data/lib/abstract_controller/helpers.rb +71 -84
- data/lib/abstract_controller/logger.rb +4 -1
- data/lib/abstract_controller/railties/routes_helpers.rb +2 -0
- data/lib/abstract_controller/rendering.rb +13 -13
- data/lib/abstract_controller/translation.rb +12 -13
- data/lib/abstract_controller/url_for.rb +8 -6
- data/lib/abstract_controller.rb +2 -0
- data/lib/action_controller/api/api_rendering.rb +2 -0
- data/lib/action_controller/api.rb +76 -72
- data/lib/action_controller/base.rb +199 -126
- data/lib/action_controller/caching.rb +16 -14
- data/lib/action_controller/deprecator.rb +2 -0
- data/lib/action_controller/form_builder.rb +21 -18
- data/lib/action_controller/log_subscriber.rb +23 -2
- data/lib/action_controller/metal/allow_browser.rb +133 -0
- data/lib/action_controller/metal/basic_implicit_render.rb +2 -0
- data/lib/action_controller/metal/conditional_get.rb +217 -175
- data/lib/action_controller/metal/content_security_policy.rb +25 -24
- data/lib/action_controller/metal/cookies.rb +4 -2
- data/lib/action_controller/metal/data_streaming.rb +72 -63
- data/lib/action_controller/metal/default_headers.rb +5 -3
- data/lib/action_controller/metal/etag_with_flash.rb +3 -1
- data/lib/action_controller/metal/etag_with_template_digest.rb +17 -15
- data/lib/action_controller/metal/exceptions.rb +16 -9
- data/lib/action_controller/metal/flash.rb +13 -14
- data/lib/action_controller/metal/head.rb +15 -11
- data/lib/action_controller/metal/helpers.rb +63 -55
- data/lib/action_controller/metal/http_authentication.rb +209 -201
- data/lib/action_controller/metal/implicit_render.rb +17 -15
- data/lib/action_controller/metal/instrumentation.rb +16 -14
- data/lib/action_controller/metal/live.rb +177 -128
- data/lib/action_controller/metal/logging.rb +6 -4
- data/lib/action_controller/metal/mime_responds.rb +151 -142
- data/lib/action_controller/metal/parameter_encoding.rb +34 -32
- data/lib/action_controller/metal/params_wrapper.rb +57 -59
- data/lib/action_controller/metal/permissions_policy.rb +22 -12
- data/lib/action_controller/metal/rate_limiting.rb +92 -0
- data/lib/action_controller/metal/redirecting.rb +213 -94
- data/lib/action_controller/metal/renderers.rb +78 -57
- data/lib/action_controller/metal/rendering.rb +111 -77
- data/lib/action_controller/metal/request_forgery_protection.rb +182 -143
- data/lib/action_controller/metal/rescue.rb +20 -9
- data/lib/action_controller/metal/streaming.rb +118 -195
- data/lib/action_controller/metal/strong_parameters.rb +720 -530
- data/lib/action_controller/metal/testing.rb +2 -0
- data/lib/action_controller/metal/url_for.rb +17 -15
- data/lib/action_controller/metal.rb +86 -60
- data/lib/action_controller/railtie.rb +36 -15
- data/lib/action_controller/railties/helpers.rb +2 -0
- data/lib/action_controller/renderer.rb +41 -36
- data/lib/action_controller/structured_event_subscriber.rb +116 -0
- data/lib/action_controller/template_assertions.rb +4 -2
- data/lib/action_controller/test_case.rb +160 -131
- data/lib/action_controller.rb +5 -1
- data/lib/action_dispatch/constants.rb +8 -0
- data/lib/action_dispatch/deprecator.rb +2 -0
- data/lib/action_dispatch/http/cache.rb +163 -35
- data/lib/action_dispatch/http/content_disposition.rb +2 -0
- data/lib/action_dispatch/http/content_security_policy.rb +54 -39
- data/lib/action_dispatch/http/filter_parameters.rb +14 -8
- data/lib/action_dispatch/http/filter_redirect.rb +22 -1
- data/lib/action_dispatch/http/headers.rb +22 -22
- data/lib/action_dispatch/http/mime_negotiation.rb +89 -41
- data/lib/action_dispatch/http/mime_type.rb +25 -21
- data/lib/action_dispatch/http/mime_types.rb +3 -0
- data/lib/action_dispatch/http/param_builder.rb +187 -0
- data/lib/action_dispatch/http/param_error.rb +26 -0
- data/lib/action_dispatch/http/parameters.rb +14 -12
- data/lib/action_dispatch/http/permissions_policy.rb +25 -36
- data/lib/action_dispatch/http/query_parser.rb +55 -0
- data/lib/action_dispatch/http/rack_cache.rb +2 -0
- data/lib/action_dispatch/http/request.rb +141 -92
- data/lib/action_dispatch/http/response.rb +137 -77
- data/lib/action_dispatch/http/upload.rb +18 -16
- data/lib/action_dispatch/http/url.rb +187 -89
- data/lib/action_dispatch/journey/formatter.rb +21 -9
- data/lib/action_dispatch/journey/gtg/builder.rb +4 -3
- data/lib/action_dispatch/journey/gtg/simulator.rb +34 -11
- data/lib/action_dispatch/journey/gtg/transition_table.rb +47 -53
- data/lib/action_dispatch/journey/nfa/dot.rb +2 -0
- data/lib/action_dispatch/journey/nodes/node.rb +8 -6
- data/lib/action_dispatch/journey/parser.rb +99 -195
- data/lib/action_dispatch/journey/path/pattern.rb +4 -1
- data/lib/action_dispatch/journey/route.rb +54 -38
- data/lib/action_dispatch/journey/router/utils.rb +22 -27
- data/lib/action_dispatch/journey/router.rb +63 -83
- data/lib/action_dispatch/journey/routes.rb +11 -2
- data/lib/action_dispatch/journey/scanner.rb +46 -42
- data/lib/action_dispatch/journey/visitors.rb +57 -23
- data/lib/action_dispatch/journey/visualizer/fsm.js +4 -6
- data/lib/action_dispatch/journey.rb +2 -0
- data/lib/action_dispatch/log_subscriber.rb +7 -1
- data/lib/action_dispatch/middleware/actionable_exceptions.rb +2 -0
- data/lib/action_dispatch/middleware/assume_ssl.rb +8 -5
- data/lib/action_dispatch/middleware/callbacks.rb +3 -1
- data/lib/action_dispatch/middleware/cookies.rb +125 -106
- data/lib/action_dispatch/middleware/debug_exceptions.rb +37 -8
- data/lib/action_dispatch/middleware/debug_locks.rb +15 -13
- data/lib/action_dispatch/middleware/debug_view.rb +13 -5
- data/lib/action_dispatch/middleware/exception_wrapper.rb +18 -23
- data/lib/action_dispatch/middleware/executor.rb +19 -4
- data/lib/action_dispatch/middleware/flash.rb +63 -51
- data/lib/action_dispatch/middleware/host_authorization.rb +17 -15
- data/lib/action_dispatch/middleware/public_exceptions.rb +14 -12
- data/lib/action_dispatch/middleware/reloader.rb +5 -3
- data/lib/action_dispatch/middleware/remote_ip.rb +87 -77
- data/lib/action_dispatch/middleware/request_id.rb +16 -10
- data/lib/action_dispatch/middleware/server_timing.rb +4 -2
- data/lib/action_dispatch/middleware/session/abstract_store.rb +2 -0
- data/lib/action_dispatch/middleware/session/cache_store.rb +30 -8
- data/lib/action_dispatch/middleware/session/cookie_store.rb +27 -26
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +7 -3
- data/lib/action_dispatch/middleware/show_exceptions.rb +16 -16
- data/lib/action_dispatch/middleware/ssl.rb +53 -40
- data/lib/action_dispatch/middleware/stack.rb +11 -10
- data/lib/action_dispatch/middleware/static.rb +33 -31
- data/lib/action_dispatch/middleware/templates/rescues/_copy_button.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +3 -5
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +9 -5
- data/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +4 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +50 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +1 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +1 -1
- data/lib/action_dispatch/railtie.rb +23 -3
- data/lib/action_dispatch/request/session.rb +24 -21
- data/lib/action_dispatch/request/utils.rb +11 -3
- data/lib/action_dispatch/routing/endpoint.rb +2 -0
- data/lib/action_dispatch/routing/inspector.rb +85 -60
- data/lib/action_dispatch/routing/mapper.rb +1031 -851
- data/lib/action_dispatch/routing/polymorphic_routes.rb +69 -62
- data/lib/action_dispatch/routing/redirection.rb +47 -39
- data/lib/action_dispatch/routing/route_set.rb +79 -56
- data/lib/action_dispatch/routing/routes_proxy.rb +7 -4
- data/lib/action_dispatch/routing/url_for.rb +130 -125
- data/lib/action_dispatch/routing.rb +150 -148
- data/lib/action_dispatch/structured_event_subscriber.rb +20 -0
- data/lib/action_dispatch/system_test_case.rb +91 -81
- data/lib/action_dispatch/system_testing/browser.rb +16 -23
- data/lib/action_dispatch/system_testing/driver.rb +2 -0
- data/lib/action_dispatch/system_testing/server.rb +2 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +34 -23
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +2 -0
- data/lib/action_dispatch/testing/assertion_response.rb +9 -7
- data/lib/action_dispatch/testing/assertions/response.rb +52 -25
- data/lib/action_dispatch/testing/assertions/routing.rb +168 -87
- data/lib/action_dispatch/testing/assertions.rb +2 -0
- data/lib/action_dispatch/testing/integration.rb +233 -223
- data/lib/action_dispatch/testing/request_encoder.rb +11 -9
- data/lib/action_dispatch/testing/test_helpers/page_dump_helper.rb +35 -0
- data/lib/action_dispatch/testing/test_process.rb +11 -8
- data/lib/action_dispatch/testing/test_request.rb +3 -1
- data/lib/action_dispatch/testing/test_response.rb +27 -26
- data/lib/action_dispatch.rb +36 -32
- data/lib/action_pack/gem_version.rb +6 -4
- data/lib/action_pack/version.rb +3 -1
- data/lib/action_pack.rb +17 -16
- metadata +36 -32
- data/lib/action_dispatch/journey/parser.y +0 -50
- data/lib/action_dispatch/journey/parser_extras.rb +0 -31
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# :markup: markdown
|
|
4
|
+
|
|
3
5
|
require "rack/session/abstract/id"
|
|
4
6
|
require "active_support/core_ext/hash/conversions"
|
|
5
7
|
require "active_support/core_ext/object/to_query"
|
|
@@ -16,21 +18,31 @@ module ActionController
|
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
module Live
|
|
19
|
-
# Disable controller / rendering threads in tests. User tests can access
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
21
|
+
# Disable controller / rendering threads in tests. User tests can access the
|
|
22
|
+
# database on the main thread, so they could open a txn, then the controller
|
|
23
|
+
# thread will open a new connection and try to access data that's only visible
|
|
24
|
+
# to the main thread's txn. This is the problem in #23483.
|
|
25
|
+
alias_method :original_new_controller_thread, :new_controller_thread
|
|
26
|
+
|
|
23
27
|
silence_redefinition_of_method :new_controller_thread
|
|
24
28
|
def new_controller_thread # :nodoc:
|
|
25
29
|
yield
|
|
26
30
|
end
|
|
27
31
|
|
|
32
|
+
# Because of the above, we need to prevent the clearing of thread locals, since
|
|
33
|
+
# no new thread is actually spawned in the test environment.
|
|
34
|
+
alias_method :original_clean_up_thread_locals, :clean_up_thread_locals
|
|
35
|
+
|
|
36
|
+
silence_redefinition_of_method :clean_up_thread_locals
|
|
37
|
+
def clean_up_thread_locals(*args) # :nodoc:
|
|
38
|
+
end
|
|
39
|
+
|
|
28
40
|
# Avoid a deadlock from the queue filling up
|
|
29
41
|
Buffer.queue_size = nil
|
|
30
42
|
end
|
|
31
43
|
|
|
32
|
-
# ActionController::TestCase will be deprecated and moved to a gem in the
|
|
33
|
-
# Please use ActionDispatch::IntegrationTest going forward.
|
|
44
|
+
# ActionController::TestCase will be deprecated and moved to a gem in the
|
|
45
|
+
# future. Please use ActionDispatch::IntegrationTest going forward.
|
|
34
46
|
class TestRequest < ActionDispatch::TestRequest # :nodoc:
|
|
35
47
|
DEFAULT_ENV = ActionDispatch::TestRequest::DEFAULT_ENV.dup
|
|
36
48
|
DEFAULT_ENV.delete "PATH_INFO"
|
|
@@ -104,7 +116,7 @@ module ActionController
|
|
|
104
116
|
set_header k, "application/x-www-form-urlencoded"
|
|
105
117
|
end
|
|
106
118
|
|
|
107
|
-
case content_mime_type
|
|
119
|
+
case content_mime_type&.to_sym
|
|
108
120
|
when nil
|
|
109
121
|
raise "Unknown Content-Type: #{content_type}"
|
|
110
122
|
when :json
|
|
@@ -119,7 +131,7 @@ module ActionController
|
|
|
119
131
|
end
|
|
120
132
|
end
|
|
121
133
|
|
|
122
|
-
data_stream = StringIO.new(data)
|
|
134
|
+
data_stream = StringIO.new(data.b)
|
|
123
135
|
set_header "CONTENT_LENGTH", data_stream.length.to_s
|
|
124
136
|
set_header "rack.input", data_stream
|
|
125
137
|
end
|
|
@@ -232,116 +244,127 @@ module ActionController
|
|
|
232
244
|
end
|
|
233
245
|
end
|
|
234
246
|
|
|
235
|
-
#
|
|
247
|
+
# # Action Controller Test Case
|
|
236
248
|
#
|
|
237
|
-
# Superclass for ActionController functional tests. Functional tests allow you
|
|
238
|
-
# test a single controller action per test method.
|
|
249
|
+
# Superclass for ActionController functional tests. Functional tests allow you
|
|
250
|
+
# to test a single controller action per test method.
|
|
239
251
|
#
|
|
240
|
-
#
|
|
252
|
+
# ## Use integration style controller tests over functional style controller tests.
|
|
241
253
|
#
|
|
242
|
-
#
|
|
254
|
+
# Rails discourages the use of functional tests in favor of integration tests
|
|
243
255
|
# (use ActionDispatch::IntegrationTest).
|
|
244
256
|
#
|
|
245
|
-
# New
|
|
246
|
-
# only be used for backward compatibility. Integration style
|
|
247
|
-
# requests, whereas functional style controller
|
|
248
|
-
#
|
|
249
|
-
#
|
|
257
|
+
# New Rails applications no longer generate functional style controller tests
|
|
258
|
+
# and they should only be used for backward compatibility. Integration style
|
|
259
|
+
# controller tests perform actual requests, whereas functional style controller
|
|
260
|
+
# tests merely simulate a request. Besides, integration tests are as fast as
|
|
261
|
+
# functional tests and provide lot of helpers such as `as`, `parsed_body` for
|
|
262
|
+
# effective testing of controller actions including even API endpoints.
|
|
250
263
|
#
|
|
251
|
-
#
|
|
264
|
+
# ## Basic example
|
|
252
265
|
#
|
|
253
266
|
# Functional tests are written as follows:
|
|
254
|
-
# 1.
|
|
255
|
-
#
|
|
256
|
-
# 2.
|
|
257
|
-
#
|
|
267
|
+
# 1. First, one uses the `get`, `post`, `patch`, `put`, `delete`, or `head`
|
|
268
|
+
# method to simulate an HTTP request.
|
|
269
|
+
# 2. Then, one asserts whether the current state is as expected. "State" can be
|
|
270
|
+
# anything: the controller's HTTP response, the database contents, etc.
|
|
271
|
+
#
|
|
258
272
|
#
|
|
259
273
|
# For example:
|
|
260
274
|
#
|
|
261
|
-
#
|
|
262
|
-
#
|
|
263
|
-
#
|
|
264
|
-
#
|
|
275
|
+
# class BooksControllerTest < ActionController::TestCase
|
|
276
|
+
# def test_create
|
|
277
|
+
# # Simulate a POST response with the given HTTP parameters.
|
|
278
|
+
# post(:create, params: { book: { title: "Love Hina" }})
|
|
265
279
|
#
|
|
266
|
-
#
|
|
267
|
-
#
|
|
268
|
-
#
|
|
280
|
+
# # Asserts that the controller tried to redirect us to
|
|
281
|
+
# # the created book's URI.
|
|
282
|
+
# assert_response :found
|
|
269
283
|
#
|
|
270
|
-
#
|
|
271
|
-
#
|
|
284
|
+
# # Asserts that the controller really put the book in the database.
|
|
285
|
+
# assert_not_nil Book.find_by(title: "Love Hina")
|
|
286
|
+
# end
|
|
272
287
|
# end
|
|
273
|
-
# end
|
|
274
288
|
#
|
|
275
289
|
# You can also send a real document in the simulated HTTP request.
|
|
276
290
|
#
|
|
277
|
-
#
|
|
278
|
-
#
|
|
279
|
-
#
|
|
280
|
-
#
|
|
291
|
+
# def test_create
|
|
292
|
+
# json = {book: { title: "Love Hina" }}.to_json
|
|
293
|
+
# post :create, body: json
|
|
294
|
+
# end
|
|
295
|
+
#
|
|
296
|
+
# ## Special instance variables
|
|
297
|
+
#
|
|
298
|
+
# ActionController::TestCase will also automatically provide the following
|
|
299
|
+
# instance variables for use in the tests:
|
|
300
|
+
#
|
|
301
|
+
# @controller
|
|
302
|
+
# : The controller instance that will be tested.
|
|
281
303
|
#
|
|
282
|
-
#
|
|
304
|
+
# @request
|
|
305
|
+
# : An ActionController::TestRequest, representing the current HTTP request.
|
|
306
|
+
# You can modify this object before sending the HTTP request. For example,
|
|
307
|
+
# you might want to set some session properties before sending a GET
|
|
308
|
+
# request.
|
|
283
309
|
#
|
|
284
|
-
#
|
|
285
|
-
#
|
|
310
|
+
# @response
|
|
311
|
+
# : An ActionDispatch::TestResponse object, representing the response of the
|
|
312
|
+
# last HTTP response. In the above example, `@response` becomes valid after
|
|
313
|
+
# calling `post`. If the various assert methods are not sufficient, then you
|
|
314
|
+
# may use this object to inspect the HTTP response in detail.
|
|
286
315
|
#
|
|
287
|
-
# @controller::
|
|
288
|
-
# The controller instance that will be tested.
|
|
289
|
-
# @request::
|
|
290
|
-
# An ActionController::TestRequest, representing the current HTTP
|
|
291
|
-
# request. You can modify this object before sending the HTTP request. For example,
|
|
292
|
-
# you might want to set some session properties before sending a GET request.
|
|
293
|
-
# @response::
|
|
294
|
-
# An ActionDispatch::TestResponse object, representing the response
|
|
295
|
-
# of the last HTTP response. In the above example, <tt>@response</tt> becomes valid
|
|
296
|
-
# after calling +post+. If the various assert methods are not sufficient, then you
|
|
297
|
-
# may use this object to inspect the HTTP response in detail.
|
|
298
316
|
#
|
|
299
|
-
#
|
|
317
|
+
# ## Controller is automatically inferred
|
|
300
318
|
#
|
|
301
319
|
# ActionController::TestCase will automatically infer the controller under test
|
|
302
320
|
# from the test class name. If the controller cannot be inferred from the test
|
|
303
|
-
# class name, you can explicitly set it with
|
|
321
|
+
# class name, you can explicitly set it with `tests`.
|
|
304
322
|
#
|
|
305
|
-
#
|
|
306
|
-
#
|
|
307
|
-
#
|
|
323
|
+
# class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase
|
|
324
|
+
# tests WidgetController
|
|
325
|
+
# end
|
|
326
|
+
#
|
|
327
|
+
# ## Testing controller internals
|
|
308
328
|
#
|
|
309
|
-
#
|
|
329
|
+
# In addition to these specific assertions, you also have easy access to various
|
|
330
|
+
# collections that the regular test/unit assertions can be used against. These
|
|
331
|
+
# collections are:
|
|
310
332
|
#
|
|
311
|
-
#
|
|
312
|
-
#
|
|
333
|
+
# * session: Objects being saved in the session.
|
|
334
|
+
# * flash: The flash objects currently in the session.
|
|
335
|
+
# * cookies: Cookies being sent to the user on this request.
|
|
313
336
|
#
|
|
314
|
-
# * session: Objects being saved in the session.
|
|
315
|
-
# * flash: The flash objects currently in the session.
|
|
316
|
-
# * cookies: \Cookies being sent to the user on this request.
|
|
317
337
|
#
|
|
318
338
|
# These collections can be used just like any other hash:
|
|
319
339
|
#
|
|
320
|
-
#
|
|
321
|
-
#
|
|
340
|
+
# assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
|
|
341
|
+
# assert flash.empty? # makes sure that there's nothing in the flash
|
|
322
342
|
#
|
|
323
|
-
# On top of the collections, you have the complete URL that a given action
|
|
343
|
+
# On top of the collections, you have the complete URL that a given action
|
|
344
|
+
# redirected to available in `redirect_to_url`.
|
|
324
345
|
#
|
|
325
|
-
# For redirects within the same controller, you can even call follow_redirect
|
|
326
|
-
# action call which can
|
|
346
|
+
# For redirects within the same controller, you can even call follow_redirect
|
|
347
|
+
# and the redirect will be followed, triggering another action call which can
|
|
348
|
+
# then be asserted against.
|
|
327
349
|
#
|
|
328
|
-
#
|
|
350
|
+
# ## Manipulating session and cookie variables
|
|
329
351
|
#
|
|
330
|
-
# Sometimes you need to set up the session and cookie variables for a test.
|
|
331
|
-
#
|
|
352
|
+
# Sometimes you need to set up the session and cookie variables for a test. To
|
|
353
|
+
# do this just assign a value to the session or cookie collection:
|
|
332
354
|
#
|
|
333
|
-
#
|
|
334
|
-
#
|
|
355
|
+
# session[:key] = "value"
|
|
356
|
+
# cookies[:key] = "value"
|
|
335
357
|
#
|
|
336
358
|
# To clear the cookies for a test just clear the cookie collection:
|
|
337
359
|
#
|
|
338
|
-
#
|
|
360
|
+
# cookies.clear
|
|
339
361
|
#
|
|
340
|
-
#
|
|
362
|
+
# ## Testing named routes
|
|
341
363
|
#
|
|
342
|
-
# If you're using named routes, they can be easily tested using the original
|
|
364
|
+
# If you're using named routes, they can be easily tested using the original
|
|
365
|
+
# named routes' methods straight in the test case.
|
|
343
366
|
#
|
|
344
|
-
#
|
|
367
|
+
# assert_redirected_to page_url(title: 'foo')
|
|
345
368
|
class TestCase < ActiveSupport::TestCase
|
|
346
369
|
singleton_class.attr_accessor :executor_around_each_request
|
|
347
370
|
|
|
@@ -354,12 +377,12 @@ module ActionController
|
|
|
354
377
|
attr_reader :response, :request
|
|
355
378
|
|
|
356
379
|
module ClassMethods
|
|
357
|
-
# Sets the controller class name. Useful if the name can't be inferred from test
|
|
358
|
-
# Normalizes
|
|
380
|
+
# Sets the controller class name. Useful if the name can't be inferred from test
|
|
381
|
+
# class. Normalizes `controller_class` before using.
|
|
359
382
|
#
|
|
360
|
-
#
|
|
361
|
-
#
|
|
362
|
-
#
|
|
383
|
+
# tests WidgetController
|
|
384
|
+
# tests :widget
|
|
385
|
+
# tests 'widget'
|
|
363
386
|
def tests(controller_class)
|
|
364
387
|
case controller_class
|
|
365
388
|
when String, Symbol
|
|
@@ -392,92 +415,97 @@ module ActionController
|
|
|
392
415
|
|
|
393
416
|
# Simulate a GET request with the given parameters.
|
|
394
417
|
#
|
|
395
|
-
#
|
|
396
|
-
#
|
|
397
|
-
#
|
|
398
|
-
#
|
|
399
|
-
#
|
|
400
|
-
#
|
|
418
|
+
# * `action`: The controller action to call.
|
|
419
|
+
# * `params`: The hash with HTTP parameters that you want to pass. This may be
|
|
420
|
+
# `nil`.
|
|
421
|
+
# * `body`: The request body with a string that is appropriately encoded
|
|
422
|
+
# (`application/x-www-form-urlencoded` or `multipart/form-data`).
|
|
423
|
+
# * `session`: A hash of parameters to store in the session. This may be
|
|
424
|
+
# `nil`.
|
|
425
|
+
# * `flash`: A hash of parameters to store in the flash. This may be `nil`.
|
|
401
426
|
#
|
|
402
|
-
# You can also simulate POST, PATCH, PUT, DELETE, and HEAD requests with
|
|
403
|
-
# +post+, +patch+, +put+, +delete+, and +head+.
|
|
404
|
-
# Example sending parameters, session, and setting a flash message:
|
|
405
427
|
#
|
|
406
|
-
#
|
|
407
|
-
#
|
|
408
|
-
#
|
|
409
|
-
#
|
|
428
|
+
# You can also simulate POST, PATCH, PUT, DELETE, and HEAD requests with `post`,
|
|
429
|
+
# `patch`, `put`, `delete`, and `head`. Example sending parameters, session, and
|
|
430
|
+
# setting a flash message:
|
|
431
|
+
#
|
|
432
|
+
# get :show,
|
|
433
|
+
# params: { id: 7 },
|
|
434
|
+
# session: { user_id: 1 },
|
|
435
|
+
# flash: { notice: 'This is flash message' }
|
|
410
436
|
#
|
|
411
437
|
# Note that the request method is not verified. The different methods are
|
|
412
438
|
# available to make the tests more expressive.
|
|
413
439
|
def get(action, **args)
|
|
414
|
-
|
|
415
|
-
cookies.update res.cookies
|
|
416
|
-
res
|
|
440
|
+
process(action, method: "GET", **args)
|
|
417
441
|
end
|
|
418
442
|
|
|
419
443
|
# Simulate a POST request with the given parameters and set/volley the response.
|
|
420
|
-
# See
|
|
444
|
+
# See `get` for more details.
|
|
421
445
|
def post(action, **args)
|
|
422
446
|
process(action, method: "POST", **args)
|
|
423
447
|
end
|
|
424
448
|
|
|
425
|
-
# Simulate a PATCH request with the given parameters and set/volley the
|
|
426
|
-
# See
|
|
449
|
+
# Simulate a PATCH request with the given parameters and set/volley the
|
|
450
|
+
# response. See `get` for more details.
|
|
427
451
|
def patch(action, **args)
|
|
428
452
|
process(action, method: "PATCH", **args)
|
|
429
453
|
end
|
|
430
454
|
|
|
431
455
|
# Simulate a PUT request with the given parameters and set/volley the response.
|
|
432
|
-
# See
|
|
456
|
+
# See `get` for more details.
|
|
433
457
|
def put(action, **args)
|
|
434
458
|
process(action, method: "PUT", **args)
|
|
435
459
|
end
|
|
436
460
|
|
|
437
|
-
# Simulate a DELETE request with the given parameters and set/volley the
|
|
438
|
-
# See
|
|
461
|
+
# Simulate a DELETE request with the given parameters and set/volley the
|
|
462
|
+
# response. See `get` for more details.
|
|
439
463
|
def delete(action, **args)
|
|
440
464
|
process(action, method: "DELETE", **args)
|
|
441
465
|
end
|
|
442
466
|
|
|
443
467
|
# Simulate a HEAD request with the given parameters and set/volley the response.
|
|
444
|
-
# See
|
|
468
|
+
# See `get` for more details.
|
|
445
469
|
def head(action, **args)
|
|
446
470
|
process(action, method: "HEAD", **args)
|
|
447
471
|
end
|
|
448
472
|
|
|
449
|
-
# Simulate an HTTP request to
|
|
450
|
-
#
|
|
473
|
+
# Simulate an HTTP request to `action` by specifying request method, parameters
|
|
474
|
+
# and set/volley the response.
|
|
475
|
+
#
|
|
476
|
+
# * `action`: The controller action to call.
|
|
477
|
+
# * `method`: Request method used to send the HTTP request. Possible values
|
|
478
|
+
# are `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, `HEAD`. Defaults to `GET`.
|
|
479
|
+
# Can be a symbol.
|
|
480
|
+
# * `params`: The hash with HTTP parameters that you want to pass. This may be
|
|
481
|
+
# `nil`.
|
|
482
|
+
# * `body`: The request body with a string that is appropriately encoded
|
|
483
|
+
# (`application/x-www-form-urlencoded` or `multipart/form-data`).
|
|
484
|
+
# * `session`: A hash of parameters to store in the session. This may be
|
|
485
|
+
# `nil`.
|
|
486
|
+
# * `flash`: A hash of parameters to store in the flash. This may be `nil`.
|
|
487
|
+
# * `format`: Request format. Defaults to `nil`. Can be string or symbol.
|
|
488
|
+
# * `as`: Content type. Defaults to `nil`. Must be a symbol that corresponds
|
|
489
|
+
# to a mime type.
|
|
451
490
|
#
|
|
452
|
-
# - +action+: The controller action to call.
|
|
453
|
-
# - +method+: Request method used to send the HTTP request. Possible values
|
|
454
|
-
# are +GET+, +POST+, +PATCH+, +PUT+, +DELETE+, +HEAD+. Defaults to +GET+. Can be a symbol.
|
|
455
|
-
# - +params+: The hash with HTTP parameters that you want to pass. This may be +nil+.
|
|
456
|
-
# - +body+: The request body with a string that is appropriately encoded
|
|
457
|
-
# (<tt>application/x-www-form-urlencoded</tt> or <tt>multipart/form-data</tt>).
|
|
458
|
-
# - +session+: A hash of parameters to store in the session. This may be +nil+.
|
|
459
|
-
# - +flash+: A hash of parameters to store in the flash. This may be +nil+.
|
|
460
|
-
# - +format+: Request format. Defaults to +nil+. Can be string or symbol.
|
|
461
|
-
# - +as+: Content type. Defaults to +nil+. Must be a symbol that corresponds
|
|
462
|
-
# to a mime type.
|
|
463
491
|
#
|
|
464
|
-
# Example calling
|
|
492
|
+
# Example calling `create` action and sending two params:
|
|
465
493
|
#
|
|
466
|
-
#
|
|
467
|
-
#
|
|
468
|
-
#
|
|
469
|
-
#
|
|
470
|
-
#
|
|
471
|
-
#
|
|
472
|
-
#
|
|
494
|
+
# process :create,
|
|
495
|
+
# method: 'POST',
|
|
496
|
+
# params: {
|
|
497
|
+
# user: { name: 'Gaurish Sharma', email: 'user@example.com' }
|
|
498
|
+
# },
|
|
499
|
+
# session: { user_id: 1 },
|
|
500
|
+
# flash: { notice: 'This is flash message' }
|
|
473
501
|
#
|
|
474
|
-
# To simulate
|
|
475
|
-
# prefer using #get, #post, #patch, #put, #delete and #head methods
|
|
476
|
-
#
|
|
502
|
+
# To simulate `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, and `HEAD` requests
|
|
503
|
+
# prefer using #get, #post, #patch, #put, #delete and #head methods respectively
|
|
504
|
+
# which will make tests more expressive.
|
|
477
505
|
#
|
|
478
506
|
# It's not recommended to make more than one request in the same test. Instance
|
|
479
507
|
# variables that are set in one request will not persist to the next request,
|
|
480
|
-
# but it's not guaranteed that all
|
|
508
|
+
# but it's not guaranteed that all Rails internal state will be reset. Prefer
|
|
481
509
|
# ActionDispatch::IntegrationTest for making multiple requests in the same test.
|
|
482
510
|
#
|
|
483
511
|
# Note that the request method is not verified.
|
|
@@ -617,6 +645,7 @@ module ActionController
|
|
|
617
645
|
unless @request.cookie_jar.committed?
|
|
618
646
|
@request.cookie_jar.write(@response)
|
|
619
647
|
cookies.update(@request.cookie_jar.instance_variable_get(:@cookies))
|
|
648
|
+
cookies.update(@response.cookies)
|
|
620
649
|
end
|
|
621
650
|
end
|
|
622
651
|
@response.prepare!
|
|
@@ -654,8 +683,8 @@ module ActionController
|
|
|
654
683
|
end
|
|
655
684
|
|
|
656
685
|
def check_required_ivars
|
|
657
|
-
# Check for required instance variables so we can give an
|
|
658
|
-
#
|
|
686
|
+
# Check for required instance variables so we can give an understandable error
|
|
687
|
+
# message.
|
|
659
688
|
[:@routes, :@controller, :@request, :@response].each do |iv_name|
|
|
660
689
|
if !instance_variable_defined?(iv_name) || instance_variable_get(iv_name).nil?
|
|
661
690
|
raise "#{iv_name} is nil: make sure you set it in your test's setup method."
|
data/lib/action_controller.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# :markup: markdown
|
|
4
|
+
|
|
3
5
|
require "abstract_controller"
|
|
4
6
|
require "action_dispatch"
|
|
5
7
|
require "action_controller/deprecator"
|
|
6
8
|
require "action_controller/metal/strong_parameters"
|
|
7
9
|
require "action_controller/metal/exceptions"
|
|
8
10
|
|
|
9
|
-
#
|
|
11
|
+
# # Action Controller
|
|
10
12
|
#
|
|
11
13
|
# Action Controller is a module of Action Pack.
|
|
12
14
|
#
|
|
@@ -27,6 +29,7 @@ module ActionController
|
|
|
27
29
|
end
|
|
28
30
|
|
|
29
31
|
autoload_under "metal" do
|
|
32
|
+
autoload :AllowBrowser
|
|
30
33
|
autoload :ConditionalGet
|
|
31
34
|
autoload :ContentSecurityPolicy
|
|
32
35
|
autoload :Cookies
|
|
@@ -46,6 +49,7 @@ module ActionController
|
|
|
46
49
|
autoload :Logging
|
|
47
50
|
autoload :MimeResponds
|
|
48
51
|
autoload :ParamsWrapper
|
|
52
|
+
autoload :RateLimiting
|
|
49
53
|
autoload :Redirecting
|
|
50
54
|
autoload :Renderers
|
|
51
55
|
autoload :Rendering
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# :markup: markdown
|
|
4
|
+
|
|
3
5
|
require "rack/version"
|
|
4
6
|
|
|
5
7
|
module ActionDispatch
|
|
@@ -28,5 +30,11 @@ module ActionDispatch
|
|
|
28
30
|
SERVER_TIMING = "server-timing"
|
|
29
31
|
STRICT_TRANSPORT_SECURITY = "strict-transport-security"
|
|
30
32
|
end
|
|
33
|
+
|
|
34
|
+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
|
|
35
|
+
UNPROCESSABLE_CONTENT = :unprocessable_entity
|
|
36
|
+
else
|
|
37
|
+
UNPROCESSABLE_CONTENT = :unprocessable_content
|
|
38
|
+
end
|
|
31
39
|
end
|
|
32
40
|
end
|