Commit Graph

15828 Commits

Author SHA1 Message Date
Petrik
989de534a5 Add tests for send_file and redirect_to instrumentation 2023-09-30 17:22:56 +02:00
Hartley McGuire
3946ef20e1
Rm unused default_protect_from_forgery accessor
This was [added][1] when the default configuration was added for Rails
5.2, however the accessor itself has never been documented or used.
`protect_from_forgery: :exception` is added based on  whether the
configuration is set on `config.action_controller` and not this value.

Since the accessor is undocumented and unused, this commit removes it.

[1]: 48cb8b3e7097e9a1cb45b2298f59b9179f0dbdee
2023-09-28 20:38:44 -04:00
fatkodima
7ef86b6a49 Enable Lint/RedundantSafeNavigation rubocop cop 2023-09-27 14:55:07 +03:00
Rafael Mendonça França
fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França
e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00: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
Hartley McGuire
35b280fcc2
Refactor Router#find_routes to be lazier
Previously, `#find_routes` would take all of the routes that match the
current request and eagerly generate `MatchData` and `path_parameters`
for each route.

This commit changes `#find_routes` to only perform the computation one
route at a time, since the computation will never be needed for routes
in the list after `#serve` returns.

This change improves the performance of `RouteSet#call` by ~10% when
`#find_routes` finds two routes, and ~60% when `#find_routes` finds ten
routes.

Before:

```
Warming up --------------------------------------
  10 matching routes     1.182k i/100ms
   2 matching routes     1.967k i/100ms
   1 matching routes     2.221k i/100ms
Calculating -------------------------------------
  10 matching routes     11.846k (± 3.7%) i/s -     60.282k in   5.095922s
   2 matching routes     19.871k (± 3.5%) i/s -    100.317k in   5.054796s
   1 matching routes     21.904k (± 3.8%) i/s -    111.050k in   5.077449s

Comparison:
   1 matching routes:    21904.0 i/s
   2 matching routes:    19870.6 i/s - 1.10x  slower
  10 matching routes:    11845.9 i/s - 1.85x  slower
```

After:

```
Warming up --------------------------------------
  10 matching routes     1.888k i/100ms
   2 matching routes     2.215k i/100ms
   1 matching routes     2.312k i/100ms
Calculating -------------------------------------
  10 matching routes     18.623k (± 3.7%) i/s -     94.400k in   5.076043s
   2 matching routes     22.210k (± 3.6%) i/s -    112.965k in   5.092873s
   1 matching routes     22.953k (± 4.1%) i/s -    115.600k in   5.045017s

Comparison:
   1 matching routes:    22952.9 i/s
   2 matching routes:    22210.4 i/s - same-ish: difference falls within error
  10 matching routes:    18622.8 i/s - 1.23x  slower
```

Benchmark:

```
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "actionpack", path: "~/src/github.com/skipkayhil/rails"
  gem "benchmark-ips"
end

require "action_dispatch"

routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
  get "/a", to: ->(e) { [200, {}, ["a"]] }

  2.times do |i|
    is = "b" + i.to_s
    get "/b", to: ->(e) { [200, {}, [is]] }
  end

  10.times do |i|
    is = "c" + i.to_s
    get "/c", to: ->(e) { [200, {}, [is]] }
  end
end

one_env = {
  "REQUEST_METHOD" => "GET",
  "SCRIPT_NAME" => "",
  "rack.input" => File.open("/dev/null"),
  "PATH_INFO" => "/a",
}.freeze

two_env = {
  "REQUEST_METHOD" => "GET",
  "SCRIPT_NAME" => "",
  "rack.input" => File.open("/dev/null"),
  "PATH_INFO" => "/b",
}.freeze

ten_env = {
  "REQUEST_METHOD" => "GET",
  "SCRIPT_NAME" => "",
  "rack.input" => File.open("/dev/null"),
  "PATH_INFO" => "/c",
}.freeze

raise unless routes.call(one_env.dup)[2] == ["a"]
raise unless routes.call(two_env.dup)[2] == ["b0"]
raise unless routes.call(ten_env.dup)[2] == ["c0"]

require "benchmark/ips"

Benchmark.ips do |x|
  x.report("10 matching routes") { routes.call(ten_env.dup) }
  x.report("2 matching routes") { routes.call(two_env.dup) }
  x.report("1 matching routes") { routes.call(one_env.dup) }
  x.compare!
end
```
2023-09-20 17:25:11 -04: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
699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00: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
Rafael Mendonça França
f2b1ff4eec
Bump dynamic controller and action segments yet again 2023-09-09 19:30:53 +00:00
Akhil G Krishnan
61a9c1a92a Ruby code block indentation issue fix
[skip ci] indentation fix

