Class: Capybara::Server Private

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/server.rb,
lib/capybara/server/checker.rb,
lib/capybara/server/middleware.rb,
lib/capybara/server/animation_disabler.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: AnimationDisabler, Checker, Middleware

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, *deprecated_options, port: Capybara.server_port, host: Capybara.server_host, reportable_errors: Capybara.server_errors, extra_middleware: []) ⇒ Server

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Server.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/capybara/server.rb', line 21

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
  @app = app
  @extra_middleware = extra_middleware
  @server_thread = nil # suppress warnings
  @host = deprecated_options[1] || host
  @reportable_errors = deprecated_options[2] || reportable_errors
  @port = deprecated_options[0] || port
  @port ||= Capybara::Server.ports[port_key]
  @port ||= find_available_port(host)
  @checker = Checker.new(@host, @port)
end

Instance Attribute Details

#appObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/capybara/server.rb', line 19

def app
  @app
end

#hostObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/capybara/server.rb', line 19

def host
  @host
end

#portObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/capybara/server.rb', line 19

def port
  @port
end

Class Method Details

.portsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/capybara/server.rb', line 14

def ports
  @ports ||= {}
end

Instance Method Details

#base_urlObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/capybara/server.rb', line 91

def base_url
  "http#{'s' if using_ssl?}://#{host}:#{port}"
end

#bootObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/capybara/server.rb', line 72

def boot
  unless responsive?
    Capybara::Server.ports[port_key] = port

    @server_thread = Thread.new do
      Capybara.server.call(middleware, port, host)
    end

    timer = Capybara::Helpers.timer(expire_in: 60)
    until responsive?
      raise 'Rack application timed out during boot' if timer.expired?

      @server_thread.join(0.1)
    end
  end

  self
end

#errorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/capybara/server.rb', line 45

def error
  middleware.error
end

#reset_error!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
# File 'lib/capybara/server.rb', line 41

def reset_error!
  middleware.clear_error
end

#responsive?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/capybara/server.rb', line 53

def responsive?
  return false if @server_thread&.join(0)

  res = @checker.request { |http| http.get('/__identify__') }

  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
  false
end

#using_ssl?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


49
50
51
# File 'lib/capybara/server.rb', line 49

def using_ssl?
  @checker.ssl?
end

#wait_for_pending_requestsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
66
67
68
69
70
# File 'lib/capybara/server.rb', line 63

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?

    sleep 0.01
  end
end