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