test
Nested
Definitions
FakeBody
Simulates a response body that supports .each (streaming), .length, and .close — matching the Protocol::HTTP::Body interface.
Implementation
FakeBody = Struct.new(:data, :content_length, :closed) do
def initialize(data, content_length: nil)
super(data, content_length, false)
end
def each(&block)
data.each_char.each_slice(64) { |chars| block.call(chars.join) } if data
end
def length
content_length
end
def close
self.closed = true
end
end
FakeResponse
Minimal response stub with status, body, and optional headers.
Implementation
FakeResponse = Struct.new(:status, :body, :header_hash) do
def headers
header_hash || {}
end
def close
body&.close
end
end
FakeInternet
A controllable replacement for Async::HTTP::Internet.
Implementation
FakeInternet = Struct.new(:responses, :call_count) do
def initialize(responses)
super(responses, 0)
end
def call(method, url, headers, body_data)
resp = responses[call_count] || responses.last
self.call_count += 1
resp
end
def close; end
end