[skip ci] review changes added

[skip ci] indentation fix
2023-09-07 01:14:48 +05:30
Ryuta Kamizono
5415d3a19a Remove "proc to update web drivers." [ci-skip]
Follow-up to #48847.

This was originally "The webdrivers gem uses this proc to update web
drivers.".
2023-09-06 13:23:14 +09: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
Rafael Mendonça França
4df85d7089
Merge pull request #49029 from technicalpickles/abstractcontroller_base-action_methods-performance
Improve performance of AbstractController::Base#action_methods
2023-08-24 11:41:04 -04:00
Rafael Mendonça França
dc3da021ee
Fix comments and indentation 2023-08-24 15:22:34 +00:00
Josh Nichols
b2afa8b3fb
Improve performance of AbstractController::Base#action_methods
I was benchmarking some specs in my app, and saw this code come up in the
memory_profiler. It is by no means the biggest memory allocation, but it
is straightforward to make a slight improvement.

Primarily, this saves allocating one array by using concat instead of +
to add public_instance_methods(false).

That ends up being 10% less memory for my benchmark (3 actions), and 6%
faster.

```
Calculating -------------------------------------
            original     8.352k memsize (   208.000  retained)
                        22.000  objects (     2.000  retained)
                         6.000  strings (     0.000  retained)
         refactored3     7.616k memsize (   408.000  retained)
                        11.000  objects (     7.000  retained)
                         3.000  strings (     3.000  retained)

Comparison:
         refactored3:       7616 allocated
            original:       8352 allocated - 1.10x more
Warming up --------------------------------------
            original     2.326k i/100ms
         refactored3     2.441k i/100ms
Calculating -------------------------------------
            original     23.336k (± 0.7%) i/s -    118.626k in   5.083658s
         refactored3     24.692k (± 1.2%) i/s -    124.491k in   5.042345s

Comparison:
            original:    23336.0 i/s
         refactored3:    24692.5 i/s - 1.06x  faster
```

