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

169 lines
3.8 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-01-08 15:23:54 -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-11-05 11:35:45 -05:00
2010-07-09 14:31:58 -04:00
module Capybara
class Server
class Middleware
class Counter
attr_reader :value
def initialize
@value = 0
@mutex = Mutex.new
end
def increment
@mutex.synchronize { @value += 1 }
end
def decrement
@mutex.synchronize { @value -= 1 }
end
end
attr_accessor :error
def initialize(app, server_errors)
2010-07-09 14:31:58 -04:00
@app = app
@counter = Counter.new
@server_errors = server_errors
end
def pending_requests?
@counter.value > 0
2010-07-09 14:31:58 -04:00
end
2010-07-09 14:31:58 -04:00
def call(env)
if env["PATH_INFO"] == "/__identify__"
[200, {}, [@app.object_id.to_s]]
2010-07-09 14:31:58 -04:00
else
@counter.increment
begin
@app.call(env)
rescue *@server_errors => e
2018-01-08 15:23:54 -05:00
@error ||= e
raise e
ensure
@counter.decrement
end
2010-07-09 14:31:58 -04:00
end
end
end
class << self
def ports
@ports ||= {}
end
end
attr_reader :app, :port, :host
def initialize(app, *deprecated_options, port: Capybara.server_port, host: Capybara.server_host, reportable_errors: Capybara.server_errors)
warn "Positional arguments, other than the application, to Server#new are deprecated, please use keyword arguments" unless deprecated_options.empty?
2010-07-09 14:31:58 -04:00
@app = app
2014-03-31 18:10:27 -04:00
@server_thread = nil # suppress warnings
@host = deprecated_options[1] || host
@reportable_errors = deprecated_options[2] || reportable_errors
@using_ssl = false
@port = deprecated_options[0] || port
2016-07-25 13:04:50 -04:00
@port ||= Capybara::Server.ports[port_key]
@port ||= find_available_port(host)
2010-07-09 14:31:58 -04:00
end
2009-11-04 17:00:05 -05:00
def reset_error!
2016-07-25 15:12:59 -04:00
middleware.error = nil
end
def error
2016-07-25 15:12:59 -04:00
middleware.error
end
def using_ssl?
@using_ssl
end
2010-07-09 14:31:58 -04:00
def responsive?
return false if @server_thread && @server_thread.join(0)
begin
res = if !@using_ssl
http_connect
else
https_connect
end
rescue EOFError, Net::ReadTimeout
res = https_connect
@using_ssl = true
end
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
2016-07-25 15:12:59 -04:00
return res.body == app.object_id.to_s
end
rescue SystemCallError
return false
2010-07-09 14:31:58 -04:00
end
def wait_for_pending_requests
start_time = Capybara::Helpers.monotonic_time
while pending_requests?
if (Capybara::Helpers.monotonic_time - start_time) > 60
raise "Requests did not finish in 60 seconds"
end
sleep 0.01
end
end
2010-07-09 14:31:58 -04:00
def boot
unless responsive?
2016-07-25 15:12:59 -04:00
Capybara::Server.ports[port_key] = port
@server_thread = Thread.new do
2016-07-25 15:12:59 -04:00
Capybara.server.call(middleware, port, host)
2010-07-09 14:31:58 -04:00
end
start_time = Capybara::Helpers.monotonic_time
until responsive?
if (Capybara::Helpers.monotonic_time - start_time) > 60
raise "Rack application timed out during boot"
end
@server_thread.join(0.1)
end
2010-07-09 14:31:58 -04:00
end
self
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
def http_connect
Net::HTTP.start(host, port, read_timeout: 2) { |http| http.get('/__identify__') }
end
def https_connect
Net::HTTP.start(host, port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |http| http.get('/__identify__') }
end
2016-07-25 15:12:59 -04:00
def middleware
@middleware ||= Middleware.new(app, @reportable_errors)
2016-07-25 15:12:59 -04:00
end
2016-07-25 13:04:50 -04:00
def port_key
2016-07-25 15:12:59 -04:00
Capybara.reuse_server ? app.object_id : middleware.object_id
2016-07-25 13:04:50 -04:00
end
def pending_requests?
2016-07-25 15:12:59 -04:00
middleware.pending_requests?
2016-07-25 13:04:50 -04:00
end
def find_available_port(host)
server = TCPServer.new(host, 0)
server.addr[1]
ensure
server.close if server
end
2010-07-09 14:31:58 -04:00
end
2009-11-07 09:35:47 -05:00
end