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

118 lines
2.5 KiB
Ruby
Raw Normal View History

require 'uri'
2009-11-05 11:35:45 -05:00
require 'net/http'
2009-11-05 11:39:57 -05:00
require 'rack'
2010-07-09 21:06:44 -04:00
require 'capybara/util/timeout'
2009-11-05 11:35:45 -05:00
2010-07-09 14:31:58 -04:00
module Capybara
class Server
class Identify
def initialize(app)
@app = app
end
2010-07-09 14:31:58 -04:00
def call(env)
if env["PATH_INFO"] == "/__identify__"
[200, {}, @app.object_id.to_s]
else
@app.call(env)
end
end
end
2010-07-09 14:31:58 -04:00
attr_reader :app, :port
2010-07-09 14:31:58 -04:00
def initialize(app)
@app = app
end
2009-11-04 17:00:05 -05:00
2010-07-09 14:31:58 -04:00
def host
"localhost"
end
2009-11-04 17:00:05 -05:00
2010-07-09 14:31:58 -04:00
def url(path)
if path =~ /^http/
path
else
(Capybara.app_host || "http://#{host}:#{port}") + path.to_s
end
end
2010-07-09 14:31:58 -04:00
def responsive?
is_running_on_port?(port)
end
2010-07-09 14:31:58 -04:00
def handler
2010-02-19 12:13:03 -05:00
begin
2010-07-09 14:31:58 -04:00
require 'rack/handler/thin'
Rack::Handler::Thin
2010-02-19 12:13:03 -05:00
rescue LoadError
2010-07-09 14:31:58 -04:00
begin
require 'rack/handler/mongrel'
Rack::Handler::Mongrel
rescue LoadError
require 'rack/handler/webrick'
Rack::Handler::WEBrick
end
2010-02-19 12:13:03 -05:00
end
end
2010-07-09 14:31:58 -04:00
def boot
return self unless @app
find_available_port
Capybara.log "application has already booted" and return self if responsive?
Capybara.log "booting Rack applicartion on port #{port}"
2010-07-09 14:31:58 -04:00
Thread.new do
handler.run(Identify.new(@app), :Port => port, :AccessLog => [])
2009-11-04 17:00:05 -05:00
end
2010-07-09 14:31:58 -04:00
Capybara.log "checking if application has booted"
Capybara.timeout(10) do
if responsive?
Capybara.log("application has booted")
true
else
sleep 0.5
false
end
end
self
rescue Timeout::Error
Capybara.log "Rack application timed out during boot"
exit
2009-11-04 17:00:05 -05:00
end
2009-11-07 09:35:47 -05:00
2010-07-09 14:31:58 -04:00
private
2010-07-09 14:31:58 -04:00
def find_available_port
@port = 9887
@port += 1 while is_port_open?(@port) and not is_running_on_port?(@port)
end
2010-07-09 14:31:58 -04:00
def is_running_on_port?(tested_port)
res = Net::HTTP.start(host, tested_port) { |http| http.get('/__identify__') }
2009-11-07 09:35:47 -05:00
2010-07-09 14:31:58 -04:00
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
return res.body == @app.object_id.to_s
end
rescue Errno::ECONNREFUSED, Errno::EBADF
return false
end
2010-07-09 14:31:58 -04:00
def is_port_open?(tested_port)
Timeout::timeout(1) do
begin
s = TCPSocket.new(host, tested_port)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
return false
end
end
2010-07-09 14:31:58 -04:00
rescue Timeout::Error
return false
end
2010-07-09 14:31:58 -04:00
end
2009-11-07 09:35:47 -05:00
end