Class: DPay::Testing::MockHttpClient
- Inherits:
-
HTTP::Client
- Object
- HTTP::Client
- DPay::Testing::MockHttpClient
- Defined in:
- lib/dpay/testing/mock_http_client.rb,
sig/dpay/testing/mock_http_client.rbs
Instance Attribute Summary collapse
-
#queued ⇒ Array[HTTP::Response]
readonly
Returns the value of attribute queued.
-
#requests ⇒ Array[HTTP::Request]
readonly
Returns the value of attribute requests.
Instance Method Summary collapse
-
#initialize ⇒ MockHttpClient
constructor
A new instance of MockHttpClient.
- #last_request ⇒ HTTP::Request
- #last_request_body ⇒ Hash[String, untyped]
- #queue(response) ⇒ self
- #queue_json(status, body, headers = {}) ⇒ self
- #queue_text(status, body, headers = {}) ⇒ self
- #request(api_request) ⇒ HTTP::Response
Constructor Details
#initialize ⇒ MockHttpClient
Returns a new instance of MockHttpClient.
10 11 12 13 14 |
# File 'lib/dpay/testing/mock_http_client.rb', line 10 def initialize super @queued = [] @requests = [] end |
Instance Attribute Details
#queued ⇒ Array[HTTP::Response] (readonly)
Returns the value of attribute queued.
8 9 10 |
# File 'lib/dpay/testing/mock_http_client.rb', line 8 def queued @queued end |
#requests ⇒ Array[HTTP::Request] (readonly)
Returns the value of attribute requests.
8 9 10 |
# File 'lib/dpay/testing/mock_http_client.rb', line 8 def requests @requests end |
Instance Method Details
#last_request ⇒ HTTP::Request
37 38 39 40 41 |
# File 'lib/dpay/testing/mock_http_client.rb', line 37 def last_request raise "MockHttpClient recorded no requests" if @requests.empty? @requests.last end |
#last_request_body ⇒ Hash[String, untyped]
43 44 45 46 47 48 |
# File 'lib/dpay/testing/mock_http_client.rb', line 43 def last_request_body body = last_request.body raise "MockHttpClient last request has no body" if body.nil? JSON.parse(body) end |
#queue(response) ⇒ self
16 17 18 19 |
# File 'lib/dpay/testing/mock_http_client.rb', line 16 def queue(response) @queued << response self end |
#queue_json(status, body, headers = {}) ⇒ self
21 22 23 24 |
# File 'lib/dpay/testing/mock_http_client.rb', line 21 def queue_json(status, body, headers = {}) queue(HTTP::Response.new(status, { "content-type" => "application/json" }.merge(headers), Internal::PHP.json_encode(body))) end |
#queue_text(status, body, headers = {}) ⇒ self
26 27 28 |
# File 'lib/dpay/testing/mock_http_client.rb', line 26 def queue_text(status, body, headers = {}) queue(HTTP::Response.new(status, headers, body)) end |
#request(api_request) ⇒ HTTP::Response
30 31 32 33 34 35 |
# File 'lib/dpay/testing/mock_http_client.rb', line 30 def request(api_request) @requests << api_request raise "MockHttpClient queue is empty" if @queued.empty? @queued.shift end |