2009-12-09 15:39:03 +00:00
|
|
|
require 'uri'
|
2009-11-05 16:35:45 +00:00
|
|
|
require 'net/http'
|
2009-11-05 16:39:57 +00:00
|
|
|
require 'rack'
|
2010-07-10 01:06:44 +00:00
|
|
|
require 'capybara/util/timeout'
|
2009-11-05 16:35:45 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
module Capybara
|
|
|
|
class Server
|
|
|
|
class Identify
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def call(env)
|
|
|
|
if env["PATH_INFO"] == "/__identify__"
|
2011-04-26 10:41:43 +00:00
|
|
|
[200, {}, [@app.object_id.to_s]]
|
2010-07-09 18:31:58 +00:00
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-14 20:54:36 +00:00
|
|
|
class << self
|
|
|
|
def ports
|
|
|
|
@ports ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
attr_reader :app, :port
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
2009-11-04 22:00:05 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def host
|
2010-10-08 15:31:23 +00:00
|
|
|
"127.0.0.1"
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-11-04 22:00:05 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def url(path)
|
|
|
|
if path =~ /^http/
|
|
|
|
path
|
|
|
|
else
|
|
|
|
(Capybara.app_host || "http://#{host}:#{port}") + path.to_s
|
|
|
|
end
|
2010-01-17 16:40:26 +00:00
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def responsive?
|
2010-07-14 20:54:36 +00:00
|
|
|
res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') }
|
|
|
|
|
|
|
|
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
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def boot
|
2010-07-14 20:54:36 +00:00
|
|
|
if @app
|
|
|
|
@port = Capybara::Server.ports[@app.object_id]
|
2010-10-08 15:30:08 +00:00
|
|
|
|
2010-07-14 20:54:36 +00:00
|
|
|
if not @port or not responsive?
|
2010-09-02 13:03:49 +00:00
|
|
|
@port = Capybara.server_port || find_available_port
|
2010-07-14 20:54:36 +00:00
|
|
|
Capybara::Server.ports[@app.object_id] = @port
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2010-10-08 15:30:08 +00:00
|
|
|
Thread.new do
|
2010-10-28 02:53:59 +00:00
|
|
|
Capybara.server.call(Identify.new(@app), @port)
|
2010-10-08 15:30:08 +00:00
|
|
|
end
|
2010-07-09 18:31:58 +00:00
|
|
|
|
2010-11-17 15:40:39 +00:00
|
|
|
Capybara.timeout(Capybara.server_boot_timeout) do
|
|
|
|
if responsive? then true else sleep(0.5) and false end
|
|
|
|
end
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
|
|
|
end
|
2010-11-09 13:05:56 +00:00
|
|
|
rescue TimeoutError
|
2010-07-11 11:11:30 +00:00
|
|
|
puts "Rack application timed out during boot"
|
2010-07-09 18:31:58 +00:00
|
|
|
exit
|
2010-07-14 20:54:36 +00:00
|
|
|
else
|
|
|
|
self
|
2009-11-04 22:00:05 +00:00
|
|
|
end
|
2009-11-07 14:35:47 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
private
|
2010-01-17 16:40:26 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def find_available_port
|
2010-07-14 20:54:36 +00:00
|
|
|
server = TCPServer.new('127.0.0.1', 0)
|
|
|
|
server.addr[1]
|
|
|
|
ensure
|
|
|
|
server.close if server
|
2010-07-09 08:05:30 +00:00
|
|
|
end
|
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-11-07 14:35:47 +00:00
|
|
|
end
|