Class: Capybara::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/server.rb

Defined Under Namespace

Classes: Middleware

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, port = Capybara.server_port, host = Capybara.server_host) ⇒ Server

Returns a new instance of Server



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

def initialize(app, port=Capybara.server_port, host=Capybara.server_host)
  @app = app
  @middleware = Middleware.new(@app)
  @server_thread = nil # suppress warnings
  @host, @port = host, port
  @port ||= Capybara::Server.ports[Capybara.reuse_server ? @app.object_id : @middleware.object_id]
  @port ||= find_available_port(host)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app



60
61
62
# File 'lib/capybara/server.rb', line 60

def app
  @app
end

#hostObject (readonly)

Returns the value of attribute host



60
61
62
# File 'lib/capybara/server.rb', line 60

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port



60
61
62
# File 'lib/capybara/server.rb', line 60

def port
  @port
end

Class Method Details

.portsObject



55
56
57
# File 'lib/capybara/server.rb', line 55

def ports
  @ports ||= {}
end

Instance Method Details

#bootObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/capybara/server.rb', line 97

def boot
  unless responsive?
    Capybara::Server.ports[Capybara.reuse_server ? @app.object_id : @middleware.object_id] = @port

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

    Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
  end
rescue Timeout::Error
  raise "Rack application timed out during boot"
else
  self
end

#errorObject



75
76
77
# File 'lib/capybara/server.rb', line 75

def error
  @middleware.error
end

#reset_error!Object



71
72
73
# File 'lib/capybara/server.rb', line 71

def reset_error!
  @middleware.error = nil
end

#responsive?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/capybara/server.rb', line 79

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

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

#wait_for_pending_requestsObject



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

def wait_for_pending_requests
  Timeout.timeout(60) { sleep(0.01) while @middleware.pending_requests? }
rescue Timeout::Error
  raise "Requests did not finish in 60 seconds"
end