Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding headers to a rspec get

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

like image 637
martin Avatar asked Jul 23 '17 14:07

martin


1 Answers

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
like image 105
Igor Drozdov Avatar answered Sep 19 '22 14:09

Igor Drozdov