Skip to content

Commit

Permalink
Improve ActionController::TestCase to expose a binary encoded `requ…
Browse files Browse the repository at this point in the history
…est.body`.

The rack spec clearly states:

> The input stream is an IO-like object which contains the raw HTTP POST data.
> When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode.

Until now its encoding was generally UTF-8, which doesn't accurately reflect production
behavior.
  • Loading branch information
byroot committed Oct 14, 2024
1 parent d64611b commit bf6db28
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
* Improve `ActionController::TestCase` to expose a binary encoded `request.body`.

The rack spec clearly states:

> The input stream is an IO-like object which contains the raw HTTP POST data.
> When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode.
Until now its encoding was generally UTF-8, which doesn't accurately reflect production
behavior.

*Jean Boussier*

* Update `ActionController::AllowBrowser` to support passing method names to `:block`

```ruby
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def assign_parameters(routes, controller_path, action, parameters, generated_pat
end
end

data_stream = StringIO.new(data)
data_stream = StringIO.new(data.b)
set_header "CONTENT_LENGTH", data_stream.length.to_s
set_header "rack.input", data_stream
end
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/controller/test_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def render_body
render plain: request.body.read
end

def render_body_encoding
request.body.rewind
render plain: request.body.read.encoding.name
end

def test_params
render plain: ::JSON.dump(params.to_unsafe_h)
end
Expand Down Expand Up @@ -270,6 +275,14 @@ def test_body_stream
assert_equal params.to_query, @response.body
end

def test_body_stream_is_binary
params = Hash[:page, { name: "page name" }, "some key", 123]

post :render_body_encoding, params: params.dup

assert_equal Encoding::BINARY.name, @response.body
end

def test_document_body_and_params_with_post
post :test_params, params: { id: 1 }
assert_equal({ "id" => "1", "controller" => "test_case_test/test", "action" => "test_params" }, ::JSON.parse(@response.body))
Expand Down

0 comments on commit bf6db28

Please sign in to comment.