1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/server.rb
Theo Hultberg and Jonas Nicklas 6b099a4a33 renamed to capybara
2009-11-16 22:02:16 +01:00

53 lines
1 KiB
Ruby

require 'net/http'
require 'rack'
class Capybara::Server
attr_reader :app
def initialize(app)
@app = app
end
def port
8080
end
def host
'localhost'
end
def url(path)
"http://#{host}:#{port}#{path}"
end
def boot
Capybara.log "application has already booted" and return if responsive?
Capybara.log "booting Rack applicartion on port #{port}"
start_time = Time.now
Thread.new do
Rack::Handler::Mongrel.run @app, :Port => port
end
Capybara.log "checking if application has booted"
loop do
Capybara.log("application has booted") and break if responsive?
if Time.now - start_time > 10
Capybara.log "Rack application timed out during boot"
exit
end
Capybara.log '.'
sleep 1
end
end
def responsive?
res = Net::HTTP.start(host, port) { |http| http.get('/') }
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
return true
end
rescue Errno::ECONNREFUSED
return false
end
end