Class: Beaker::Http::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers
Defined in:
lib/beaker-http/http.rb

Overview

Beaker::Http::Connection object instantiation examples

These examples are for using the Connection object directly. If you are trying to use this from within a test, consider using the DSL constructors instead.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#get_host_cacert, #get_host_cert, #get_host_private_key

Constructor Details

#initialize(options) ⇒ Connection

Beaker::Http::Connection objects can be instantiated with object that utilizes a object for easier setup during testing.

Parameters:

  • options (Hash)

    Typically the global options provided by Beaker.

Options Hash (options):

  • :logger (Beaker::Logger)
  • :log_http_bodies (Boolean)


21
22
23
# File 'lib/beaker-http/http.rb', line 21

def initialize(options)
  @connection = create_default_connection(options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/beaker-http/http.rb', line 13

def connection
  @connection
end

Instance Method Details

#configure_cacert_with_puppet(host) ⇒ Object

Use this method if you are connecting as a user to the system; it will provide the correct SSL context but not provide authentication.



70
71
72
73
74
# File 'lib/beaker-http/http.rb', line 70

def configure_cacert_with_puppet(host)
  set_cacert(get_host_cacert(host))
  connection.host = host.hostname
  nil
end

#configure_private_key_and_cert_with_puppet(host) ⇒ Object

Use this method if you want to connect to the system using certificate based authentication. This method will provide the ssl context and use the private key and cert from the host provided for authentication.



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

def configure_private_key_and_cert_with_puppet(host)
  configure_cacert_with_puppet(host)

  client_key_raw = get_host_private_key(host)
  client_cert_raw = get_host_cert(host)

  ssl['client_key'] = OpenSSL::PKey.read(client_key_raw)
  ssl['client_cert'] = OpenSSL::X509::Certificate.new(client_cert_raw)

  nil
end

#create_default_connection(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/beaker-http/http.rb', line 27

def create_default_connection(options)
  Faraday.new do |conn|
    conn.request :json

    conn.response :follow_redirects
    conn.response :raise_error
    conn.response :json, :content_type => /\bjson$/

    # We can supply a third argument, a Hash with key of :bodies set to true or false,
    # to configure whether or not to log http bodies in requests and responses.
    # However, to uncomplicate things, we will just use the default
    # set in the middleware and not allow the http log level to be set
    # independently of the beaker log level. If we find that we should allow setting
    # of http bodies independent of the beaker log level, we should expose that setting
    # here.
    conn.response :faraday_beaker_logger, options[:logger]

    conn.adapter :net_http
  end
end

#remove_error_checkingObject

If you would like to run tests that expect 400 or even 500 responses, apply this method to remove the :raise_error middleware.



50
51
52
53
# File 'lib/beaker-http/http.rb', line 50

def remove_error_checking
  connection.builder.delete(Faraday::Response::RaiseError)
  nil
end

#set_cacert(ca_file) ⇒ Object



55
56
57
58
# File 'lib/beaker-http/http.rb', line 55

def set_cacert(ca_file)
  ssl['ca_file'] = ca_file
  url_prefix.scheme = 'https'
end

#set_client_cert(client_cert) ⇒ Object



64
65
66
# File 'lib/beaker-http/http.rb', line 64

def set_client_cert(client_cert)
  ssl['client_cert'] = client_cert
end

#set_client_key(client_key) ⇒ Object



60
61
62
# File 'lib/beaker-http/http.rb', line 60

def set_client_key(client_key)
  ssl['client_key'] = client_key
end