Benchmark and results are also posted to https://gist.github.com/technicalpickles/4a4ae6a9e2c42963af43a89f75e768fe
2023-08-24 09:30:50 -04:00
Jean Boussier
6beb15c04a Add some :nodoc: to SyntaxErrorProxy and friends
None of this is public API.
2023-08-24 09:52:18 +02:00
Rafael Mendonça França
818bfc269a
Remove deprecation messages from actionpack test suite
The 6.1 cache format that is the default is deprecated.
2023-08-23 18:43:35 +00:00
Sean Doyle
0f4ab82082 Ensure response.parsed_body support for pattern matching
Both `Nokogiri` and `Minitest` have merged the PRs mentioned to
integrate support for Ruby's Pattern matching
(https://github.com/sparklemotion/nokogiri/pull/2523 and
https://github.com/minitest/minitest/pull/936, respectively).

This commit adds coverage for those new assertions, and incorporates
examples into the documentation for the `response.parsed_body` method.

In order to incorporate pattern-matching support for JSON responses,
this commit changes the response parser to call `JSON.parse` with
[object_class: ActiveSupport::HashWithIndifferentAccess][object_class],
since String instances for `Hash` keys are incompatible with Ruby's
syntactically pattern matching.

For example:

```ruby
irb(main):001:0> json = {"key" => "value"}
=> {"key"=>"value"}
irb(main):002:0> json in {key: /value/}
=> false

irb(main):001:0> json = {"key" => "value"}
=> {"key"=>"value"}
irb(main):002:0> json in {"key" => /value/}
.../3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/lib/irb/workspace.rb:113:in `eval': (irb):2: syntax error, unexpected terminator, expecting literal content or tSTRING_DBEG or tSTRING_DVAR or tLABEL_END (SyntaxError)
json in {"key" => /value/}
             ^

        .../ruby/3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/exe/irb:9:in `<top (required)>'
        .../ruby/3.2.0/bin/irb:25:in `load'
        .../ruby/3.2.0/bin/irb:25:in `<main>'
```

When the Hash maps String keys to Symbol keys, it's able to be pattern
matched:

```ruby
irb(main):005:0> json = {"key" => "value"}.with_indifferent_access
=> {"key"=>"value"}
irb(main):006:0> json in {key: /value/}
=> true
```

[object_class]: https://docs.ruby-lang.org/en/3.2/JSON.html#module-JSON-label-Parsing+Options
2023-08-23 13:28:02 -04:00
Rafael Mendonça França
24876e6a79
Merge PR #48950 2023-08-21 19:27:53 +00:00
Rafael Mendonça França
3642668935
Merge pull request #48941 from skipkayhil/hm-show-correct-blocked-hosts
Fix host display when X_FORWARDED_HOST authorized
2023-08-21 15:05:44 -04:00
Guillermo Iguaran
5a21634407
Merge pull request #48956 from skipkayhil/hm-rack-lint-show-exceptions-failsafe
Add test coverage for ShowExceptions failsafe
2023-08-18 16:08:41 -07:00
Guillermo Iguaran
4ec3a986d5
Merge pull request #48959 from skipkayhil/hm-clean-filters-requires
Remove uneeded requires of core_ext/string/filters
2023-08-18 16:03:44 -07:00
Rafael Mendonça França
c82d624ea1
Fix documentation of with_routing in the class level
There is no assertions inside it.
2023-08-18 22:21:06 +00:00
Rafael Mendonça França
08cd280866
Use class method module instead of DSL
This will make sure documentation is generated for the module.
2023-08-18 22:11:48 +00:00
Hartley McGuire
ff6e885d59
Remove uneeded requires of core_ext/string/filters
`actionpack/lib/action_dispatch/routing.rb`
- added: 013745151be062aa4d0fc1f2a008a7303fdb6e04
- removed: 93034ad7fea7e00562103a7cd0acfab19bbfadf9

`activejob/lib/active_job/log_subscriber.rb`
- added: b314ab555e0d85e6efb41be94fb5f3a157bb12fe
- removed: 5ab2034730feacfc2caee418f8c0b55191d27427

`activemodel/lib/active_model/errors.rb`
- added: cf7fac7e29bb2816412c949fdaed3d61a923eb23
- removed: 9de6457ab0767ebab7f2c8bc583420fda072e2bd

`activerecord/lib/active_record/core.rb`
- added: b3bfa361c503e107aff4dee5edf79bd7fd3d3725
- removed: e1066f450d1a99c9a0b4d786b202e2ca82a4c3b3

`activesupport/lib/active_support/core_ext/module/introspection.rb`
- added: 358ac36edf1695fcbec0aa21f126a3d8b83d4b5a
- removed: 167b4153cac0069a21e0bb9689cb16f34f6abbaa

`activesupport/lib/active_support/duration.rb`
- added: 75924c4517c8f87712d3f59c11f10152ed57b9d8
- removed: a91ea1d51048342d13fc73f9b09ce4cfd086bb34

`railties/lib/rails/commands/server/server_command.rb`
- added: f2173648938b418d120f5a68d8f3862d8ae9dace
- removed: 553b86fc751c751db504bcbe2d033eb2bb5b6a0b

`railties/lib/rails/command/base.rb`
- added: 6813edc7d926965e5644cd8befaf229a35b9d8ca
- removed: b617a561d865a65cfc140caa0e3c4af4350bfcef
2023-08-16 17:39:25 -04:00
Hartley McGuire
6bf2ee09f7
Add test coverage for ShowExceptions failsafe
This adds additional test coverage to ShowExceptions, since one of the
possible responses it creates was not previously tested. Because of the
previous [addition][1] of Rack::Lint, this also demonstrates that the
Content-Type header needed to be fixed.

[1]: 339dda4a82356d173b62dab144870790618e40c6
2023-08-16 16:00:25 -04:00
Akira Matsuda
5cf742ef51
ERB is no longer in use here since c2e756a944fd3ca2efa58bd285c0e75e0b4794ab 2023-08-17 04:46:35 +09:00
Yuki Nishijima
72c5270e45 Add support for Playwright as a driver for system tests 2023-08-16 16:28:28 +09:00
Hartley McGuire
11ef3cecf0
Fix host display when X_FORWARDED_HOST authorized
Previously, when a Request had a non-authorized HTTP_HOST but an
authorized HTTP_X_FORWARDED_HOST, the HTTP_X_FORWARDED_HOST value would
be displayed as the one being blocked. However, this could be confusing
for users since that value would already be added to `config.hosts`.

This commit addresses the issue by tweaking how the blocked host is
displayed. Instead of always displaying Request#host (which will return
X_FORWARDED_HOST when present whether or not that's the host being
blocked), each host being blocked will be displayed on its own.

