Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP::Client spec: don't write server response if expecting read timeout #7402

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ require "openssl"
require "http/client"
require "http/server"

private def test_server(host, port, read_time = 0, content_type = "text/plain")
private def test_server(host, port, read_time = 0, content_type = "text/plain", write_response = true)
server = TCPServer.new(host, port)
begin
spawn do
io = server.accept
sleep read_time
response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => content_type}, body: "OK")
response.to_io(io)
io.flush
if write_response
response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => content_type}, body: "OK")
response.to_io(io)
io.flush
end
end

yield server
Expand Down Expand Up @@ -196,7 +198,11 @@ module HTTP
client.get("/")
end

test_server("localhost", 0, 0.5) do |server|
# Here we don't want to write a response on the server side because
# it doesn't make sense to try to write because the client will already
# timeout on read. Writing a response could lead on an exception in
# the server if the socket is closed.
test_server("localhost", 0, 0.5, write_response: false) do |server|
client = Client.new("localhost", server.local_address.port)
expect_raises(IO::Timeout, "Read timed out") do
client.read_timeout = 0.001
Expand Down