Skip to content

Commit 3f1f217

Browse files
authored
Merge pull request rails#53298 from Shopify/controller-test-case-body-encoding
Improve `ActionController::TestCase` to expose a binary encoded `request.body`.
2 parents 701af37 + bf6db28 commit 3f1f217

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Improve `ActionController::TestCase` to expose a binary encoded `request.body`.
2+
3+
The rack spec clearly states:
4+
5+
> The input stream is an IO-like object which contains the raw HTTP POST data.
6+
> When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode.
7+
8+
Until now its encoding was generally UTF-8, which doesn't accurately reflect production
9+
behavior.
10+
11+
*Jean Boussier*
12+
113
* Update `ActionController::AllowBrowser` to support passing method names to `:block`
214

315
```ruby

actionpack/lib/action_controller/test_case.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def assign_parameters(routes, controller_path, action, parameters, generated_pat
123123
end
124124
end
125125

126-
data_stream = StringIO.new(data)
126+
data_stream = StringIO.new(data.b)
127127
set_header "CONTENT_LENGTH", data_stream.length.to_s
128128
set_header "rack.input", data_stream
129129
end

actionpack/test/controller/test_case_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def render_body
5353
render plain: request.body.read
5454
end
5555

56+
def render_body_encoding
57+
request.body.rewind
58+
render plain: request.body.read.encoding.name
59+
end
60+
5661
def test_params
5762
render plain: ::JSON.dump(params.to_unsafe_h)
5863
end
@@ -270,6 +275,14 @@ def test_body_stream
270275
assert_equal params.to_query, @response.body
271276
end
272277

278+
def test_body_stream_is_binary
279+
params = Hash[:page, { name: "page name" }, "some key", 123]
280+
281+
post :render_body_encoding, params: params.dup
282+
283+
assert_equal Encoding::BINARY.name, @response.body
284+
end
285+
273286
def test_document_body_and_params_with_post
274287
post :test_params, params: { id: 1 }
275288
assert_equal({ "id" => "1", "controller" => "test_case_test/test", "action" => "test_params" }, ::JSON.parse(@response.body))

0 commit comments

Comments
 (0)