How to set request headers in rspec request spec?
Solution 1
You should be able to specify HTTP headers as the third argument to your get() method as described here:
http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get
and here
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-process
So, you can try something like this:
get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}
Solution 2
I used this in Test::Unit:
@request.env['HTTP_ACCEPT'] = "*/*, application/youtube-client"
get :index
Solution 3
I'm adding this here, as I got majorly stuck trying to do this in Rails 5.1.rc1
The get method signature is slightly different now.
You need to specify the options after the path as keyword arguments, i.e.
get /some/path, headers: {'ACCEPT' => 'application/json'}
FYI, the full set of keywords arguments are:
params: {}, headers: {}, env: {}, xhr: false, as: :symbol
Solution 4
This is working for controller specs, not request specs:
request.headers["My Header"] = "something"
Solution 5
Using rspec with Rack::Test::Methods
header 'X_YOUR_HEADER_VAR', 'val'
get '/path'
The header var will come through as X-Your-Header-Var
Sergey
Updated on July 24, 2020Comments
-
Sergey almost 4 years
In the controller spec, I can set http accept header like this:
request.accept = "application/json"
but in the request spec, "request" object is nil. So how can I do it here?
The reason I want to set http accept header to json is so I can do this:
get '/my/path'
instead of this
get '/my/path.json'
-
Matt Scilipoti about 12 yearsWe needed to use 'HTTP_ACCEPT':
get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}
-
Alex Soto about 11 yearsNOTE: This is for integration testing, similar to comment below, in rspec-rails controller tests, you would use: request.env["HTTP_ACCEPT"] =
-
gerry3 about 11 yearsSimilarly, as Alex Soto notes in a comment on another answer, in rspec-rails controller tests, you can use: request.env["HTTP_ACCEPT"]
-
ecoologic almost 11 yearsthanks a lot dude, only example that worked for me on an old 2.3 app with
ActionController::TestCase
-
Kelvin almost 11 years+1 I tried using a key named
Cookie
in the headers hash (because that's what my browser sends), but it didn't work. Then I didrequest.keys
and saw a key namedHTTP_COOKIE
. Using that worked. They really should document this better. -
ajmurmann over 10 yearsSmall gotcha that I ran into because I am silly: The header keys have to be Strings. Symbols will not show up.
-
Alan over 10 yearsNot sure, but probably works because the rails is looking for .format for that route; this happened to work for me too.
-
Kevin Carmody over 9 yearsIn case anyone is wondering, this just adds
format=json
as a query param. Not the same as a header field. -
Stepan Zakharov over 9 yearsIt really works! I also found that answer in github.com/rspec/rspec-rails/issues/65
-
Edgar Ortega about 9 yearsThis worked for me, it depends on how are you retrieving the headers, if you are using
request.headers
orrequest.env
-
β.εηοιτ.βε about 9 yearsDoes this really provide an answer to the OP question ? If it is a new question, it is a better idea to open up a new question.
-
Franklin Yu over 7 years@ajmurmann Now symbols work: "Authorization" header can be
:authorization
. -
Franklin Yu over 7 yearsNote: this is for
Test::Unit
, not for RSpec. -
Franklin Yu over 7 yearsNote: This is for controller tests, not integration tests mentioned in the question.
-
Bryan Dimas over 7 years@Sytse Sijbrandij Nobody asked about Test::Unit. Question asked about rspec.
-
yuval over 7 yearsThis is correct for generic headers, but if you're only trying to specify a JSON format with a rails controller like the OP is then
get(:action, format: :json)
should do the trick -
James Tan over 7 yearsits indicating rspec
-
Karl Wilbur over 7 yearsHere's the source code on GitHub: github.com/rails/rails/blob/…
-
Petr Gazarov about 7 yearsdidn't work for integration tests. Works with controller tests, however.
-
Cyril Duchon-Doris almost 7 yearsNew RSspec 3 syntax would be like
get my_resource_path, params: {}, headers: { 'HTTP_ACCEPT' => "application/json" }
` -
Евгений Масленков over 6 yearsYeah. In rspec it raises
ndefined method 'header'
error for me. -
SexxLuthor almost 6 yearsThanks @CyrilDuchon-Doris, yours was the only correct solution I've been able to find in order to set an auth header.
-
webaholik almost 6 yearsShouldn't
headers =
bemy_headers =
? -
Jim Stewart almost 6 yearsFixed. Thanks @webaholik.
-
Francisco Quintero about 4 yearsSweet. Just what I was looking for. Thanks!
-
Erem over 2 yearsWon't work in a controller spec.