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

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



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

- (Object) app (readonly)

Returns the value of attribute app



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

def app
  @app
end

- (Object) host (readonly)

Returns the value of attribute host



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

def host
  @host
end

- (Object) port (readonly)

Returns the value of attribute port



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

def port
  @port
end

Class Method Details

+ (Object) ports



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

def ports
  @ports ||= {}
end

Instance Method Details

- (Object) boot



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

- (Object) error



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

def error
  @middleware.error
end

- (Object) reset_error!



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

def reset_error!
  @middleware.error = nil
end

- (Boolean) responsive?



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