Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Jérémy's Profile

GitHub User: jerefrer

Comments by Jérémy

Avatar

Here is a nice answer if your are doing controller or acceptance tests.

If you're like me and went with Rack::Test::Methods (see this post section 'Testing your API', some examples here as well), here's the solution (which was a pain to figure out ...) :

ruby
get '/api/v1/users.json', nil, 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Token.encode_credentials('THE-AWESOME-TOKEN')

And here is a little helper to keep things DRY :

ruby
def perform(method, url, params = {})
  token = ActionController::HttpAuthentication::Token.encode_credentials(Settings.application_api.token)
  send method, url, params, 'HTTP_AUTHORIZATION' => token

end