|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require "protocol/http1/connection" |
| 7 | +require "protocol/http/body/buffered" |
| 8 | +require "protocol/http/body/wrapper" |
| 9 | + |
| 10 | +require "connection_context" |
| 11 | + |
| 12 | +# A body wrapper whose close callback reads the client side of the socket to verify |
| 13 | +# that the response framing is already complete at close time. |
| 14 | +class StreamCheckingBody < Protocol::HTTP::Body::Wrapper |
| 15 | + attr_reader :client_data_at_close |
| 16 | + |
| 17 | + def initialize(body, client_socket) |
| 18 | + super(body) |
| 19 | + @client_socket = client_socket |
| 20 | + @client_data_at_close = nil |
| 21 | + end |
| 22 | + |
| 23 | + def close(error = nil) |
| 24 | + # Non-blocking read of everything available on the client socket right now. |
| 25 | + # If the response was fully flushed before close, all framing bytes are here. |
| 26 | + @client_data_at_close = +"" |
| 27 | + |
| 28 | + loop do |
| 29 | + @client_data_at_close << @client_socket.read_nonblock(65536) |
| 30 | + rescue IO::WaitReadable, EOFError |
| 31 | + break |
| 32 | + end |
| 33 | + |
| 34 | + super |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +describe Protocol::HTTP1::Connection do |
| 39 | + include_context ConnectionContext |
| 40 | + |
| 41 | + before do |
| 42 | + server.open! |
| 43 | + client.open! |
| 44 | + end |
| 45 | + |
| 46 | + with "#write_body_and_close" do |
| 47 | + it "flushes all data to the stream before closing body" do |
| 48 | + body = StreamCheckingBody.new( |
| 49 | + Protocol::HTTP::Body::Buffered.new(["Hello", " ", "World"]), |
| 50 | + sockets.first, |
| 51 | + ) |
| 52 | + |
| 53 | + server.write_body_and_close(body, false) |
| 54 | + |
| 55 | + expect(body.client_data_at_close).not.to be_nil |
| 56 | + expect(body.client_data_at_close).to be(:include?, "Hello") |
| 57 | + expect(body.client_data_at_close).to be(:include?, "World") |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + with "#write_chunked_body" do |
| 62 | + it "writes terminal chunk before closing body" do |
| 63 | + body = StreamCheckingBody.new( |
| 64 | + Protocol::HTTP::Body::Buffered.new(["Hello", " ", "World"]), |
| 65 | + sockets.first, |
| 66 | + ) |
| 67 | + |
| 68 | + server.write_chunked_body(body, false) |
| 69 | + |
| 70 | + expect(body.client_data_at_close).not.to be_nil |
| 71 | + # Terminal chunk should already be on the wire: |
| 72 | + expect(body.client_data_at_close).to be(:include?, "\r\n0\r\n\r\n") |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + with "#write_fixed_length_body" do |
| 77 | + it "writes all data before closing body" do |
| 78 | + body = StreamCheckingBody.new( |
| 79 | + Protocol::HTTP::Body::Buffered.new(["Hello", " ", "World"]), |
| 80 | + sockets.first, |
| 81 | + ) |
| 82 | + |
| 83 | + server.write_fixed_length_body(body, 11, false) |
| 84 | + |
| 85 | + expect(body.client_data_at_close).not.to be_nil |
| 86 | + expect(body.client_data_at_close).to be(:include?, "Hello World") |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments