|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require "async/http/protocol/http2" |
| 7 | +require "sus/fixtures/async/http" |
| 8 | +require "protocol/http/body/wrapper" |
| 9 | + |
| 10 | +describe Async::HTTP::Protocol::HTTP2 do |
| 11 | + with "response body close on stream error" do |
| 12 | + include Sus::Fixtures::Async::HTTP::ServerContext |
| 13 | + let(:protocol) {subject} |
| 14 | + |
| 15 | + let(:body_closed) {Async::Variable.new} |
| 16 | + |
| 17 | + let(:app) do |
| 18 | + variable = body_closed |
| 19 | + |
| 20 | + Protocol::HTTP::Middleware.for do |request| |
| 21 | + inner_body = Protocol::HTTP::Body::Buffered.new(["Hello World"]) |
| 22 | + |
| 23 | + tracking_body = Class.new(Protocol::HTTP::Body::Wrapper) do |
| 24 | + define_method(:close) do |error = nil| |
| 25 | + super(error) |
| 26 | + variable.value = true |
| 27 | + end |
| 28 | + end.new(inner_body) |
| 29 | + |
| 30 | + if request.respond_to?(:stream) && request.stream |
| 31 | + Async do |
| 32 | + sleep(0.01) |
| 33 | + request.stream.send_reset_stream(Protocol::HTTP2::NO_ERROR) |
| 34 | + end |
| 35 | + |
| 36 | + sleep(0.05) |
| 37 | + end |
| 38 | + |
| 39 | + Protocol::HTTP::Response[200, {}, tracking_body] |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + it "closes the response body when the stream is reset before sending" do |
| 44 | + expect do |
| 45 | + response = client.get("/") |
| 46 | + end.to raise_exception(Exception) |
| 47 | + |
| 48 | + # Wait up to 1 second for the body to be closed. Without the fix, |
| 49 | + # close is never called and this times out → test failure. |
| 50 | + result = Async::Task.current.with_timeout(1.0) do |
| 51 | + body_closed.wait |
| 52 | + rescue Async::TimeoutError |
| 53 | + nil |
| 54 | + end |
| 55 | + |
| 56 | + expect(result).to be == true |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments