Skip to content

Commit

Permalink
Document response.parsed_body in 7.1 release notes
Browse files Browse the repository at this point in the history
Add notes for [#47144][] to the 7.1 Release Notes.

Additionally, in the time since that was merged, both Nokogiri and
Minitest have merged the PRs mentioned to integrate support for Ruby's
Pattern matching (sparklemotion/nokogiri#2523
and minitest/minitest#936, respectively).

This commit adds coverage for those new assertions, and incorporates
guidance into the release notes.

[#47144]: #47144
  • Loading branch information
seanpdoyle committed Aug 23, 2023
1 parent c18bcd5 commit 90c9011
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions guides/.rubocop.yml
@@ -1,6 +1,9 @@
inherit_from:
- '../.rubocop.yml'

AllCops:
TargetRubyVersion: 3.0

Style/StringLiterals:
Enabled: false

Expand Down
66 changes: 66 additions & 0 deletions guides/source/7_1_release_notes.md
Expand Up @@ -65,6 +65,72 @@ TODO: https://github.com/rails/rails/pull/45602

TODO: https://github.com/rails/rails/pull/46049

### Support pattern matching for JSON `response.parsed_body`

When `ActionDispatch::IntegrationTest` tests blocks invoke
`response.parsed_body` for JSON responses, their payloads will be available with
indifferent access. This enables integration with [Ruby's Pattern
Matching][pattern-matching], and built-in [Minitest support for pattern
matching][minitest-pattern-matching]:

```ruby
get "/posts.json"

response.content_type # => "application/json; charset=utf-8"
response.parsed_body.class # => Array
response.parsed_body # => [{"id"=>42, "title"=>"Title"},...

assert_pattern { response.parsed_body => [{ id: 42 }] }

get "/posts/42.json"

response.content_type # => "application/json; charset=utf-8"
response.parsed_body.class # => ActiveSupport::HashWithIndifferentAccess
response.parsed_body # => {"id"=>42, "title"=>"Title"}

assert_pattern { response.parsed_body => [{ title: /title/i }] }
```

[pattern-matching]: https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html
[minitest-pattern-matching]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_pattern

### Extend `response.parsed_body` to parse HTML with Nokogiri

[Extend the `ActionDispatch::Testing` module][#47144] to support parsing the
value of an HTML `response.body` into a `Nokogiri::HTML5::Document` instance:

```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..."
```

Newly added [Nokogiri support for pattern matching][nokogiri-pattern-matching],
along with built-in [Minitest support for pattern
matching][minitest-pattern-matching] presents opportunities to make test
assertions about the structure and content of the HTML response:

```ruby
get "/posts"

html = response.parsed_body # => <html>
# <head></head>
# <body>
# <main><h1>Some main content</h1></main>
# </body>
# </html>

assert_pattern { html.at("main") => { content: "Some main content" } }
assert_pattern { html.at("main") => { content: /content/ } }
assert_pattern { html.at("main") => { children: [{ name: "h1", content: /content/ }] } }
```

[#47144]: https://github.com/rails/rails/pull/47144
[nokogiri-pattern-matching]: https://nokogiri.org/rdoc/Nokogiri/XML/Attr.html#method-i-deconstruct_keys
[minitest-pattern-matching]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_pattern

Railties
--------

Expand Down

0 comments on commit 90c9011

Please sign in to comment.