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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



35
36
37
38
39
# File 'lib/inferno/dsl/http_client.rb', line 35

def self.included(klass)
  klass.extend ClassMethods
  klass.include RequestStorage
  klass.include TCPExceptionHandler
end

Instance Method Details

#connectionObject



94
95
96
97
98
99
# File 'lib/inferno/dsl/http_client.rb', line 94

def connection
  Faraday.new do |f|
    f.request :url_encoded
    f.use FaradayMiddleware::FollowRedirects
  end
end

#delete(url = '', client: :default, name: :nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request

Perform an HTTP DELETE request



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/inferno/dsl/http_client.rb', line 140

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

#find_http_client_definition(client) ⇒ Object



57
58
59
# File 'lib/inferno/dsl/http_client.rb', line 57

def find_http_client_definition(client)
  self.class.find_http_client_definition(client)
end

#get(url = '', client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request

Perform an HTTP GET request



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inferno/dsl/http_client.rb', line 77

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

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 = find_http_client_definition(client)
  return nil if definition.nil?

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

#http_clientsObject



62
63
64
# File 'lib/inferno/dsl/http_client.rb', line 62

def http_clients
  @http_clients ||= {}
end

#post(url = '', body: nil, client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request

Perform an HTTP POST request



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/inferno/dsl/http_client.rb', line 113

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



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

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