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

127 lines
3.2 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'
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
# @api private
2010-07-09 14:31:58 -04:00
class Server
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,
extra_middleware: [])
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
@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]
@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
def reset_error!
2018-08-17 16:57:12 -04:00
middleware.clear_error
end
def error
2016-07-25 15:12:59 -04:00
middleware.error
end
def using_ssl?
2018-06-05 22:01:20 -04:00
@checker.ssl?
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__') }
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)
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
def wait_for_pending_requests
timer = Capybara::Helpers.timer(expire_in: 60)
while pending_requests?
raise "Requests did not finish in 60 seconds: #{middleware.pending_requests}" if timer.expired?
2018-09-24 12:43:46 -04:00
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
timer = Capybara::Helpers.timer(expire_in: 60)
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
@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
def base_url
"http#{'s' if using_ssl?}://#{host}:#{port}"
end
2010-07-09 14:31:58 -04:00
private
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
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)
port
2019-09-08 12:09:06 -04:00
rescue Errno::EADDRINUSE
retry
ensure
server&.close
end
2010-07-09 14:31:58 -04:00
end
2009-11-07 09:35:47 -05:00
end