rails icon indicating copy to clipboard operation
rails copied to clipboard

ActionDispatch::IntegrationTest GET with params keyword cause a conversion to POST

Open matteo-rossi-wise opened this issue 1 year ago • 5 comments

Steps to reproduce

  • Setup a new Rails api_only app with MiniTest
  • Write a controller with index and create 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

matteo-rossi-wise avatar Jun 20 '22 13:06 matteo-rossi-wise

Looks like this is intentional

https://github.com/rails/rails/blob/1f979184efc27e73f42c5d86c7f19437c6719612/actionpack/lib/action_dispatch/testing/integration.rb#L204-L211

deepakmahakale avatar Jun 20 '22 17:06 deepakmahakale

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.

skipkayhil avatar Jun 20 '22 17:06 skipkayhil

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?

matteo-rossi-wise avatar Jun 20 '22 18:06 matteo-rossi-wise

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.

rails-bot[bot] avatar Sep 18 '22 19:09 rails-bot[bot]

This is intentional, but it should be document. Apart from that, I don't think this behavior should change.

rafaelfranca avatar Sep 20 '22 00:09 rafaelfranca