rails
rails copied to clipboard
ActionDispatch::IntegrationTest GET with params keyword cause a conversion to POST
Steps to reproduce
- Setup a new Rails api_only app with MiniTest
- Write a controller with
index
andcreate
actions (and respective routes) - Write an
ActionDispatch::IntegrationTest
to test both previous actions - Run the test suite
test "should get index and return ok on success" do
get items_url, params: index_params, as: :json
assert_response :ok
end
test "should post create and return created on success" do
post items_url, params: create_params, as: :json
assert_response :created
end
Expected behavior
The first test (GET) is successful
Actual behavior
The first test fails, its verb is converted to POST
and the invoked action is create
. I managed make the test pass by converting the first test like this:
test "should get index and return ok on success" do
get items_url(index_params), as: :json
assert_response :ok
end
or by removing the as: :json
like this:
test "should get index and return ok on success" do
get items_url, params: index_params
assert_response :ok
end
System configuration
Rails version: 6.1.6
Ruby version: 3.0.2
Looks like this is intentional
https://github.com/rails/rails/blob/1f979184efc27e73f42c5d86c7f19437c6719612/actionpack/lib/action_dispatch/testing/integration.rb#L204-L211
Looks like this is intentional
Beat me to it by seconds 😆
For some more context: added in af1680f to fix #26033
@matteo-rossi-wise what is the high level expected behavior here? I believe JSON bodies for GET requests are undefined in the HTTP spec:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
From my point of view this behaviour is misleading and not documented at all. Moreover for an inbound request, Rails treats boh query-string and body params the same way with the params
Hash. With this, I'd expect the same behaviour in my integration tests.
In the end I find really confusing (and dangerous) converting an explicit GET request into a POST one.
What do you think?
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable
branch or on main
, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
This is intentional, but it should be document. Apart from that, I don't think this behavior should change.
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-0-stable
branch or on main
, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
I don't think this behavior should change.
@rafaelfranca Why not? This behavior doesn't make sense and it prevents consistency in test cases. Even though @matteo-rossi-wise's workaround works, it doesn't make sense to NOT be able to send params for GET requests.