Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails rspec http basic authentication test

how do I test logging into a rails 3.1 app with http basic auth using rspec2 & capybara?

I'm using 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 it gives me this error;

 NoMethodError:
            undefined method `env' for nil:NilClass
like image 611
raphael_turtle Avatar asked Dec 05 '22 20:12

raphael_turtle


1 Answers

Capybara comes with Rack::Test built in.

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

basic_authorize 'username', 'password'
get 'index'
like image 88
ian Avatar answered Dec 07 '22 23:12

ian