Skip to content

Commit

Permalink
Support for keyword arguments for Request/Response. (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Aug 30, 2024
1 parent 2048d0d commit 4d1a70b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/protocol/http/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/protocol/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/protocol/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 52 additions & 1 deletion test/protocol/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)}

Expand All @@ -23,7 +74,7 @@
version: be == "HTTP/1.0",
headers: be == headers,
body: be == body,
protocol: be == nil
protocol: be_nil
)
end

Expand Down
45 changes: 45 additions & 0 deletions test/protocol/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4d1a70b

Please sign in to comment.