Module: Inferno::DSL::HTTPClient

Defined in:
lib/inferno/dsl/http_client.rb

Overview

This module contains the HTTP DSL available to test writers.

Examples:

class MyTestGroup < Inferno::TestGroup
  # create a "default" client for a group
  http_client do
    url 'https://example.com/'
  end

  test :some_test do
    run do
      # performs a GET to https://example.com
      get
      # performs a GET to https://example.com/abc
      get('abc')

      request  # the most recent request
      response # the most recent response
      requests # all of the requests which have been made in this test
    end
  end
end

See Also:

  • Documentation for the client configuration DSL

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#delete(url = '', client: :default, name: :nil, **options) ⇒ Inferno::Entities::Request

Perform an HTTP DELETE request

Parameters:

  • url (String) (defaults to: '')

    if this request is using a defined client, this will be appended to the client's url. Must be an absolute url for requests made without a defined client

  • client (Symbol) (defaults to: :default)
  • name (Symbol) (defaults to: :nil)

    Name for this request to allow it to be used by other tests

Returns:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/inferno/dsl/http_client.rb', line 133

def delete(url = '', client: :default, name: :nil, **options)
  store_request('outgoing', name) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        client.delete(url, nil, options[:headers])
      elsif url.match?(%r{\Ahttps?://})
        connection.delete(url, nil, options[:headers])
      else
        raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
      end
    end
  end
end

#get(url = '', client: :default, name: nil, **options) ⇒ Inferno::Entities::Request

Perform an HTTP GET request

Parameters:

  • url (String) (defaults to: '')

    if this request is using a defined client, this will be appended to the client's url. Must be an absolute url for requests made without a defined client

  • client (Symbol) (defaults to: :default)
  • name (Symbol) (defaults to: nil)

    Name for this request to allow it to be used by other tests

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • Input (Hash)

    headers here - headers are optional and must be entered as the last piece of input to this method

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/inferno/dsl/http_client.rb', line 72

def get(url = '', client: :default, name: nil, **options)
  store_request('outgoing', name) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        client.get(url, nil, options[:headers])
      elsif url.match?(%r{\Ahttps?://})
        connection.get(url, nil, options[:headers])
      else
        raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
      end
    end
  end
end

#http_client(client = :default) ⇒ Faraday::Connection

Return a previously defined HTTP client

Parameters:

  • client (Symbol) (defaults to: :default)

    the name of the client

Returns:

  • (Faraday::Connection)

See Also:

  • HTTPClientBuilder


45
46
47
48
49
50
51
52
53
54
# File 'lib/inferno/dsl/http_client.rb', line 45

def http_client(client = :default)
  return http_clients[client] if http_clients[client]

  definition = self.class.http_client_definitions[client]
  return nil if definition.nil?

  tcp_exception_handler do
    http_clients[client] = HTTPClientBuilder.new.build(self, definition)
  end
end

#post(url = '', body: nil, client: :default, name: nil, **options) ⇒ Inferno::Entities::Request

Perform an HTTP POST request

Parameters:

  • url (String) (defaults to: '')

    if this request is using a defined client, this will be appended to the client's url. Must be an absolute url for requests made without a defined client

  • body (String) (defaults to: nil)
  • client (Symbol) (defaults to: :default)
  • name (Symbol) (defaults to: nil)

    Name for this request to allow it to be used by other tests

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • Input (Hash)

    headers here - headers are optional and must be entered as the last piece of input to this method

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/inferno/dsl/http_client.rb', line 108

def post(url = '', body: nil, client: :default, name: nil, **options)
  store_request('outgoing', name) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        client.post(url, body, options[:headers])
      elsif url.match?(%r{\Ahttps?://})
        connection.post(url, body, options[:headers])
      else
        raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
      end
    end
  end
end

#stream(block, url = '', limit = 100, client: :default, name: nil, **options) ⇒ Inferno::Entities::Request

Perform an HTTP GET request and stream the response

Parameters:

  • block (Proc)

    A Proc object whose input will be the string chunks received while streaming response to the get request.

  • url (String) (defaults to: '')

    If this request is using a defined client, this will be appended to the client's url. Must be an absolute url for requests made without a defined client

  • limit (Integer) (defaults to: 100)

    The number of streamed-in chunks to be stored in the response body. This optional input defaults to 100.

  • client (Symbol) (defaults to: :default)
  • name (Symbol) (defaults to: nil)

    Name for this request to allow it to be used by other tests

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • Input (Hash)

    headers here - headers are optional and must be entered as the last piece of input to this method

Returns:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/inferno/dsl/http_client.rb', line 164

def stream(block, url = '', limit = 100, client: :default, name: nil, **options)
  streamed = []

  collector = proc do |chunk, bytes|
    streamed << chunk if limit.positive?
    limit -= 1
    block.call(chunk, bytes)
  end

  store_request('outgoing', name) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        response = client.get(url, nil, options[:headers]) { |req| req.options.on_data = collector }
      elsif url.match?(%r{\Ahttps?://})
        response = connection.get(url, nil, options[:headers]) { |req| req.options.on_data = collector }
      else
        raise StandardError, 'Must use an absolute url or define an HTTP client with a base url'
      end
      response.env.body = streamed.join
      response
    end
  end
end