teamcapybara--capybara/lib/capybara/server.rb

57 lines
1.1 KiB
Ruby
Raw Normal View History

require 'uri'
2009-11-05 16:35:45 +00:00
require 'net/http'
2009-11-05 16:39:57 +00:00
require 'rack'
2009-12-07 01:53:57 +00:00
require 'rack/handler/mongrel'
2009-11-05 16:35:45 +00:00
2009-11-16 21:02:16 +00:00
class Capybara::Server
2009-11-04 22:00:05 +00:00
attr_reader :app
def initialize(app)
@app = app
end
def port
2009-11-07 14:35:47 +00:00
8080
2009-11-04 22:00:05 +00:00
end
def host
'localhost'
end
def url(path)
path = URI.parse(path).request_uri if path =~ /^http/
2009-11-04 22:00:05 +00:00
"http://#{host}:#{port}#{path}"
end
def boot
2009-11-16 21:02:16 +00:00
Capybara.log "application has already booted" and return if responsive?
Capybara.log "booting Rack applicartion on port #{port}"
2009-11-04 22:00:05 +00:00
start_time = Time.now
Thread.new do
Rack::Handler::Mongrel.run @app, :Port => port
end
2009-11-16 21:02:16 +00:00
Capybara.log "checking if application has booted"
2009-11-04 22:00:05 +00:00
loop do
2009-11-16 21:02:16 +00:00
Capybara.log("application has booted") and break if responsive?
2009-11-07 14:35:47 +00:00
if Time.now - start_time > 10
2009-11-16 21:02:16 +00:00
Capybara.log "Rack application timed out during boot"
2009-11-04 22:00:05 +00:00
exit
end
2009-11-16 21:02:16 +00:00
Capybara.log '.'
2009-11-04 22:00:05 +00:00
sleep 1
end
end
2009-11-07 14:35:47 +00:00
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