2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
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'
|
2018-05-16 23:04:24 -04:00
|
|
|
require 'capybara/server/middleware'
|
|
|
|
require 'capybara/server/animation_disabler'
|
2018-06-05 22:01:20 -04:00
|
|
|
require 'capybara/server/checker'
|
2009-11-05 11:35:45 -05:00
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
module Capybara
|
2018-10-03 12:08:15 -04:00
|
|
|
# @api private
|
2010-07-09 14:31:58 -04:00
|
|
|
class Server
|
2010-07-14 16:54:36 -04:00
|
|
|
class << self
|
|
|
|
def ports
|
|
|
|
@ports ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-11 17:26:37 -04:00
|
|
|
attr_reader :app, :port, :host
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2018-11-02 14:38:58 -04:00
|
|
|
def initialize(app,
|
|
|
|
*deprecated_options,
|
|
|
|
port: Capybara.server_port,
|
|
|
|
host: Capybara.server_host,
|
|
|
|
reportable_errors: Capybara.server_errors,
|
|
|
|
extra_middleware: [])
|
2019-10-15 21:02:35 -04:00
|
|
|
unless deprecated_options.empty?
|
|
|
|
warn 'Positional arguments, other than the application, to Server#new are deprecated, please use keyword arguments'
|
|
|
|
end
|
2010-07-09 14:31:58 -04:00
|
|
|
@app = app
|
2018-05-16 23:04:24 -04:00
|
|
|
@extra_middleware = extra_middleware
|
2014-03-31 18:10:27 -04:00
|
|
|
@server_thread = nil # suppress warnings
|
2018-05-01 16:36:42 -04:00
|
|
|
@host = deprecated_options[1] || host
|
|
|
|
@reportable_errors = deprecated_options[2] || reportable_errors
|
|
|
|
@port = deprecated_options[0] || port
|
2016-07-25 13:04:50 -04:00
|
|
|
@port ||= Capybara::Server.ports[port_key]
|
2016-03-18 17:34:58 -04:00
|
|
|
@port ||= find_available_port(host)
|
2018-06-05 22:01:20 -04:00
|
|
|
@checker = Checker.new(@host, @port)
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
|
2012-07-13 08:57:43 -04:00
|
|
|
def reset_error!
|
2018-08-17 16:57:12 -04:00
|
|
|
middleware.clear_error
|
2012-07-13 08:57:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error
|
2016-07-25 15:12:59 -04:00
|
|
|
middleware.error
|
2012-07-13 08:57:43 -04:00
|
|
|
end
|
|
|
|
|
2018-05-01 16:36:42 -04:00
|
|
|
def using_ssl?
|
2018-06-05 22:01:20 -04:00
|
|
|
@checker.ssl?
|
2018-05-01 16:36:42 -04:00
|
|
|
end
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def responsive?
|
2018-05-10 16:20:23 -04:00
|
|
|
return false if @server_thread&.join(0)
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-06-05 22:01:20 -04:00
|
|
|
res = @checker.request { |http| http.get('/__identify__') }
|
2010-07-14 16:54:36 -04:00
|
|
|
|
2018-10-12 19:21:53 -04:00
|
|
|
return res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)
|
2018-08-29 09:51:53 -04:00
|
|
|
rescue SystemCallError, Net::ReadTimeout, OpenSSL::SSL::SSLError
|
2018-05-14 17:30:34 -04:00
|
|
|
false
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2015-07-06 19:19:47 -04:00
|
|
|
def wait_for_pending_requests
|
2018-06-06 13:51:25 -04:00
|
|
|
timer = Capybara::Helpers.timer(expire_in: 60)
|
2018-04-18 10:52:37 -04:00
|
|
|
while pending_requests?
|
2019-10-07 20:26:48 -04:00
|
|
|
raise "Requests did not finish in 60 seconds: #{middleware.pending_requests}" if timer.expired?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-04-18 10:52:37 -04:00
|
|
|
sleep 0.01
|
|
|
|
end
|
2015-07-06 19:19:47 -04:00
|
|
|
end
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
def boot
|
2012-07-13 09:19:20 -04:00
|
|
|
unless responsive?
|
2016-07-25 15:12:59 -04:00
|
|
|
Capybara::Server.ports[port_key] = port
|
2010-10-08 11:30:08 -04:00
|
|
|
|
2012-07-13 09:19:20 -04:00
|
|
|
@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
|
2012-07-13 09:19:20 -04:00
|
|
|
|
2018-06-06 13:51:25 -04:00
|
|
|
timer = Capybara::Helpers.timer(expire_in: 60)
|
2018-04-18 10:52:37 -04:00
|
|
|
until responsive?
|
2018-07-10 17:18:39 -04:00
|
|
|
raise 'Rack application timed out during boot' if timer.expired?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-04-18 10:52:37 -04:00
|
|
|
@server_thread.join(0.1)
|
|
|
|
end
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2018-04-18 10:52:37 -04:00
|
|
|
|
2010-07-14 16:54:36 -04:00
|
|
|
self
|
2009-11-04 17:00:05 -05:00
|
|
|
end
|
2009-11-07 09:35:47 -05:00
|
|
|
|
2019-07-07 18:45:20 -04:00
|
|
|
def base_url
|
|
|
|
"http#{'s' if using_ssl?}://#{host}:#{port}"
|
|
|
|
end
|
|
|
|
|
2010-07-09 14:31:58 -04:00
|
|
|
private
|
2010-01-17 11:40:26 -05:00
|
|
|
|
2016-07-25 15:12:59 -04:00
|
|
|
def middleware
|
2018-05-16 23:04:24 -04:00
|
|
|
@middleware ||= Middleware.new(app, @reportable_errors, @extra_middleware)
|
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
|
|
|
|
|
2016-03-18 17:34:58 -04:00
|
|
|
def find_available_port(host)
|
2019-09-08 12:09:06 -04:00
|
|
|
server = TCPServer.new(host, 0)
|
|
|
|
port = server.addr[1]
|
|
|
|
server.close
|
|
|
|
|
|
|
|
# Workaround issue where some platforms (mac, ???) when passed a host
|
|
|
|
# of '0.0.0.0' will return a port that is only available on one of the
|
|
|
|
# ip addresses that resolves to, but the next binding to that port requires
|
|
|
|
# that port to be available on all ips
|
|
|
|
server = TCPServer.new(host, port)
|
2019-09-07 07:06:31 -04:00
|
|
|
port
|
2019-09-08 12:09:06 -04:00
|
|
|
rescue Errno::EADDRINUSE
|
|
|
|
retry
|
|
|
|
ensure
|
|
|
|
server&.close
|
2010-07-09 04:05:30 -04:00
|
|
|
end
|
2010-07-09 14:31:58 -04:00
|
|
|
end
|
2009-11-07 09:35:47 -05:00
|
|
|
end
|