@@ -65,6 +65,72 @@ TODO: https://github.com/rails/rails/pull/45602
65
65
66
66
TODO: https://github.com/rails/rails/pull/46049
67
67
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
+
68
134
Railties
69
135
--------
70
136
0 commit comments