Christopher Foy
Christopher Foy

Reputation: 758

How do you specify Content-Type (and other headers) and request body in RSpec Rails Controller test?

I'm trying to test a rails action that takes raw json in the POST body. If i curl with the Content-Type: application/json header set, rails parses the params correctly.

How do you set the request body and headers directly in an rspec controller test?

Upvotes: 9

Views: 6030

Answers (2)

Damian Hamill
Damian Hamill

Reputation: 25

I use post :method_name, { :format => :json, ... other parameter data }

Upvotes: 0

Matthew Shanley
Matthew Shanley

Reputation: 1091

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

Upvotes: 9

Related Questions