Commit Graph

7126 Commits

Author SHA1 Message Date
Rafael Mendonça França
61accb7dea
Merge pull request #45867 from jdufresne/show-rescuable-exceptions
Make the test environment show rescuable exceptions in responses
2023-05-24 13:45:45 -04:00
Rafael Mendonça França
5e34a9297b
Test behavior in unit test 2023-05-24 17:01:38 +00:00
Zeke Gabrielse
7d8cb154d4 Fix issue with empty values within delimited authorization header
When the Authorization header would contain a set of delimited values
where one or more values were blank, an ArgumentError would be raised.
This resolves that by removing blank values during parsing of the
Authorization header.
2023-05-22 15:27:03 -05:00
Jon Dufresne
e28f147329 Make the test environment show rescuable exceptions in responses
Background
----------

During integration tests, it is desirable for the application to respond
as closely as possible to the way it would in production. This improves
confidence that the application behavior acts as it should.

In Rails tests, one major mismatch between the test and production
environments is that exceptions raised during an HTTP request (e.g.
`ActiveRecord::RecordNotFound`) are re-raised within the test rather
than rescued and then converted to a 404 response.

Setting `config.action_dispatch.show_exceptions` to `true` will make the
test environment act like production, however, when an unexpected
internal server error occurs, the test will be left with a opaque 500
response rather than presenting a useful stack trace. This makes
debugging more difficult.

This leaves the developer with choosing between higher quality
integration tests or an improved debugging experience on a failure.

I propose that we can achieve both.

Solution
--------

Change the configuration option `config.action_dispatch.show_exceptions`
from a boolean to one of 3 values: `:all`, `:rescuable`, `:none`. The
values `:all` and `:none` behaves the same as the previous `true` and
`false` respectively. What was previously `true` (now `:all`) continues
to be the default for non-test environments.

The new `:rescuable` value is the new default for the test environment.
It will show exceptions in the response only for rescuable exceptions as
defined by `ActionDispatch::ExceptionWrapper.rescue_responses`. In the
event of an unexpected internal server error, the exception that caused
the error will still be raised within the test so as to provide a useful
stack trace and a good debugging experience.
2023-05-17 06:30:28 -07:00
Igor S. Morozov
300b8e32c2
Content Security Policy: add :unsafe_hashes mapping 2023-05-13 01:48:13 +03:00
Joé Dupuis
4fd27842b8 Remove redundant protocol test for url_for
test_protocol_with_and_without_separators already has the same assertion.
2023-05-11 21:15:03 -07:00
Eileen M. Uchitelle
1769487bc2
Merge pull request #48123 from cpjmcquillan/cm/remove-redundant-test-setup
Remove redundant test
2023-05-09 09:28:36 -04:00
Connor McQuillan
db8f573ada
Remove redundant test
This test was introduced in #19904.

In #21368 a bunch of test setup was removed, but the assignment
of `@set` was duplicated.

Removing the extraneous test setup means the test is identical to the
`test_cart_inspect` test.

This removes the test entirely.
2023-05-09 08:33:35 +01:00
Jonathan Hefner
c924ba0df9 Support :message_pack as cookies serializer
This commit adds support for `:message_pack` and `:message_pack_allow_marshal`
as serializers for `config.action_dispatch.cookies_serializer`, just
like `config.active_support.message_serializer`.

