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
Instance Method Summary collapse
-
#delete(url = '', client: :default, name: :nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP DELETE request.
-
#find_http_client_definition(client) ⇒ Object
-
#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.
-
#post(url = '', body: nil, client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP POST request.
-
#put(url = '', body: nil, client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP PUT 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.
Instance Method Details
#delete(url = '', client: :default, name: :nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP DELETE request
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/inferno/dsl/http_client.rb', line 168 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
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 |
#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 |
#put(url = '', body: nil, client: :default, name: nil, headers: nil, tags: []) ⇒ Inferno::Entities::Request
Perform an HTTP PUT request
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/inferno/dsl/http_client.rb', line 141 def put(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.put(url, body, headers) elsif url.match?(%r{\Ahttps?://}) connection.put(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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/inferno/dsl/http_client.rb', line 199 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 |