From f300b37bd4c701cb6e9f1ac8677e771c0ddf0a3d Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 31 Aug 2024 09:48:40 +1200 Subject: [PATCH] Support for keyword arguments for `Request`/`Response`. --- lib/protocol/http/methods.rb | 4 +-- lib/protocol/http/request.rb | 6 ++-- lib/protocol/http/response.rb | 4 +-- test/protocol/http/request.rb | 53 +++++++++++++++++++++++++++++++++- test/protocol/http/response.rb | 45 +++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 8 deletions(-) diff --git a/lib/protocol/http/methods.rb b/lib/protocol/http/methods.rb index edcb41c..e0086ba 100644 --- a/lib/protocol/http/methods.rb +++ b/lib/protocol/http/methods.rb @@ -70,9 +70,9 @@ def self.each end self.each do |name, value| - define_method(name) do |location, headers = nil, body = nil| + define_method(name) do |location, *arguments, **options| self.call( - Request[value, location.to_s, Headers[headers], body] + Request[value, location.to_s, *arguments, **options] ) end end diff --git a/lib/protocol/http/request.rb b/lib/protocol/http/request.rb index 88855e0..544b7f0 100644 --- a/lib/protocol/http/request.rb +++ b/lib/protocol/http/request.rb @@ -81,11 +81,11 @@ def connect? # @parameter path [String] The path, e.g. `"/index.html"`, `"/search?q=hello"`, etc. # @parameter headers [Hash] The headers, e.g. `{"accept" => "text/html"}`, etc. # @parameter body [String | Array(String) | Body::Readable] The body, e.g. `"Hello, World!"`, etc. See {Body::Buffered.wrap} for more information about . - def self.[](method, path, headers = nil, body = nil) + def self.[](method, path, _headers = nil, _body = nil, scheme: nil, authority: nil, headers: _headers, body: _body, protocol: nil) body = Body::Buffered.wrap(body) - headers = ::Protocol::HTTP::Headers[headers] + headers = Headers[headers] - self.new(nil, nil, method, path, nil, headers, body) + self.new(scheme, authority, method, path, nil, headers, body, protocol) end # Whether the request can be replayed without side-effects. diff --git a/lib/protocol/http/response.rb b/lib/protocol/http/response.rb index 17985a0..b335423 100644 --- a/lib/protocol/http/response.rb +++ b/lib/protocol/http/response.rb @@ -130,9 +130,9 @@ def internal_server_error? # @parameter status [Integer] The HTTP status code, e.g. `200`, `404`, etc. # @parameter headers [Hash] The headers, e.g. `{"content-type" => "text/html"}`, etc. # @parameter body [String | Array(String) | Body::Readable] The body, e.g. `"Hello, World!"`, etc. See {Body::Buffered.wrap} for more information about . - def self.[](status, headers = nil, body = nil, protocol = nil) + def self.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil) body = Body::Buffered.wrap(body) - headers = ::Protocol::HTTP::Headers[headers] + headers = Headers[headers] self.new(nil, status, headers, body, protocol) end diff --git a/test/protocol/http/request.rb b/test/protocol/http/request.rb index 3877520..f193f41 100644 --- a/test/protocol/http/request.rb +++ b/test/protocol/http/request.rb @@ -11,6 +11,57 @@ 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 + end + with "simple GET request" do let(:request) {subject.new("http", "localhost", "GET", "/index.html", "HTTP/1.0", headers, body)} @@ -23,7 +74,7 @@ version: be == "HTTP/1.0", headers: be == headers, body: be == body, - protocol: be == nil + protocol: be_nil ) end diff --git a/test/protocol/http/response.rb b/test/protocol/http/response.rb index d53da6d..da7df39 100644 --- a/test/protocol/http/response.rb +++ b/test/protocol/http/response.rb @@ -275,4 +275,49 @@ expect(response).to be(:not_modified?) end end + + with ".[]" do + let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")} + let(:headers) {Protocol::HTTP::Headers[{"accept" => "text/html"}]} + + it "creates a new response" do + response = subject[200, headers] + + expect(response).to have_attributes( + version: be_nil, + status: be == 200, + headers: be == headers, + body: be_nil, + protocol: be_nil + ) + end + + it "creates a new response with keyword arguments" do + response = subject[200, headers: headers, body: body] + + expect(response).to have_attributes( + version: be_nil, + status: be == 200, + headers: be == headers, + body: be == body, + protocol: be_nil + ) + end + + it "converts header hash to headers instance" do + response = subject[200, {"accept" => "text/html"}] + + expect(response).to have_attributes( + headers: be == headers, + ) + end + + it "converts array body to buffered body" do + response = subject[200, headers: headers, body: ["Hello, World!"]] + + expect(response).to have_attributes( + body: be_a(Protocol::HTTP::Body::Buffered) + ) + end + end end