0

I'm building a REST API on Rails, and I'm trying to use RSpec to test it. I'm using fairly boilerplate auth code:

  def authenticate_user_from_token
    pp 'authenticate_user_from_token'
    pp request.headers.inspect
    user = authenticate_with_http_token do |token, options|
      pp token
      user_id = options[:user_id]
      user = user_id && Eater.find_by_id(user_id)

      if user && Devise.secure_compare(user.auth_token, token)
        user
      else
        nil
      end
    end

    @current_user = user
  end

This works perfectly if I send requests with curl, but I can't figure out how to pass the auth token in Rspec. My basic request is this:

get '/api/1/users/1'

I've tried setting the token using the header method:

header 'Authorization: Token token', @token

That doesn't work. I've also tried passing it to the get method as I've seen in some examples:

get '/api/1/users/1', :authorization => @token

Also doesn't work. What's the correct way to do this?

2
  • Is this a controller spec or a request spec? Commented Jun 2, 2015 at 9:58
  • Request. I'm trying to test the API by using it as a client would. Commented Jun 2, 2015 at 13:43

1 Answer 1

1

The HTTP parameters are the third argument to get, so you need to do something like:

get '/api/1/users/1', nil, {'Authorization' => @token}

Depending on the constraints on the key, you may also get away with:

get '/api/1/users/1', nil, authorization: @token

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.