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.



36
37
38
39
40
41
42
43
# File 'lib/capybara/server.rb', line 36

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[@app.object_id]
  @port ||= find_available_port
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



34
35
36
# File 'lib/capybara/server.rb', line 34

def app
  @app
end

#hostObject (readonly)

Returns the value of attribute host.



34
35
36
# File 'lib/capybara/server.rb', line 34

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



34
35
36
# File 'lib/capybara/server.rb', line 34

def port
  @port
end

Class Method Details

.portsObject



29
30
31
# File 'lib/capybara/server.rb', line 29

def ports
  @ports ||= {}
end

Instance Method Details

#bootObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/capybara/server.rb', line 65

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

    @server_thread = Thread.new do
      Capybara.server.call(@middleware, @port)
    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



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

def error
  @middleware.error
end

#reset_error!Object



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

def reset_error!
  @middleware.error = nil
end

#responsive?Boolean

Returns:

  • (Boolean)


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

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