httpx/test/error_response_test.rb
HoneyryderChuck 8b2ee0b466 remove form, json, ,xml and body from the Options class
Options become a bunch of session and connection level parameters, and requests do not need to maintain a separate Options object when they contain a body anymore, instead, objects is shared with the session, while request-only parameters get passed downwards to the request and its body. This reduces allocations of Options, currently the heaviest object to manage.
2024-06-11 18:23:45 +01:00

59 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require_relative "test_helper"
class ErrorResponseTest < Minitest::Test
include HTTPX
def test_error_response_finished?
r1 = make_error_response(RuntimeError.new("wow"))
assert r1.finished?
end
def test_error_response_error
error = RuntimeError.new("wow")
r1 = make_error_response(error)
assert r1.error == error
end
def test_error_response_raise_for_status
some_error = Class.new(RuntimeError)
r1 = make_error_response(some_error.new("wow"))
assert_raises(some_error) { r1.raise_for_status }
end
def test_error_response_to_s
r = make_error_response(RuntimeError.new("wow"))
str = r.to_s
assert str.match(/wow \(.*RuntimeError.*\)/), "expected \"wow (RuntimeError)\" in \"#{str}\""
end
def test_error_response_close
response = Response.new(request_mock, 200, "1.1", {})
request_mock.response = response
r = make_error_response(RuntimeError.new("wow"))
assert !response.body.closed?
r.close
assert response.body.closed?
end
def test_error_response_body_write
response = Response.new(request_mock, 200, "1.1", {})
request_mock.response = response
r = ErrorResponse.new(request_mock, RuntimeError.new("wow"), {})
assert response.body.empty?, "body should be empty after init"
r << "data"
assert response.body == "data", "body should have been updated"
end
private
def request_mock
@request_mock ||= Request.new("GET", "http://example.com/", Options.new)
end
def make_error_response(*args)
ErrorResponse.new(request_mock, *args, request_mock.options)
end
end