adding headers to a rspec get

Viewed 9421

I am a newbie at rspec - there it's said!

I'm trying to pass a jwt token to a get request. I have seen several posts saying that the syntax is:

get :endpoint, params: {}, headers: {}

So that's what I do:

require 'rails_helper'
require "rack/test"
include Rack::Test::Methods

def authenticated_header(user, password)
  response = AuthenticateUser.call(user, password)
  { "Authorization" => response.result }
end

RSpec.describe Api::AlbumsController, type: :controller do
  it "returns albums" do
    get :index, params: {}, headers: authenticated_header("admin", "123")
    puts response.body
  end
end

But I get an error:

F.....

Failures:

  1) Api::AlbumsController returns albums
     Failure/Error: get "/index", params: {}, headers: authenticated_header("admin", "123")

     ArgumentError:
       unknown keyword: headers
     # ./spec/controllers/photos_controller_spec.rb:14:in `block (2 levels) in <top (required)>'

Finished in 0.44102 seconds (files took 8.6 seconds to load)
6 examples, 1 failure

what am I doing wrong? Thanks

3 Answers

In rspec-rails 4.0 I had to use a slightly different format to add to the header for my spec:

request.headers.merge!(HTTP_REFERER: 'http://test.host/home')

Related