rails/actionpack/test/dispatch
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
..
request Make the test environment show rescuable exceptions in responses 2023-05-17 06:30:28 -07:00
routing Remove redundant test 2023-05-09 08:33:35 +01:00
session Improve cookie attribute assertions. 2023-07-19 15:36:57 +12:00
system_testing Add support for Playwright as a driver for system tests 2023-08-16 16:28:28 +09:00
test_response_test Ensure response.parsed_body support for pattern matching 2023-08-23 13:28:02 -04:00
actionable_exceptions_test.rb Add Rack::Lint to ActionableExceptions tests 2023-07-26 10:13:57 -04:00
assume_ssl_test.rb Add Rack::Lint to AssumeSSL middleware tests 2023-07-26 11:08:43 +00:00
callbacks_test.rb Add Rack::Lint to ActionDispatch::Callbacks tests 2023-07-26 08:52:23 +00:00
content_disposition_test.rb Escape # in RFC 5987 pattern 2020-08-31 10:31:30 -04:00
content_security_policy_test.rb Add Rack::Lint to ContentSecurityPolicy::Middleware tests 2023-07-28 09:37:23 +00:00
cookies_test.rb Add ActionDispatch::Cookies middleware test with Rack::Lint 2023-07-27 11:18:47 -04:00
debug_exceptions_test.rb Add Rack::Lint to DebugExceptions missing test 2023-07-31 13:19:32 +05:30
debug_locks_test.rb Add Rack::Lint to DebugLocks tests 2023-07-27 16:33:41 -04:00
exception_wrapper_test.rb Test behavior in unit test 2023-05-24 17:01:38 +00:00
executor_test.rb Add Rack::Lint to ActionDispatch::Executor tests 2023-07-27 15:38:06 +00:00
header_test.rb Remove Rubocop's comments from Rails code base 2018-07-26 23:37:31 +03:00
host_authorization_test.rb Fix host display when X_FORWARDED_HOST authorized 2023-08-15 03:27:13 -04:00
live_response_test.rb Rack spec requires mutable headers 2023-06-11 01:32:31 -07:00
mapper_test.rb Anchor the Action Cable server's route 2022-06-29 13:26:49 -05:00
middleware_stack_test.rb Add Rack::Lint to ActionDispatch::MiddlewareStack tests 2023-07-27 17:24:08 +00:00
mime_type_test.rb improve quoted parameters in mime types 2023-06-05 12:53:08 +02:00
mount_test.rb mounted routes with non-word characters 2019-04-15 15:11:13 +02:00
permissions_policy_test.rb Use constants defined in ActionDispatch::Constants instead of conditional assignments 2023-07-27 15:21:18 -07:00
prefix_generation_test.rb Remove body content from redirect responses 2022-02-25 13:31:54 -04:00
rack_cache_test.rb Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00
reloader_test.rb Add Rack::Lint to ActionDispatch::Reloader tests 2023-07-29 14:18:49 +05:30
request_id_test.rb Add Rack::Lint to RequestId tests 2023-07-28 10:29:31 -04:00
request_test.rb Add Rack::Lint to ActionDispatch::RemoteIp tests 2023-07-31 08:13:04 +00:00
response_test.rb Merge pull request #47092 from ioquatix/rack-3-streaming-body 2023-02-14 17:01:45 -08:00
routing_assertions_test.rb Create a class level #with_routing helper. 2023-06-06 14:13:24 -04:00
routing_test.rb Use expected casing for x-cascade headers in router 2023-07-28 16:17:08 -04:00
runner_test.rb Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00
server_timing_test.rb Add Rack::Lint to ActionDispatch::ServerTiming tests 2023-07-28 15:24:32 +00:00
show_exceptions_test.rb Add test coverage for ShowExceptions failsafe 2023-08-16 16:00:25 -04:00
ssl_test.rb Make ActionDispatch::SSL compatible with Rack 3.0 2023-07-28 14:23:25 -04:00
static_test.rb Add Rack::Lint to Static tests 2023-07-27 15:48:43 -04:00
test_request_test.rb Fix TestRequest coercing SERVER_PORT to be an int 2023-07-28 13:02:27 -04:00
test_response_test.rb Ensure response.parsed_body support for pattern matching 2023-08-23 13:28:02 -04:00
uploaded_file_test.rb Encodes the uploaded file's headers - Closes #38080 2021-02-22 17:01:39 -03:00
url_generation_test.rb Fix setting trailing_slash: true in route definition 2022-02-15 10:44:33 +01:00