I'm using the rails-api gem to build a web service and want to test my API with RSpec. Every request I make, regardless of the HTTP method has the CONTENT_TYPE
header set as "application/x-www-form-urlencoded". This isn't really a problem until I try to use wrap_parameters in my controller and it's not have any affect on the params hash:
class ApplicationController < ActionController::API
include ActionController::ParamsWrapper
end
class ProjectsController < ApplicationController
wrap_parameters :project, include: [:name]
# ...
end
This hack no longer works (@request is nil), and none of the other Stack Overflow posts I found work either.
If I make the following request in my RSpec test:
put "/projects/1.json", {name: 'Updated Project 1'}
and put a debugger in my controller I get:
(rdb:1) p params
{ "name"=>"Updated Project 1",
"action"=>"update",
"controller"=>"projects",
"id"=>"5539bbd9-010c-4cfb-88d3-82dadbc99507",
"format"=>"json"
}
(rdb:1) p request.content_type
"application/x-www-form-urlencoded"
I'm expecting to see something like this for the params hash (note the addition of the project key):
{ "name"=>"Updated Project 1",
"action"=>"update",
"controller"=>"projects",
"id"=>"5539bbd9-010c-4cfb-88d3-82dadbc99507",
"format"=>"json",
"project" => {"name" => "Updated Project 1"}
}
Is it possible to set the content type header using just RSpec? Or do I have have to use rack/test for this functionality?
wrap_parameters format: [:url_encoded_form, :json]
– Nimitzrequest.env['HTTP_CONTENT_TYPE'] = 'application/json'
or whatever else you need. – Heathenish