|
| 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 "stringio" |
| 8 | + |
| 9 | +describe Protocol::HTTP1::Connection do |
| 10 | + let(:stream) {StringIO.new("".b)} |
| 11 | + let(:connection) {subject.new(stream)} |
| 12 | + |
| 13 | + ["\r", "\n", "\0", "\r\n"].each do |character| |
| 14 | + with "a value containing #{character.inspect}", unique: character.inspect do |
| 15 | + let(:value) {"before#{character}after"} |
| 16 | + let(:headers) {Protocol::HTTP::Headers["location" => value]} |
| 17 | + |
| 18 | + it "rejects ordinary header values before writing them" do |
| 19 | + expect do |
| 20 | + connection.write_headers(headers) |
| 21 | + end.to raise_exception(Protocol::HTTP1::BadHeader) |
| 22 | + |
| 23 | + expect(stream.string).to be == "" |
| 24 | + end |
| 25 | + |
| 26 | + it "rejects request header values" do |
| 27 | + expect do |
| 28 | + connection.write_request(nil, "GET", "/", "HTTP/1.1", headers) |
| 29 | + end.to raise_exception(Protocol::HTTP::RefusedError) |
| 30 | + end |
| 31 | + |
| 32 | + it "rejects response header values" do |
| 33 | + connection.open! |
| 34 | + |
| 35 | + expect do |
| 36 | + connection.write_response("HTTP/1.1", 302, headers) |
| 37 | + end.to raise_exception(Protocol::HTTP1::BadHeader) |
| 38 | + end |
| 39 | + |
| 40 | + it "rejects interim response header values" do |
| 41 | + connection.open! |
| 42 | + |
| 43 | + expect do |
| 44 | + connection.write_interim_response("HTTP/1.1", 103, headers) |
| 45 | + end.to raise_exception(Protocol::HTTP1::BadHeader) |
| 46 | + end |
| 47 | + |
| 48 | + it "rejects the authority before writing any request bytes" do |
| 49 | + expect do |
| 50 | + connection.write_request(value, "GET", "/", "HTTP/1.1", {}) |
| 51 | + end.to raise_exception(Protocol::HTTP::RefusedError) |
| 52 | + |
| 53 | + expect(stream.string).to be == "" |
| 54 | + end |
| 55 | + |
| 56 | + it "rejects the target before writing any request bytes" do |
| 57 | + expect do |
| 58 | + connection.write_request("example.com", "GET", "/#{value}", "HTTP/1.1", {}) |
| 59 | + end.to raise_exception(Protocol::HTTP::RefusedError) |
| 60 | + |
| 61 | + expect(stream.string).to be == "" |
| 62 | + end |
| 63 | + |
| 64 | + it "rejects CONNECT targets before writing any request bytes" do |
| 65 | + expect do |
| 66 | + connection.write_request(nil, "CONNECT", "#{value}:443", "HTTP/1.1", {}) |
| 67 | + end.to raise_exception(Protocol::HTTP::RefusedError) |
| 68 | + |
| 69 | + expect(stream.string).to be == "" |
| 70 | + end |
| 71 | + |
| 72 | + it "rejects generated Upgrade header values before writing them" do |
| 73 | + expect do |
| 74 | + connection.write_upgrade_header(value) |
| 75 | + end.to raise_exception(Protocol::HTTP1::BadHeader) |
| 76 | + |
| 77 | + expect(stream.string).not.to be(:include?, "upgrade:") |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + it "preserves an empty header value" do |
| 83 | + connection.write_headers("x-empty" => "") |
| 84 | + |
| 85 | + expect(stream.string).to be == "x-empty: \r\n" |
| 86 | + end |
| 87 | + |
| 88 | + it "preserves all other bytes in header values" do |
| 89 | + value = ((0..255).to_a - [0, 10, 13]).pack("C*") |
| 90 | + connection.write_headers("x-bytes" => value) |
| 91 | + |
| 92 | + expect(stream.string).to be == "x-bytes: #{value}\r\n" |
| 93 | + end |
| 94 | + |
| 95 | + [Encoding::ASCII_8BIT, Encoding::UTF_8, Encoding::US_ASCII].each do |encoding| |
| 96 | + with "non-ASCII bytes tagged as #{encoding}", unique: encoding.name do |
| 97 | + let(:value) {"a\xFFb".b.force_encoding(encoding)} |
| 98 | + |
| 99 | + it "preserves the request target bytes" do |
| 100 | + connection.write_request("example.com", "GET", "/#{value}", "HTTP/1.1", {}) |
| 101 | + |
| 102 | + expect(stream.string.b).to be == "GET /a\xFFb HTTP/1.1\r\nhost: example.com\r\n".b |
| 103 | + expect(value.encoding).to be == encoding |
| 104 | + end |
| 105 | + |
| 106 | + it "preserves the authority bytes" do |
| 107 | + connection.write_request(value, "GET", "/", "HTTP/1.1", {}) |
| 108 | + |
| 109 | + expect(stream.string.b).to be == "GET / HTTP/1.1\r\nhost: a\xFFb\r\n".b |
| 110 | + expect(value.encoding).to be == encoding |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + it "writes a valid Host header" do |
| 116 | + connection.write_request("example.com", "GET", "/", "HTTP/1.1", {}) |
| 117 | + |
| 118 | + expect(stream.string).to be == "GET / HTTP/1.1\r\nhost: example.com\r\n" |
| 119 | + end |
| 120 | + |
| 121 | + it "preserves percent-encoded request targets" do |
| 122 | + connection.write_request("example.com", "GET", "/%00%0D%0A", "HTTP/1.1", {}) |
| 123 | + |
| 124 | + expect(stream.string).to be == "GET /%00%0D%0A HTTP/1.1\r\nhost: example.com\r\n" |
| 125 | + end |
| 126 | + |
| 127 | + it "writes a valid CONNECT target without a Host header" do |
| 128 | + connection.write_request(nil, "CONNECT", "example.com:443", "HTTP/1.1", {}) |
| 129 | + |
| 130 | + expect(stream.string).to be == "CONNECT example.com:443 HTTP/1.1\r\n" |
| 131 | + end |
| 132 | + |
| 133 | + it "writes valid upgrade headers" do |
| 134 | + connection.write_upgrade_header("websocket") |
| 135 | + |
| 136 | + expect(stream.string).to be == "connection: upgrade\r\nupgrade: websocket\r\n" |
| 137 | + end |
| 138 | +end |
0 commit comments