The `:message_pack` serializer can fall back to deserializing with
`AS::JSON`, and the `:message_pack_allow_marshal` serializer can fall
back to deserializing with `AS::JSON` or `Marshal`.  Additionally, the
`:marshal`, `:json`, and `:hybrid` / `:json_allow_marshal` serializers
can now fall back to deserializing with `AS::MessagePack`.  These
behaviors make it easier to migrate between cookies serializers.
2023-05-08 15:00:08 -05:00
Guillermo Iguaran
06c219710a
Merge pull request #48148 from tindron/arbitrary-hash-arrays
[Fix #38698] Fix nested Arrays when permitting arbitrary Hashes
2023-05-07 22:42:26 -07:00
Joe Dupuis
5216400529 Fix body stream test on Rack 3.0 by rewinding the body stream.
Since 3.0, Rack doesn't guarantee rewindable request body streams.
Therefore Rack doesn't rewind the body after parsing the POST params
like it use to.
Since this is a test request, we can guarantee the stream is rewindable
and do it in the test.
2023-05-07 17:14:18 -07:00
Randy Stoller
8ed86b7e21
[Fix #38698] Fix nested Arrays when permitting arbitrary Hashes 2023-05-06 13:09:03 -07:00
Gareth Adams
8f3c6a1005 Make Rails cookies RFC6265-compliant with domain: :all
Rails has incorrectly been adding leading dots to cookie domain values
when the `domain: :all` option is present.

This leading dot was required in cookies based on [RFC 2965][rfc2965]
(October 2000), but [RFC 6265][rfc6265] (April 2011) changed that
behaviour, making a leading dot strictly incorrect. Todays browsers aim
to confirm to RFC6265 with repect to cookies.

The new behaviour is that *any* cookie with an explicitly passed domain
is sent to all matching subdomains[[ref][mdn]]. For a server to indicate
that only the exact origin server should receive the cookie, it should
instead pass *no* domain attribute.

Despite the change in behaviour, browser devtools often display a cookie
domain with a leading dot to indicate that it is valid for subdomains -
this prefixed domain is *not* necessarily the raw value that was passed
in the Set-Cookie header. This explains why it's a common belief among
developers that the leading dot is required.

RFC6265 standard gives UAs an algorithm to handle old-style cookie
domain parameters (they can drop a leading dot if present), so it's
unlikely that this error would ever have had any effect on web browsers.

However, cookies generated this way can't be processed by Ruby's own
CGI::Cookie class:

> CGI::Cookie.new "domain" => ".foo.bar", "name" => "foo"
ArgumentError: invalid domain: ".foo.bar"

Newer versions of the Ruby CGI library accomodate the same fallback
behaviour (dropping the extra dot) but this isn't a justification for it
being the right way to set a cookie.

[mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#domain_attribute
[rfc2965]: https://www.rfc-editor.org/rfc/rfc2965#section-3.2
[rfc6265]: https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1
2023-04-24 01:38:34 +01:00
Daniel Colson
a00e548da7
Print source location when inspecting routes
In larger route files, or when routes are spread across multiple files,
it can be difficult to get from the output of the route inspector to the
relevant route definition.

This commit adds a route source location to the route, and uses that in
the HtmlTableFormatter (for rails/info and the debug exceptions
middleware) and the Expanded formatter (for `rails routes -E`).

To avoid doing extra work in production, it only sets the source location
in development.

This commit injects the application's backtrace cleaner so we can use it
to remove the rails root from the path. This also means we don't get
source locations for the routes defined by Rails.

If mounting an engine from a gem, we'll get a source location for where
we mount it in the application, but not for the routes defined in the
gem itself. That's probably good enough, since Rails already prints
routes for an engine separately under the title "Routes for
Foo::Engine".

Co-authored-by: John Hawthorn <jhawthorn@github.com>
Co-authored-by: Luan Vieira <luanzeba@github.com>
Co-authored-by: Daniel Colson <composerinteralia@github.com>
2023-04-10 09:52:27 -04:00
Hidde-Jan Jongsma
603b5cdde5 Alias ActionController::Parameters except as without 2023-03-29 10:56:58 +02:00
Andrew Novoselac
6902cbce1b Introducs TestFixtures#fixture_paths.
Multiple fixture paths can now be specified using the `#fixture_paths` accessor.
2023-03-14 19:02:56 -04:00
Ron Shinall
45547e1ee7 Fix #47535 - flag multiple cookies as secure 2023-03-08 12:29:26 -05:00
Rafael Mendonça França
696ccbc265
Remove deprecated poltergeist and webkit (capybara-webkit) driver registration for system testing 2023-03-03 00:38:23 +00:00
Rafael Mendonça França
689b277733
Remove deprecated behavior on Request#content_type 2023-03-03 00:38:21 +00:00
Matthew Draper
e06844f69b Expect Static to set lowercase values in Vary 2023-02-26 19:36:29 +10:30
Gannon McGibbon
3f4a7734da Set ORIGINAL_FULLPATH header in controller tests
Since rails/rails#47296, nothing sets the fullpath early, so changing
the path of a request, and then calling original_fullpath returns the
updated fullpath. This is a controller testing specific bug as
integration tests and real requests always have this header set, so I
think controller tests should too.
2023-02-22 00:33:09 -06:00
Aaron Patterson
218ec10df8
Merge pull request #47092 from ioquatix/rack-3-streaming-body
Update `ActionDispatch::Response` to support streaming bodies.
2023-02-14 17:01:45 -08:00
Aaron Patterson
e89b2e4271
Merge pull request #47147 from ioquatix/rack-3-headers-name-normalization
Normalize usage of "Headers".
2023-02-14 17:01:05 -08:00
Rafael Mendonça França
aa3fc0fca2
Merge pull request #47296 from ritikesh/filtered_path
use filtered_path in action_controller event payloads instead of fullpath
2023-02-08 13:29:10 -05:00
Ole Friis Østergaard
1d3edd1c84 Fix regression in ActionDispatch::Routing::RouteSet#recognize_path 2023-02-08 10:49:29 +00:00
Ritikesh G
c59ce2cf71
use filtered_path in action_controller event payloads instead of fullpath 2023-02-08 13:28:32 +05:30
Eileen M. Uchitelle
3cca0d5205
Merge pull request #47200 from skipkayhil/use-ad-deprecator-illegal-state
Use AD.deprecator for IllegalStateError
2023-01-31 08:48:59 -05:00
Hartley McGuire
d1689ef8eb
Use AD.deprecator for IllegalStateError
Previously, ActionDispatch::IllegalStateError was deprecated using
Module#deprecate_constant in 0b4b4c6b96a41ef649f15e1a3df26e28ef95ff24.
This requires the -w flag to be used to actually see the deprecation
warning, and it can not be controlled using ActiveSupport::Deprecator
configuration.

This commit changes the deprecation to use #deprecate_constant from
ActiveSupport::Deprecation::DeprecatedConstantAccessor. This ensures
that the deprecation warning will be printed even without -w, and the
warning can be controlled by configuring ActionDispatch.deprecator
2023-01-31 03:16:20 -05:00
Hartley McGuire
ada584bcb8
Deprecate AbstractController MissingHelperError
This error used to be a wrapper for a LoadError raised when
require_dependency was used to load helpers for controllers.

Since Zeitwerk does not use require_dependency, the only usage of the
error was removed in 5b28a0e972da31da570ed24be505ef7958ab4b5e.
2023-01-31 02:49:21 -05:00
Sean Doyle
ad79ed0e6b ActionDispatch::Testing::TestResponse#parsed_body parse HTML with Nokogiri
Prior to this commit, the only out-of-the-box parsing that
`ActionDispatch::Testing::TestResponse#parsed_body` supported was for
`application/json` requests. This meant that `response.body ==
response.parsed_body` for HTML requests.

```ruby
get "/posts"
response.content_type         # => "text/html; charset=utf-8"
response.parsed_body.class    # => Nokogiri::HTML5::Document
response.parsed_body.to_html  # => "<!DOCTYPE html>\n<html>\n..."
```

Using `parsed_body` for JSON requests supports `Hash#fetch`, `Hash#dig`,
and Ruby 3.2 destructuring assignment and pattern matching.

The introduction of [Nokogiri support for pattern
matching][nokogiri-pattern-matching] poses an opportunity to make assertions
about the structure of the HTML response.

On top of that, there is ongoing work to [introduce pattern matching
support in MiniTest][minitest-pattern-matching].

[nokogiri-pattern-matching]: https://github.com/sparklemotion/nokogiri/pull/2523
[minitest-pattern-matching]: https://github.com/minitest/minitest/pull/936
2023-01-27 23:52:21 -05:00
Samuel Williams
a3caa2b551
Normalize usage of "Headers". 2023-01-26 12:06:24 +13:00
Rafael Mendonça França
2cf8740e15
Merge pull request #47091 from ioquatix/rack-3-case-insensitive-headers
Use Rack's own headers classes where appropriate.
2023-01-25 17:22:47 -05:00
Joel Hawksley
2fc46f95a5
Tag HTTP::Request with route URI pattern (#47129)
In the GitHub RoR monolith, we output the route URI pattern
in an HTML meta tag in our application layout for analysis
purposes. However, our current implementation is quite manual.

This change adds an attribute to requests with the URI pattern
of the matched route.

Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
Co-authored-by: Kate Higa <khiga8@github.com>
2023-01-25 16:43:12 -05:00
John Hawthorn
f6e36c90aa
Merge pull request #47087 from jhawthorn/cookie_domain
Fix cookie domain for `domain: all` on two letter single level TLD
2023-01-24 13:56:38 -08:00
John Hawthorn
941e0cd212 Fix cookie domain calculation for two letter tld 2023-01-23 16:37:16 -08:00
Ron Shinall
189e4108c5 Address Selenium :capabilities deprecation warning 2023-01-23 21:40:01 +00:00
Samuel Williams
89df368e4d
Update ActionDispatch::Response to support streaming bodies.
Rack 3 introduces streaming bodies, which don't respond to `#each` and
MUST respond to `#call`. Ensure that the methods are correctly delegated.

`#to_ary` must also work correctly for enumerable bodies, and is used by
middleware like `Rack::ETag` to buffer enumerable bodies correctly.
2023-01-21 13:12:24 +13:00
Samuel Williams
0b4b4c6b96
Use Rack's own headers classes where appropriate.
Rack 3 response headers must be a mutable hash with lower-case keys. Rack
provides `Rack::Headers` as a compatibility layer for existing systems
which don't conform to this requirement. Prefer `Rack::Utils::HeaderHash`
on Rack 2, and `Rack::Headers` on Rack 3.


Remove some of the response test cases which test `nil` header keys as
these are considered invalid, and will fail with `Rack::Headers`.
2023-01-21 12:55:06 +13:00
Rafael Mendonça França
72642bd74b
Merge pull request #47081 from ioquatix/rack-3-query-parser-utf8
Don't assume that invalid UTF-8 will raise an error.
2023-01-20 15:20:42 -05:00
Rafael Mendonça França
3adec8df0a
Merge pull request #47083 from ioquatix/rack-3-ssl_test-headers
Don't assume the existance of `Rack::Utils::HeaderHash`.
2023-01-20 15:14:19 -05:00
Rafael Mendonça França
4c7c69d1fe
Merge pull request #47082 from ioquatix/rack-3-env-fields
Don't assume these fields will be present in a rack env.
2023-01-20 15:03:52 -05:00
Rafael Mendonça França
f43a088b78
Merge pull request #47077 from ioquatix/rack-3-rewindability
Don't assume Rack input supports rewindability.
2023-01-20 14:10:54 -05:00
Samuel Williams
fc0ec10272
Don't assume the existance of Rack::Utils::HeaderHash.
In Rack 3, this was deprecated and replaced with `Rack::Headers`. Using a
hash instance here appars to be sufficient.
2023-01-21 05:56:15 +13:00
Samuel Williams
7981dce06c
Don't assume these fields will be present in a rack env.
Rack 3 drops several of these fields as mandatory.
2023-01-21 05:53:20 +13:00
Samuel Williams
fd48e72518
Don't assume that invalid UTF-8 will raise an error.
Rack 3 parses the query part of a URL as ASCII-8BIT and does not fail on
invalid UTF-8 code sequences. Limit the tests to Rack 2.
2023-01-21 05:51:29 +13:00
Samuel Williams
911556fd81
Don't assume Rack input supports rewindability.
Rack 3 drops the requirement for a Rack input body to be rewindable. Limit
the related tests to run only on Rack < 3.
2023-01-21 05:38:54 +13:00
Samuel Williams
08510b5437
Improve matching of headers assertions.
The current implementation makes assumptions about the case and format of
headers. Introduce methods to handle headers in a case insensitive manner
and reduce churn when comparing with multi-value headers.
2023-01-21 05:26:22 +13:00
Samuel Williams
f63a5e7d16
Improve matching of cookie assertions.
The current implementation makes assumptions about the order and case
sensitivity of cookie attributes. Introduce methods to parse those fields
and compare them semantically. Update the existing tests to take advantage
of these new assertions.
2023-01-20 14:53:18 +13:00
Rafael Mendonça França
7c38258f70
Merge pull request #46327 from elliotcm/elliotcm/fix-stream-content-type
Fix content-type header with `send_stream`
2023-01-18 15:32:48 -05:00
Rafael Mendonça França
9ecc8785ef
Fix rubocop violations 2023-01-17 19:51:40 +00:00