Commit Graph

7247 Commits

Author SHA1 Message Date
Jonathan Hefner 9c3ffab47c Fix RateLimitingTest assertion for :by option
Follow-up to #50788.

This test wasn't actually asserting that the `:by` option worked.
2024-01-18 18:43:43 -06:00
Jean Boussier 4530a56e0f `RateLimiting` Cleanups
Followup: https://github.com/rails/rails/pull/50781

Some small issues I failed to address in the original PR.
2024-01-18 10:11:43 +01:00
Jean Boussier d839ddb71a Refactor `ActionController::RateLimiting` to use `AS::Cache`
Given that the limiter implementation provided by Kredis is a simple
increment with a limit, all `ActiveSupport::Cache` already provide that
same capability, with a wide range of backing stores, and not just Redis.

This even allow to use SolidCache has a backend if you so desire.

If we feel particularly fancy, we could also accept a more generic
limiter interface to better allow users to swap the implementation
for better algorithms such as leaky-bucket etc.
2024-01-17 15:40:49 +01:00
Eugene Kenny 0656787be6
Merge pull request #50764 from eugeneius/syntax_error_proxy_nil_backtrace_locations
Handle nil backtrace_locations in SyntaxErrorProxy
2024-01-16 14:20:13 +00:00
Jean Boussier 946e46ebcc Modernize method missing implementations
`...` is both simpler an more correct since the keyword argument
separation.
2024-01-16 13:17:45 +01:00
Eugene Kenny 16d1351a93 Handle nil backtrace_locations in SyntaxErrorProxy 2024-01-16 01:05:53 +00:00
Rafael Mendonça França a39332fa45
Remove code duplication and improve message 2024-01-15 22:29:46 +00:00
Hartley McGuire 0821d25d56
Add custom ArgumentError for invalid to: values
Previously, it was theoretically possible to define a route with a
Symbol as a `to:` value (or at least, it would not raise a
`NoMethodError`). However, passing a Symbol broke when `/#/.match?(to)`
was [replaced][1] with `to&.include?("#")` with the assumption that `to`
was always a String.

Instead of restoring the previous error, this commit improves how the
`to:` value is checked so that it raises an `ArgumentError` for any
invalid values. The extra strictness will specifically improve the error
when a Symbol or String that doesn't include a "#" are passed since they
were effectively equivalent to passing a `nil` value, or not specifying
`to:` at all.

[1]: 5726b1d1d7
2024-01-15 22:29:45 +00:00
Jean Boussier 2d6b02bad6
Merge pull request #50632 from skipkayhil/hm-move-chunk-test-to-actionpack
Move Transfer-Encoding chunked test to Action Pack
2024-01-09 14:35:50 +01:00
Rafael Mendonça França 84f773f9d1
Merge pull request #50622 from seanpdoyle/document-render-in-examples
Document rendering `:renderable` and `#render_in`
2024-01-08 16:51:39 -05:00
Hartley McGuire a03a854193
Move Transfer-Encoding chunked test to Action Pack
While working on [another PR][1], I found that removing the
Transfer-Encoding conditionals did not result in any failing tests in
Action Pack. This was surprising to me until I found that there was a
test for this behavior in Railties. However, nothing about the test
really depends on having a full Rails application or the Railties test
suite.

This commit moves the test into Action Pack to simplify/speedup the test
(no need to build a full app) as well as keeping the test closer to the
actual behavior being tested.

[1]: 0c334b48fdc5d70b0c8406ba184fbfd26750b049
2024-01-07 14:33:25 -05:00
koya1616 6fe2221225 Delete FIXME annotation in dispatch/mapper_test.rb
Delete `FIXME` in `actionpack/test/dispatch/mapper_test.rb` because it's fixed.
2024-01-07 21:43:41 +09:00
Sean Doyle 4117583e4b Document rendering `:renderable` and `#render_in`
Provide examples for rendering objects that respond to `render_in`. Also
highlight that the object can also define a `#format` method to control
how the rendered String should be treated.

