Module: Inferno::DSL::HTTPClient
- Defined in:
- lib/inferno/dsl/http_client.rb
Overview
This module contains the HTTP DSL available to test writers.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #connection ⇒ Object
-
#delete(url = '', client: :default, name: :nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP DELETE request.
-
#get(url = '', client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP GET request.
-
#http_client(client = :default) ⇒ Faraday::Connection
Return a previously defined HTTP client.
- #http_clients ⇒ Object
-
#post(url = '', body: nil, client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP POST request.
-
#stream(block, url = '', limit = 100, client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP GET request and stream the response.
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
#connection ⇒ Object
90 91 92 93 94 95 |
# File 'lib/inferno/dsl/http_client.rb', line 90 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
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
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
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 |
#http_clients ⇒ Object
58 59 60 |
# File 'lib/inferno/dsl/http_client.rb', line 58 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
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
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..on_data = collector } elsif url.match?(%r{\Ahttps?://}) response = connection.get(url, nil, headers) { |req| req..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 |