Setting Content-Type header for RSpec and Rails-API
A lot of frustration and variations and that's what worked for me.
Rails 3.2.12 Rspec 2.10
@request.env["HTTP_ACCEPT"] = "application/json"
@request.env["CONTENT_TYPE"] = "application/json"
put :update, :id => 1, "email" => "bing@test.com"
wrap_parameters seems to be working declared this way
wrap_parameters User, format: :json
being used for User
model
How do you specify Content-Type (and other headers) and request body in RSpec Rails Controller test?
In RSpec 1.3 you can operate on a "request" variable. Something like this should work for you:
request.env['CONTENT_TYPE'] = 'application/json'
post :method_name
RSpec/rails not setting request headers
Switching from type :api to type :request gave me access to the request and response variables, which, before, I had to created variables myself to try access them.
When using type :api, I had access to last_response instead of response, because my type :api was including Rack::Test::Methods, which was messing up those variables (response and request), as according to this post.
When I switched to type :request, and was no longer importing Rack::Test::Methods, I was able to access request variable and saw that the desired headers were correctly added.
Using Rspec, how do I test the JSON format of my controller in Rails 3.0.11?
Try moving the :format
key inside the params hash of the request, like this:
describe ApplicationsController do
render_views
disconnect_sunspot
let(:application) { Factory.create(:application) }
subject { application }
context "JSON" do
describe "creating a new application" do
context "when not authorized" do
it "should not allow creation of an application" do
params = { :format => 'json', :application => { :name => "foo", :description => "bar" } }
post :create, params
Expect(Application.count).to eq(0)
expect(response.status).to eq(403)
expect(JSON.parse(response.body)["status"]).to eq("error")
expect(JSON.parse(response.body)["message"]).to match(/authorized/)
end
end
context "authorized" do
end
end
end
end
Let me know how it goes! thats the way I have set my tests, and they are working just fine!
Rspec api documentation + apitome request body in json format
I found this in the rspec api doc config
# Change how the post body is formatted by default, you can still override by `raw_post` # Can be :json, :xml, or a proc that will be passed the params
POSTing raw JSON data with Rails 3.2.11 and RSpec
As far as I have been able to tell, sending raw POST data is no longer possible within a controller spec. However, it can be done pretty easily in a request spec:
describe "Example", :type => :request do
params = { token: 0 }
post "/user/reset_password", params.to_json, { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' }
#=> params contains { "controller" => "user", "action" => "reset_password", "token" => 0 }
end
Related Topics
Ruby Google_Drive Gem Oauth2 Saving
Sidekiq Worker Not Getting Triggered
Ruby Conditional-Assignment and Private Methods
Spacing Around Parentheses in Ruby
Why Does a Simple Thin Server Stop Responding at 16500 Requests When Benchmarking
Why Is Throw and Catch Hardly Ever Used in Ruby
Devise Not Displaying Error Messge During an Authentication Failure
How to Add a Custom Log Level to Logger in Ruby
Can Someone Please Explain Class << Self to Me
What Is the Most Ruby-Ish Way of Accessing Nested Hash Values at Arbitrary Depths
Exception_Notification for Delayed_Job
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
Where to Put a Before_Filter Shared Between Multiple Controllers
How to Update Rails Locale Yaml File Without Loosing Comments and Variables
How to Implement Yes/No Instead of Boolean for Certain Cases in Rails
Twitter 3-Legged Authorization in Ruby