Skip to content

Commit 90c9011

Browse files
committed
Document response.parsed_body in 7.1 release notes
Add notes for [rails#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. [rails#47144]: rails#47144
1 parent c18bcd5 commit 90c9011

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

guides/.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
inherit_from:
22
- '../.rubocop.yml'
33

4+
AllCops:
5+
TargetRubyVersion: 3.0
6+
47
Style/StringLiterals:
58
Enabled: false
69

guides/source/7_1_release_notes.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,72 @@ TODO: https://github.com/rails/rails/pull/45602
6565

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

68+
### Support pattern matching for JSON `response.parsed_body`
69+
70+
When `ActionDispatch::IntegrationTest` tests blocks invoke
71+
`response.parsed_body` for JSON responses, their payloads will be available with
72+
indifferent access. This enables integration with [Ruby's Pattern
73+
Matching][pattern-matching], and built-in [Minitest support for pattern
74+
matching][minitest-pattern-matching]:
75+
76+
```ruby
77+
get "/posts.json"
78+
79+
response.content_type # => "application/json; charset=utf-8"
80+
response.parsed_body.class # => Array
81+
response.parsed_body # => [{"id"=>42, "title"=>"Title"},...
82+
83+
assert_pattern { response.parsed_body => [{ id: 42 }] }
84+
85+
get "/posts/42.json"
86+
87+
response.content_type # => "application/json; charset=utf-8"
88+
response.parsed_body.class # => ActiveSupport::HashWithIndifferentAccess
89+
response.parsed_body # => {"id"=>42, "title"=>"Title"}
90+
91+
assert_pattern { response.parsed_body => [{ title: /title/i }] }
92+
```
93+
94+
[pattern-matching]: https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html
95+
[minitest-pattern-matching]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_pattern
96+
97+
### Extend `response.parsed_body` to parse HTML with Nokogiri
98+
99+
[Extend the `ActionDispatch::Testing` module][#47144] to support parsing the
100+
value of an HTML `response.body` into a `Nokogiri::HTML5::Document` instance:
101+
102+
```ruby
103+
get "/posts"
104+
105+
response.content_type # => "text/html; charset=utf-8"
106+
response.parsed_body.class # => Nokogiri::HTML5::Document
107+
response.parsed_body.to_html # => "<!DOCTYPE html>\n<html>\n..."
108+
```
109+
110+
Newly added [Nokogiri support for pattern matching][nokogiri-pattern-matching],
111+
along with built-in [Minitest support for pattern
112+
matching][minitest-pattern-matching] presents opportunities to make test
113+
assertions about the structure and content of the HTML response:
114+
115+
```ruby
116+
get "/posts"
117+
118+
html = response.parsed_body # => <html>
119+
# <head></head>
120+
# <body>
121+
# <main><h1>Some main content</h1></main>
122+
# </body>
123+
# </html>
124+
125+
assert_pattern { html.at("main") => { content: "Some main content" } }
126+
assert_pattern { html.at("main") => { content: /content/ } }
127+
assert_pattern { html.at("main") => { children: [{ name: "h1", content: /content/ }] } }
128+
```
129+
130+
[#47144]: https://github.com/rails/rails/pull/47144
131+
[nokogiri-pattern-matching]: https://nokogiri.org/rdoc/Nokogiri/XML/Attr.html#method-i-deconstruct_keys
132+
[minitest-pattern-matching]: https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_pattern
133+
68134
Railties
69135
--------
70136

0 commit comments

Comments
 (0)