Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to set app_host to for Capybara

My tests attempt to visit webpages and verify that certain elements exist on the pages. For example, it visits http://foo.com/homepage.html and checks for a logo image, then visits http://bar.com/store/blah.html and checks that certain text appears on the page. My goal is to visit Kerberos authenticated webpages.

I found Kerberos code as below:

Main file

uri = URI.parse(Capybara.app_host)
kerberos = Kerberos.new(uri.host)
@kerberos_token = kerberos.encoded_token

kerberos.rb file

class Kerberos
    def initialize(host)
      @host = host
      @credentials = AuthGss::Negotiate.new("HTTP@#{@host}")
      @credentials.cache = ENV['KRB5CCNAME'] if ENV['KRB5CCNAME']
      @token = @credentials.step("")
    end

    def encoded_token
      Base64.encode64(@token).gsub(/\n/,"")
    end
  end

It utilizes Capybara.app_host value. I can't figure out what to set the Capybara.app_host value to. I can't figure out what it does. I have Capybara.run_server = false. Can someone help me understand how to use Capybara.app_host and how this relates to Kerberos authentication?

like image 460
Nosrettap Avatar asked Nov 15 '13 00:11

Nosrettap


2 Answers

The Capybara docs show an example of using a remote host. app_host is the base host of your web application:

Capybara.current_driver = :selenium
Capybara.run_server = false
Capybara.app_host = 'http://www.google.com'

visit('/users') # goes to http://www.google.com/users
like image 137
Shepmaster Avatar answered Nov 11 '22 11:11

Shepmaster


I was confused about app_host, but after a dig around in the Capybara source code it seems like it doesn't actually do very much. In fact, it only seems to be used at one place in the Capybara source code:

if url_relative && Capybara.app_host
  url = Capybara.app_host + url
  url_relative = false
end

Basically, if you pass a relative URL like /posts/1/edit to a method like visit, Capybara will visit the URL "#{Capybara.app_host}/posts/1/edit`. So the following two snippets of code are equivalent:

# 1:
Capybara.app_host = "https://stackoverflow.com"
visit "/questions/19991349"

# 2:
visit "https://stackoverflow.com/questions/19991349"

By default RSpec sets app_host to http://localhost, which is why you can write code like before { visit edit_post_path(1) } when using Capybara and Rails straight out of the box, and you don't have to write localhost every time.

like image 35
GMA Avatar answered Nov 11 '22 10:11

GMA