Co-authored-by: Daniel Schlosser <Eusebius1920@users.noreply.github.com>
2023-08-15 03:27:13 -04:00
Matija Čupić
eed1ac1782
Use relative path for screenshot metadata 2023-08-05 15:20:01 +02:00
Rafael Mendonça França
2df0e5fa0c
Merge pull request #48857 from seanpdoyle/fixture-file-upload-rename
Rename `fixture_file_upload` method to `file_fixture_upload`
2023-08-04 17:08:40 -04:00
Sean Doyle
6cafc49d2c Rename fixture_file_upload method to file_fixture_upload
The naming difference between the test harness' [file_fixture][] helper
made available through Active Support (along with the
`file_fixture_path` configuration value) and the integration test
harness' [fixture_file_upload][] is a constant source of confusion and
surprise.

Since Active Support is more ubiquitous, this commit renames the
`fixture_file_upload` method to `file_fixture_upload` to match the order
of words in `file_fixture` and `file_fixture_path`.

To preserve backwards compatibility, declare a `fixture_file_upload`
alias to be preserved into the future (or removed at a future point in
time).

[file_fixture]: https://edgeapi.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture
[fixture_file_upload]: https://edgeapi.rubyonrails.org/classes/ActionDispatch/TestProcess/FixtureFile.html#method-i-fixture_file_upload
2023-08-04 12:40:17 -04:00
Bryan Traywick
a4633725d2 Fix NoMethodError when request Content-Type is blank. 2023-08-04 12:36:11 -04:00
Mike Dalessio
8a57ba8c0e
Update Action View and Dispatch to use Rails::Dom::Testing helpers
Use the helpers introduced in rails-dom-testing 2.2.0 instead of
managing the HTML parsers as was done in #48523.

