How to set request headers in rspec request spec?

86,271

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

Share:
86,271
Sergey
Author by

Sergey

Updated on July 24, 2020

Comments

  • Sergey
    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
    Matt Scilipoti about 12 years
    We needed to use 'HTTP_ACCEPT': get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}
  • Alex Soto
    Alex Soto about 11 years
    NOTE: This is for integration testing, similar to comment below, in rspec-rails controller tests, you would use: request.env["HTTP_ACCEPT"] =
  • gerry3
    gerry3 about 11 years
    Similarly, as Alex Soto notes in a comment on another answer, in rspec-rails controller tests, you can use: request.env["HTTP_ACCEPT"]
  • ecoologic
    ecoologic almost 11 years
    thanks a lot dude, only example that worked for me on an old 2.3 app with ActionController::TestCase
  • Kelvin
    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 did request.keys and saw a key named HTTP_COOKIE. Using that worked. They really should document this better.
  • ajmurmann
    ajmurmann over 10 years
    Small gotcha that I ran into because I am silly: The header keys have to be Strings. Symbols will not show up.
  • Alan
    Alan over 10 years
    Not sure, but probably works because the rails is looking for .format for that route; this happened to work for me too.
  • Kevin Carmody
    Kevin Carmody over 9 years
    In case anyone is wondering, this just adds format=json as a query param. Not the same as a header field.
  • Stepan Zakharov
    Stepan Zakharov over 9 years
    It really works! I also found that answer in github.com/rspec/rspec-rails/issues/65
  • Edgar Ortega
    Edgar Ortega about 9 years
    This worked for me, it depends on how are you retrieving the headers, if you are using request.headers or request.env
  • β.εηοιτ.βε
    β.εηοιτ.βε about 9 years
    Does 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
    Franklin Yu over 7 years
    @ajmurmann Now symbols work: "Authorization" header can be :authorization.
  • Franklin Yu
    Franklin Yu over 7 years
    Note: this is for Test::Unit, not for RSpec.
  • Franklin Yu
    Franklin Yu over 7 years
    Note: This is for controller tests, not integration tests mentioned in the question.
  • Bryan Dimas
    Bryan Dimas over 7 years
    @Sytse Sijbrandij Nobody asked about Test::Unit. Question asked about rspec.
  • yuval
    yuval over 7 years
    This 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
    James Tan over 7 years
    its indicating rspec
  • Karl Wilbur
    Karl Wilbur over 7 years
    Here's the source code on GitHub: github.com/rails/rails/blob/…
  • Petr Gazarov
    Petr Gazarov about 7 years
    didn't work for integration tests. Works with controller tests, however.
  • Cyril Duchon-Doris
    Cyril Duchon-Doris almost 7 years
    New RSspec 3 syntax would be like get my_resource_path, params: {}, headers: { 'HTTP_ACCEPT' => "application/json" } `
  • Евгений Масленков
    Евгений Масленков over 6 years
    Yeah. In rspec it raises ndefined method 'header' error for me.
  • SexxLuthor
    SexxLuthor almost 6 years
    Thanks @CyrilDuchon-Doris, yours was the only correct solution I've been able to find in order to set an auth header.
  • webaholik
    webaholik almost 6 years
    Shouldn't headers = be my_headers = ?
  • Jim Stewart
    Jim Stewart almost 6 years
    Fixed. Thanks @webaholik.
  • Francisco Quintero
    Francisco Quintero about 4 years
    Sweet. Just what I was looking for. Thanks!
  • Erem
    Erem over 2 years
    Won't work in a controller spec.