|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "protocol/http1/connection" |
| 7 | +require "protocol/http/body/buffered" |
| 8 | +require "protocol/http/body/writable" |
| 9 | + |
| 10 | +require "connection_context" |
| 11 | + |
| 12 | +describe Protocol::HTTP1::Connection do |
| 13 | + include_context ConnectionContext |
| 14 | + |
| 15 | + with "multiple requests in a single connection" do |
| 16 | + it "handles two back-to-back GET requests (HTTP/1.1 keep-alive)" do |
| 17 | + client.write_request("localhost", "GET", "/first", "HTTP/1.1", {"Header-A" => "Value-A"}) |
| 18 | + client.write_body("HTTP/1.1", nil) |
| 19 | + expect(client).to be(:half_closed_local?) |
| 20 | + |
| 21 | + # Server reads it: |
| 22 | + authority, method, path, version, headers, body = server.read_request |
| 23 | + expect(authority).to be == "localhost" |
| 24 | + expect(method).to be == "GET" |
| 25 | + expect(path).to be == "/first" |
| 26 | + expect(version).to be == "HTTP/1.1" |
| 27 | + expect(headers["header-a"]).to be == ["Value-A"] |
| 28 | + expect(body).to be_nil |
| 29 | + |
| 30 | + # Server writes a response: |
| 31 | + expect(server).to be(:half_closed_remote?) |
| 32 | + server.write_response("HTTP/1.1", 200, {"Res-A" => "ValA"}, "OK") |
| 33 | + server.write_body("HTTP/1.1", nil) |
| 34 | + expect(server).to be(:idle?) |
| 35 | + |
| 36 | + # Client reads first response: |
| 37 | + version, status, reason, headers, body = client.read_response("GET") |
| 38 | + expect(version).to be == "HTTP/1.1" |
| 39 | + expect(status).to be == 200 |
| 40 | + expect(reason).to be == "OK" |
| 41 | + expect(headers["res-a"]).to be == ["ValA"] |
| 42 | + expect(body).to be_nil |
| 43 | + |
| 44 | + # Now both sides should be back to :idle (persistent re-use): |
| 45 | + expect(client).to be(:idle?) |
| 46 | + expect(server).to be(:idle?) |
| 47 | + |
| 48 | + # Second request: |
| 49 | + client.write_request("localhost", "GET", "/second", "HTTP/1.1", {"Header-B" => "Value-B"}) |
| 50 | + client.write_body("HTTP/1.1", nil) |
| 51 | + expect(client).to be(:half_closed_local?) |
| 52 | + |
| 53 | + # Server reads it: |
| 54 | + authority, method, path, version, headers, body = server.read_request |
| 55 | + expect(authority).to be == "localhost" |
| 56 | + expect(method).to be == "GET" |
| 57 | + expect(path).to be == "/second" |
| 58 | + expect(version).to be == "HTTP/1.1" |
| 59 | + expect(headers["header-b"]).to be == ["Value-B"] |
| 60 | + expect(body).to be_nil |
| 61 | + |
| 62 | + # Server writes a response: |
| 63 | + expect(server).to be(:half_closed_remote?) |
| 64 | + server.write_response("HTTP/1.1", 200, {"Res-B" => "ValB"}, "OK") |
| 65 | + server.write_body("HTTP/1.1", nil) |
| 66 | + |
| 67 | + # Client reads second response: |
| 68 | + version, status, reason, headers, body = client.read_response("GET") |
| 69 | + expect(version).to be == "HTTP/1.1" |
| 70 | + expect(status).to be == 200 |
| 71 | + expect(reason).to be == "OK" |
| 72 | + expect(headers["res-b"]).to be == ["ValB"] |
| 73 | + expect(body).to be_nil |
| 74 | + |
| 75 | + # Confirm final states: |
| 76 | + expect(client).to be(:idle?) |
| 77 | + expect(server).to be(:idle?) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + with "partial body read" do |
| 82 | + it "closes correctly if server does not consume entire fixed-length body" do |
| 83 | + # Indicate Content-Length = 11 but only read part of it on server side: |
| 84 | + client.stream.write "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 11\r\n\r\nHello" |
| 85 | + client.stream.close |
| 86 | + |
| 87 | + # Server reads request line/headers: |
| 88 | + authority, method, path, version, headers, body = server.read_request |
| 89 | + expect(method).to be == "POST" |
| 90 | + expect(body).to be_a(Protocol::HTTP1::Body::Fixed) |
| 91 | + |
| 92 | + # Partially read 5 bytes only: |
| 93 | + partial = body.read |
| 94 | + expect(partial).to be == "Hello" |
| 95 | + |
| 96 | + expect do |
| 97 | + body.read |
| 98 | + end.to raise_exception(EOFError) |
| 99 | + |
| 100 | + # Then server forcibly closes read (simulating a deliberate stop): |
| 101 | + server.close_read |
| 102 | + |
| 103 | + # Because of partial consumption, that should move the state to half-closed remote or closed, etc. |
| 104 | + expect(server).to be(:half_closed_remote?) |
| 105 | + expect(server).not.to be(:persistent) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + with "no persistence" do |
| 110 | + it "closes connection after request" do |
| 111 | + server.persistent = false |
| 112 | + |
| 113 | + client.write_request("localhost", "GET", "/first", "HTTP/1.1", {"Header-A" => "Value-A"}) |
| 114 | + client.write_body("HTTP/1.1", nil) |
| 115 | + expect(client).to be(:half_closed_local?) |
| 116 | + |
| 117 | + # Server reads it: |
| 118 | + authority, method, path, version, headers, body = server.read_request |
| 119 | + expect(authority).to be == "localhost" |
| 120 | + expect(method).to be == "GET" |
| 121 | + expect(path).to be == "/first" |
| 122 | + expect(version).to be == "HTTP/1.1" |
| 123 | + expect(headers["header-a"]).to be == ["Value-A"] |
| 124 | + expect(body).to be_nil |
| 125 | + |
| 126 | + # Server writes a response: |
| 127 | + expect(server).to be(:half_closed_remote?) |
| 128 | + server.write_response("HTTP/1.1", 200, {"Res-A" => "ValA"}, "OK") |
| 129 | + server.write_body("HTTP/1.1", nil) |
| 130 | + expect(server).to be(:closed?) |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments