Last Updated: February 25, 2016
·
3.225K
· Alexandr K

Basic http authentication using rspec and rack-test.

Hello.

Example how to use basic http authentication for the sinatra or grape api rspec tests:

def headers(options = {})
  # Using gem 'choices' for define different auth
  # configuration based on env.
  username = options.fetch(:username, Rails.configuration.auth.username)
  password = options.fetch(:password, Rails.configuration.auth.password)

  { 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(username, password) }
  end

def auth_get(route, params = {})
  get route, params, headers
end