Class: Pact::Server

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

Defined Under Namespace

Classes: Middleware

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, host, port, options = {}) ⇒ Server

Returns a new instance of Server.



39
40
41
42
43
44
45
46
# File 'lib/pact/consumer/server.rb', line 39

def initialize(app, host, port, options = {})
  @app = app
  @middleware = Middleware.new(@app)
  @server_thread = nil
  @host = host
  @port = port
  @options = options
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



37
38
39
# File 'lib/pact/consumer/server.rb', line 37

def app
  @app
end

#hostObject (readonly)

Returns the value of attribute host.



37
38
39
# File 'lib/pact/consumer/server.rb', line 37

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'lib/pact/consumer/server.rb', line 37

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



37
38
39
# File 'lib/pact/consumer/server.rb', line 37

def port
  @port
end

Class Method Details

.portsObject



32
33
34
# File 'lib/pact/consumer/server.rb', line 32

def ports
  @ports ||= {}
end

Instance Method Details

#bootObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pact/consumer/server.rb', line 99

def boot
  unless responsive?
    @server_thread = Thread.new { run_default_server(@middleware, @port) }
    Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
  end
rescue Timeout::Error
  raise "Rack application timed out during boot"
else
  Pact::Server.ports[@app.object_id] = @port
  self
end

#errorObject



52
53
54
# File 'lib/pact/consumer/server.rb', line 52

def error
  @middleware.error
end

#get_identityObject



75
76
77
78
79
80
81
82
83
# File 'lib/pact/consumer/server.rb', line 75

def get_identity
  return false unless @port
  http = Net::HTTP.new host, @port
  if options[:ssl]
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http.get('/__identify__')
end

#reset_error!Object



48
49
50
# File 'lib/pact/consumer/server.rb', line 48

def reset_error!
  @middleware.error = nil
end

#responsive?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pact/consumer/server.rb', line 56

def responsive?
  return false if @server_thread && @server_thread.join(0)
  res = get_identity
  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
rescue EOFError
  return false
end

#run_default_server(app, port) ⇒ Object



68
69
70
71
72
73
# File 'lib/pact/consumer/server.rb', line 68

def run_default_server(app, port)
  require 'rack/handler/webrick'
  Rack::Handler::WEBrick.run(app, **webrick_opts) do |server|
    @port = server[:Port]
  end
end

#ssl_optsObject



95
96
97
# File 'lib/pact/consumer/server.rb', line 95

def ssl_opts
  { SSLEnable: true, SSLCertName: [ ["CN", host] ] }
end

#webrick_optsObject



85
86
87
88
89
90
91
92
93
# File 'lib/pact/consumer/server.rb', line 85

def webrick_opts
  opts = { Host: host.nil? ? 'localhost' : host, Port: port.nil? ? 0 : port, AccessLog: [], Logger: WEBrick::Log::new(nil, 0) }
  opts.merge!({
    :SSLCertificate => OpenSSL::X509::Certificate.new(File.open(options[:sslcert]).read) }) if options[:sslcert]
  opts.merge!({
    :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open(options[:sslkey]).read) }) if options[:sslkey]
  opts.merge!(ssl_opts) if options[:ssl]
  opts
end