mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-06 00:02:08 -04:00
69 lines
2.0 KiB
Ruby
69 lines
2.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Requests
|
|
module ResponseBody
|
|
def test_http_response_copy_to_file
|
|
file = Tempfile.new(%w[cat .jpeg])
|
|
uri = build_uri("/image")
|
|
response = HTTPX.get(uri, headers: { "accept" => "image/jpeg" })
|
|
verify_status(response, 200)
|
|
verify_header(response.headers, "content-type", "image/jpeg")
|
|
response.copy_to(file)
|
|
verify_body_length(response)
|
|
content_length = response.headers["content-length"].to_i
|
|
assert file.size == content_length, "file should contain the content of response"
|
|
ensure
|
|
if file
|
|
file.close
|
|
file.unlink
|
|
end
|
|
end
|
|
|
|
def test_http_response_copy_to_io
|
|
io = StringIO.new
|
|
uri = build_uri("/image")
|
|
response = HTTPX.get(uri, headers: { "accept" => "image/jpeg" })
|
|
verify_status(response, 200)
|
|
verify_header(response.headers, "content-type", "image/jpeg")
|
|
response.copy_to(io)
|
|
content_length = response.headers["content-length"].to_i
|
|
assert io.size == content_length, "file should contain the content of response"
|
|
ensure
|
|
io.close if io
|
|
end
|
|
|
|
def test_http_response_buffer_to_custom
|
|
uri = build_uri("/")
|
|
custom_body = Class.new(HTTPX::Response::Body) do
|
|
attr_reader :file
|
|
|
|
def initialize(_response, _opts)
|
|
super
|
|
@file = Tempfile.new("httpx-test")
|
|
end
|
|
|
|
def write(data)
|
|
@file << data
|
|
end
|
|
|
|
def close
|
|
return unless @file
|
|
|
|
@file.close
|
|
@file.unlink
|
|
end
|
|
end
|
|
|
|
response = HTTPX.with(response_body_class: custom_body).get(uri)
|
|
verify_status(response, 200)
|
|
assert response.body.is_a?(custom_body), "body should be from custom type"
|
|
file = response.body.file
|
|
file.rewind
|
|
content_length = response.headers["content-length"].to_i
|
|
assert file.size == content_length, "didn't buffer the whole body"
|
|
ensure
|
|
response.close if response
|
|
end
|
|
end
|
|
end
|