Rails rspec HTTP Basic Authentication

how to check login to rails 3.1 application using http basic auth using rspec2 and capybara?

I use this;

describe "GET 'index'" do it "should be successful" do request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") get 'index' response.should be_success end end 

but he gives me this error;

  NoMethodError: undefined method `env' for nil:NilClass 
+4
source share
2 answers

Capybara comes with Rack :: Test .

So, you can use the Rack::Test::Session.basic_authorize method to set the Authorize HTTP header field before making a request.

 basic_authorize 'username', 'password' get 'index' 
+16
source
 @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") 

It should be:

 request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password") 

as request is not an instance variable.

-1
source

All Articles