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



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

def initialize(app, port=Capybara.server_port, host=Capybara.server_host)
  @app = app
  @server_thread = nil # suppress warnings
  @host, @port = host, port
  @port ||= Capybara::Server.ports[port_key]
  @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



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

def boot
  unless responsive?
    Capybara::Server.ports[port_key] = 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



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

def error
  middleware.error
end

#reset_error!Object



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

def reset_error!
  middleware.error = nil
end

#responsive?Boolean



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

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



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

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