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:

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#delete(url = '', client: :default, name: :nil, headers: nil, tags: []) ⇒ 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

  • headers (Hash) (defaults to: nil)

    Input headers here

  • tags (Array<String>) (defaults to: [])

    a list of tags to assign to the request

Returns:



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

def delete(url = '', client: :default, name: :nil, headers: nil, tags: [])
  store_request('outgoing', name:, tags:) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        client.delete(url, nil, headers)
      elsif url.match?(%r{\Ahttps?://})
        connection.delete(url, nil, 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, headers: nil, tags: []) ⇒ 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

  • headers (Hash) (defaults to: nil)

    Input headers here

  • tags (Array<String>) (defaults to: [])

    a list of tags to assign to the request

Returns:



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

def get(url = '', client: :default, name: nil, headers: nil, tags: [])
  store_request('outgoing', name:, tags:) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        client.get(url, nil, headers)
      elsif url.match?(%r{\Ahttps?://})
        connection.get(url, nil, 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


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

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, headers: nil, tags: []) ⇒ 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

  • headers (Hash) (defaults to: nil)

    Input headers here

  • tags (Array<String>) (defaults to: [])

    a list of tags to assign to the request

Returns:



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

def post(url = '', body: nil, client: :default, name: nil, headers: nil, tags: [])
  store_request('outgoing', name:, tags:) do
    tcp_exception_handler do
      client = http_client(client)

      if client
        client.post(url, body, headers)
      elsif url.match?(%r{\Ahttps?://})
        connection.post(url, body, 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, headers: nil, tags: []) ⇒ 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

  • headers (Hash) (defaults to: nil)

    Input headers here

  • tags (Array<String>) (defaults to: [])

    a list of tags to assign to the request

Returns:



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

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

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

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

      if client
        response = client.get(url, nil, headers) { |req| req.options.on_data = collector }
      elsif url.match?(%r{\Ahttps?://})
        response = connection.get(url, nil, 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