Add test coverage for both Action View's and Action Pack's support for
`render` with `:renderable` options.
2024-01-06 17:57:02 -05:00
Rafael Mendonça França 8b4e92f4be
Point rubocop to ruby 3.1 2024-01-03 19:02:32 +00:00
Rafael Mendonça França 9d18dc8505
Remove all code to work with Ruby < 3.1 2024-01-03 19:02:31 +00:00
Hartley McGuire 90da071bb6
bundle update rubocop --conservative (#50515)
Also perform two autocorrects with `bundle exec rubocop -A`:

- fixes a new case of [`Style/RedundantReturn`][1]
- fixes a new case of [`Performance/StringInclude`][2]

[1]: 146b1c2e33
[2]: 3158bbb9f6

Co-authored-by: David Heinemeier Hansson <david@basecamp.com>
2024-01-02 12:49:36 +01:00
David Heinemeier Hansson e3da4fc53d
Add allow_browser to set minimum versions for your application (#50505)
* Add allow_browser to set minimum versions for your application
2023-12-31 19:19:16 +01:00
David Heinemeier Hansson 179b979ddb
Add rate limiting to Action Controller via the Kredis limiter type (#50490)
* Add rate limiting via the Kredis limiter type
2023-12-31 13:26:01 +01:00
Jonathan Hefner 76c26135b9 Fix inclusion of url_helpers module in concern
Follow-up to #46530.

The dynamically generated `url_helpers` module is an
`ActiveSupport::Concern`.  Therefore, when it is included directly in
another `ActiveSupport::Concern`, its `included` block is deferred until
the latter concern is itself included elsewhere.  Thus, in that case,
the call to `base._routes` in `def self.included(base)` will raise
`NoMethodError` because the `included` block will not yet have defined
the `_routes` method.

This commit prevents the error by first checking if `base` responds to
`_routes`.
2023-12-19 17:43:32 -06:00
Georg Ledermann 0fc5b06c08
Allow serving compressed SVG images
This adds `image/svg+xml` to the compressible content types
of ActionDispatch::Static

Co-authored-by: Mike Dalessio <mike.dalessio@gmail.com>
2023-12-14 11:38:07 -05:00
Eugene Kenny 8278626a2c
Merge pull request #49297 from hannahramadan/main
Add instrumentation for ActionController::Live#send_stream
2023-12-13 08:38:59 +00:00
hramadan 6488bd9ec0 Add instrumentation for ActionController::Live#send_stream
Allows subscribing to `send_stream` events.
The event payload contains the filename, disposition, and type.
2023-12-13 08:19:48 +00:00
Jean Boussier 572f4750f1
Merge pull request #47306 from zzak/re-47129
Follow up to HTTP::Request#route_uri_pattern
2023-12-11 18:39:05 +01:00
Jonathan Hefner be272a83a0 Fix system tests with Chrome cached by Selenium
Follow-up to #49908.

When Selenium resolves the driver path to a copy of Chrome that it has
downloaded / cached, it mutates the `Selenium::WebDriver::Chrome::Options`
object it receives, and relies on those changes later when the options
are used.  If `Selenium::WebDriver::Chrome::Service.driver_path` is set
but a different options object is used, Selenium will raise "cannot find
Chrome binary".  Therefore, this commit ensures that the options object
passed to `Selenium::WebDriver::DriverFinder.path` is the same options
object used by the driver later.
2023-12-07 12:52:51 -06:00
Mario Caropreso df6d2fbf2e Addressed an issue where syntax errors generated inside an eval
method cause an Internal Server Error due to a TypeError.

In order to display the extraced source of a syntax error, we try
to locate the node id for the backtrace location. The call to
RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location is expecting
a Thread::Backtrace::Location object, but we are passing a
SourceMapLocation instead.

This commit does two things:
1) it addresses the issue by making sure that we are always passing
a Thread::Backtrace::Location instead
2) it allows the development view to show the extracted source fragment
2023-12-05 06:40:19 +00:00
Hartley McGuire ec3d392e20
Fix skips in Action Pack
We can't run this test on Ruby 2.7 due to minitest being locked in the
Gemfile to an older version, so we should use that as a condition
instead of skipping if minitest doesn't have metadata.
2023-11-30 11:11:22 -05:00
Jean Boussier 4467b26a3e
Merge pull request #50145 from fatkodima/include-db-errors-in-connection-errors
Include exception causes into log messages
2023-11-28 20:45:13 +01:00
Gannon McGibbon d46d5ce610 Make with_routing test helper work for integration tests
Adds support for with_routing test helpers in ActionDispatch::IntegrationTest.
Previously, this helper didn't work in an integration context because
the rack app and integration session under test were not mutated.
Because controller tests are integration tests by default, we should
support test routes for these kinds of test cases as well.
2023-11-23 22:10:45 -06:00
fatkodima 4fc2df277d Include exception causes into log messages 2023-11-23 17:21:28 +02:00
Rafael Mendonça França ec2c2666c2
Remove deprecated support to set `Rails.application.config.action_dispatch.show_exceptions` to `true` and `false` 2023-11-22 22:12:10 +00:00
Rafael Mendonça França 30193dc087
Remove deprecated `speaker`, `vibrate`, and `vr` permissions policy directives 2023-11-22 22:12:09 +00:00
Rafael Mendonça França 43e42c1ea8
Remove deprecated comparison between `ActionController::Parameters` and `Hash` 2023-11-22 22:12:07 +00:00
Rafael Mendonça França 87bc1190fb
Remove deprecated constant `AbstractController::Helpers::MissingHelperError` 2023-11-22 22:12:03 +00:00
Rafael Mendonça França b2f96d07d4
Remove deprecated constant `ActionDispatch::IllegalStateError` 2023-11-22 22:11:43 +00:00
Matt Brictson e7d743b8ac Preload Selenium driver_path before parallelizing system tests
When the webdrivers gem is not present (which is the default scenario in
Rails 7.1+), the Selenium `driver_path` starts out as `nil`. This means
the driver is located lazily, and deferred until a system test is run.

If parallel testing is used, this leads to a race condition, where each
worker process tries to resolve the driver simultaneously. The result is
an error as described in #49906.

This commit fixes the race condition by changing the implementation of
`Browser#preload`. The previous implementation worked when `driver_path`
was set to a Proc by the `webdrivers` gem, but doesn't work when the
`webdrivers` gem is not being used and the `driver_path` is `nil`.

`Browser#preload` now uses the `DriverFinder` utility provided by the
`selenium-webdriver` gem to eagerly resolve the driver path if needed.
This will ensures that `driver_path` is set before parallel test workers
are forked.

Fixes #49906.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-07 11:15:18 -06:00
fatkodima 97ae6e7bce Fix `StrongParameters#extract_value` to include blank values
Co-authored-by: Yasha Krasnou <yakau@hey.com>
Co-authored-by: Matthias Eiglsperger <38067299+mateigl@users.noreply.github.com>
2023-10-23 23:14:49 +03:00
Nikita Vasilevsky 19f8ab2e7d
[Tests only] Enable `Minitest/AssertPredicate` rule 2023-10-13 19:26:47 +00:00
zzak 6635543d51
Support handling Enumerator for non-buffered responses
The downside to this is that we cannot generate ETags for these types of responses, but are assuming that by using an enumerator they don't expect a buffered response to be cacheable. This means you cannot use Enumerator to generate streaming responses.

Fixes #49588

See also: #47092

Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
2023-10-13 11:12:16 +09:00
fatkodima b8829cabec Enable `Style/RedundantDoubleSplatHashBraces` rubocop cop 2023-10-11 14:55:00 +03:00
Petrik 9c96042690 Improve instrumentation tests
Don't build AS::Notifications::Event manually, similar to 95b6fbd00f
Also assert that options get passed.
2023-10-03 13:14:37 +02:00
Jean Boussier 02e679ba75 Get rid of the `jruby_skip` test helper
The last test calling it actually passes on latest
JRuby.
2023-10-02 13:01:44 +02:00
Rafael Mendonça França 7c303b9ddf
Merge pull request #49442 from p8/actionpack/notifications-tests
Add tests for send_file and redirect_to instrumentation
2023-10-01 17:30:00 -04:00
Petrik 989de534a5 Add tests for send_file and redirect_to instrumentation 2023-09-30 17:22:56 +02:00
Bart de Water 95b6fbd00f Stop building AS::Notifications::Event manually
It's possible since Rails 6 (3ea2857943) to let the framework create Event objects, but the guides and docs weren't updated to lead with this example.

Manually instantiating an Event doesn't record CPU time and allocations, I've seen it more than once that people copy-pasting the example code get confused about these stats returning 0. The tests here show that - just like the apps I've worked on - the old pattern keeps getting copy-pasted.
2023-09-29 12:34:23 -04:00
Sean Doyle 1d999e681e Support `ActionController::Parameters#deep_merge`
When [rails/rails#20868][] changed the `ActionController::Parameters`
ancestory from `HashWithIndifferentAccess` to `Object`, support for
`#deep_merge` and `#deep_merge!` were omitted.

This commit restores support by integrating with
[ActiveSupport::DeepMergeable](./activesupport/lib/active_support/deep_mergeable.rb).

[rails/rails#20868]: https://github.com/rails/rails/pull/20868

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-26 16:27:28 -05:00
Shouichi Kamiya 51ac8b9f6f Enable Minitest/LiteralAsActualArgument
There are assertions that expected/actual arguments are passed in the
reversed order by mistake. Enabling the LiteralAsActualArgument rule
prevents this mistake from happening.

The existing tests were auto-corrected by rubocop with a bit of
indentation adjustment.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-13 10:09:32 +09:00
Rafael Mendonça França ed0c34d1e0
Merge pull request #48699 from kamil-gwozdz/fix-action_methods_with_inherited_shadowed_internal_method-v2
AbstractController#internal_methods: ignore action_methods
2023-09-09 16:55:26 -04:00
Alex Ghiculescu ff6881d2b2
Remove old `raise_on_missing_translations` behaviour
ref: https://github.com/rails/rails/pull/47105#issuecomment-1400843060

Removes the old `raise_on_missing_translations` accessors, that used to live [here](fee61e3abc/actionpack/lib/abstract_controller/translation.rb (L7)) and [here](5c835bd669/actionview/lib/action_view/helpers/translation_helper.rb (L15)).

Closes https://github.com/rails/rails/pull/45361
2023-09-09 19:59:49 +00:00
euglena1215 9d2a7f89ee
Prevent `NoMethodError` in extract_value when specifying non-existent keys
Introduced in https://github.com/rails/rails/pull/49042, the method `ActionController::Parameters#extract_value` promises to replace utility methods that were previously defined as private methods in controllers.
However, it currently throws a `NoMethodError` when passed a non-existent key.

`params` is dependent on client requests and is thus beyond the application's control.
Rather than throwing a `NoMethodError`, it would be more convenient for the method to return `nil`.
2023-08-31 22:31:24 +09:00
Nikita Vasilevsky da7a6da4e7
Add `extract_value` method to `ActionController::Parameters`
This commit adds `extract_value` method to `ActionController::Parameters`
as a primary way to extract composite `id` values serialized from
`ActiveRecord::Base#to_param` called on a model with a composite primary key.
2023-08-25 22:34:10 +00:00