See also related #47144 / ad79ed0e
2023-08-03 11:17:38 -04:00
Jean Boussier
ee3117ba8a
Merge pull request #48863 from matteeyah/main
Save screenshot path on system test failure
2023-08-03 16:35:29 +02:00
Eugene Kenny
797a7c5a62 Fix typos in RequestForgeryProtection docs [ci-skip] 2023-08-03 13:58:26 +01:00
Matija Čupić
a962fc3c9c
Save failure screenshot path in test metadata 2023-08-03 14:43:49 +02:00
Rafael Mendonça França
e980f158a4
Merge pull request #48847 from seanpdoyle/omit-webdrivers-from-gemfile-template
Omit `webdrivers` gem from `Gemfile` template
2023-08-02 13:49:28 -04:00
Guillermo Iguaran
4c9a990ffd
Merge pull request #48855 from akhilgkrishnan/add-rack-link-to-debug-exception-missing-test
Add Rack::Lint to DebugExceptions missing test
2023-08-01 10:38:33 -07:00
Sean Doyle
9a53234695 Omit webdrivers gem from Gemfile template
As of Selenium 4.6, [the Selenium Manager is capable of managing Chrome
Driver installations and integrations][readme]. As of Selenium 4.11, the
Selenium Manager is capable of [capable of resolving the Chrome for
Testing installation][] path.

By omitting the `gem` declaration from the `Gemfile.tt`, newly generated
applications and applications updating their `Gemfile` in lockstep with
newer Rails versions can shed the dependency and avoid test failures
introduced by newly released Chrome versions (like, for example,
[titusfortner/webdrivers#247][]).

[readme]: 43f8ac436c (update-selenium-manager)
[titusfortner/webdrivers#247]: https://github.com/titusfortner/webdrivers/issues/247
[capable of resolving the Chrome for Testing installation]: https://github.com/rails/rails/pull/48847#issuecomment-1656756862

Co-authored-by: Titus Fortner <titusfortner@users.noreply.github.com>
2023-08-01 09:22:08 -04:00
Adrianna Chang
339dda4a82
Add Rack::Lint to ActionDispatch::ShowExceptions tests
This wraps test coverage for `ActionDispatch::ShowExpections` in
`Rack::Lint` middleware in order to validate that both
`ActionDispatch::ShowExceptions` and `ActionDispatch::PublicExceptions`
conform to the Rack SPEC.

It also ensures that the response headers returned by the *Exceptions
middleware respect casing (mixed case for Rack 2, lower case for Rack 3)
2023-08-01 09:18:59 -04:00
Nuno Silva
613e8fd0a1
Add Rack::Lint to ActionDispatch::RemoteIp tests
To ensure Rails is and remains compliant with [the Rack 3
spec](6d16306192/UPGRADE-GUIDE.md)
we can add `Rack::Lint` to the Rails middleware tests.

This adds additional test coverage to
`ActionDispatch::RemoteIp` to validate that its input and
output follow the Rack SPEC.

The only code testing this middleware are the ones for
`ActionDispatch::Request`.

Several changes were required to make the tests pass:

- `CONTENT_LENGTH` must be a string
- `SERVER_PORT` must be a string
- `HTTP_HOST` must be a string
- `rack.input` must be an IO object, with ASCII-8BIT encoding
     - By leveraging `Rack::MockRequest`, we can pass the symbol :input,
       and the string value, and it will be converted to an IO object
       with the correct encoding.
     - See [definition here](444dc8a130/lib/rack/mock_request.rb (L89-L97))
- using `Rack::MockRequest` also means that any symbol keys being passed
to setup the env, will be discarded. [Only string keys are copied.]444dc8a130/lib/rack/mock_request.rb (L156)
2023-07-31 08:13:04 +00:00
Akhil G Krishnan
7df276f938 Add Rack::Lint to DebugExceptions missing test 2023-07-31 13:19:32 +05:30
Guillermo Iguaran
ad790cb2f6
Merge pull request #48837 from skipkayhil/hm-rack-lint-debug-exceptions
Add Rack::Lint to DebugExceptions tests
2023-07-30 23:58:34 -07:00
Hartley McGuire
ac7ee278fd
Add Rack::Lint to DebugExceptions tests
This adds additional test coverage to DebugExceptions to validate that
its behavior conforms to the Rack SPEC.

The only changes necessary were to use dynamic header casing for
Content-Type and Content-Length
2023-07-29 16:49:10 -04:00
Akhil G Krishnan
15bca6e94d Add Rack::Lint to ActionDispatch::Reloader tests 2023-07-29 14:18:49 +05:30