Search code examples
rspecruby-on-rails-5.1

Testing POST ActionDispatch::Http::Parameters::ParseError: 765


It says there's an unexpected token in my params.

"ActionDispatch::Http::Parameters::ParseError: 765: unexpected token at 'conversation_identifier[participant_list][]=2&conversation_identifier[participant_list][]=1"

A version of the test with magic numbers for clarity:

let(:headers) do
  { 'HTTP_CURRENT_USER_ID' => 2,
    'Content-Type'         => 'application/json' }
end
let(:params) { { conversation_identifier: { participant_list: [1, 2] } }

it 'is getting testy' do
  post resource_url, params: params, headers: headers
  assert_equal 201, response.status
end

Now here's what's weird. It has no trouble parsing those params if I give it no headers.


Solution

  • Removing the 'Content-Type' => 'application/json' solved the problem.

    Finally remembered that ActionDispatch uses the headers to know how to parse the params. 'Content-Type' => 'application/json' is a standard piece of boilerplate that's ok to throw around with GET requests and query params but not with POST when used this way.