1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2023-2025, by Samuel Williams.
require "protocol/http/request"
require "json"
describe Protocol::HTTP::Request do
let(:headers) {Protocol::HTTP::Headers.new}
let(:body) {nil}
with ".[]" do
let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")}
let(:headers) {Protocol::HTTP::Headers[{"accept" => "text/html"}]}
it "creates a new request" do
request = subject["GET", "/index.html", headers]
expect(request).to have_attributes(
scheme: be_nil,
authority: be_nil,
method: be == "GET",
path: be == "/index.html",
version: be_nil,
headers: be == headers,
body: be_nil,
protocol: be_nil
)
end
it "creates a new request with keyword arguments" do
request = subject["GET", "/index.html", scheme: "http", authority: "localhost", headers: headers, body: body]
expect(request).to have_attributes(
scheme: be == "http",
authority: be == "localhost",
method: be == "GET",
path: be == "/index.html",
version: be_nil,
headers: be == headers,
body: be == body,
protocol: be_nil
)
end
it "converts header hash to headers instance" do
request = subject["GET", "/index.html", {"accept" => "text/html"}]
expect(request).to have_attributes(
headers: be == headers,
)
end
it "converts array body to buffered body" do
request = subject["GET", "/index.html", headers: headers, body: ["Hello, World!"]]
expect(request).to have_attributes(
body: be_a(Protocol::HTTP::Body::Buffered)
)
end
it "can accept no arguments" do
request = subject["GET"]
expect(request).to have_attributes(
method: be == "GET",
path: be_nil,
)
end
it "converts path to string" do
request = subject["GET", :index]
expect(request).to have_attributes(
method: be == "GET",
path: be == "index",
)
end
end
with "simple GET request" do
let(:request) {subject.new("http", "localhost", "GET", "/index.html", "HTTP/1.0", headers, body)}
it "should have attributes" do
expect(request).to have_attributes(
scheme: be == "http",
authority: be == "localhost",
method: be == "GET",
path: be == "/index.html",
version: be == "HTTP/1.0",
headers: be == headers,
body: be == body,
protocol: be_nil,
peer: be_nil,
)
end
with "#as_json" do
it "generates a JSON representation" do
expect(request.as_json).to be == {
scheme: "http",
authority: "localhost",
method: "GET",
path: "/index.html",
version: "HTTP/1.0",
headers: headers.as_json,
body: nil,
protocol: nil
}
end
it "generates a JSON string" do
expect(JSON.dump(request)).to be == request.to_json
end
end
it "should not be HEAD" do
expect(request).not.to be(:head?)
end
it "should not be CONNECT" do
expect(request).not.to be(:connect?)
end
it "should be idempotent" do
expect(request).to be(:idempotent?)
end
it "should have a string representation" do
expect(request.to_s).to be == "http://localhost: GET /index.html HTTP/1.0"
end
it "can apply the request to a connection" do
connection = proc{|request| request}
expect(connection).to receive(:call).with(request)
request.call(connection)
end
end
with "interim response" do
let(:request) {subject.new("http", "localhost", "GET", "/index.html", "HTTP/1.0", headers, body)}
it "should call block" do
request.on_interim_response do |status, headers|
expect(status).to be == 100
expect(headers).to be == {}
end
request.send_interim_response(100, {})
end
it "calls multiple blocks" do
sequence = []
request.on_interim_response do |status, headers|
sequence << 1
expect(status).to be == 100
expect(headers).to be == {}
end
request.on_interim_response do |status, headers|
sequence << 2
expect(status).to be == 100
expect(headers).to be == {}
end
request.send_interim_response(100, {})
expect(sequence).to be == [2, 1]
end
end
end
|