Reputation: 3995
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?
Upvotes: 0
Views: 2092
Reputation: 29389
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
Upvotes: 1