Question: adding headers to a rspec get

Question

adding headers to a rspec get

Answers 1
Added at 2017-07-23 16:07
Tags
Question

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

Answers to

adding headers to a rspec get

nr: #1 dodano: 2017-07-23 16:07

Unfortunately, rspec doesn't allow to set request headers, so you'll need to workaround it like this:

  it "returns albums" do
    request.headers.merge!(authenticated_header("admin", "123"))
    get "/index", params: {}
    puts response.body
  end
Source Show
◀ Wstecz