Skip to content

Commit 4d1a70b

Browse files
authored
Support for keyword arguments for Request/Response. (#64)
1 parent 2048d0d commit 4d1a70b

File tree

5 files changed

+104
-8
lines changed

5 files changed

+104
-8
lines changed

lib/protocol/http/methods.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def self.each
7070
end
7171

7272
self.each do |name, value|
73-
define_method(name) do |location, headers = nil, body = nil|
73+
define_method(name) do |location, *arguments, **options|
7474
self.call(
75-
Request[value, location.to_s, Headers[headers], body]
75+
Request[value, location.to_s, *arguments, **options]
7676
)
7777
end
7878
end

lib/protocol/http/request.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def connect?
8181
# @parameter path [String] The path, e.g. `"/index.html"`, `"/search?q=hello"`, etc.
8282
# @parameter headers [Hash] The headers, e.g. `{"accept" => "text/html"}`, etc.
8383
# @parameter body [String | Array(String) | Body::Readable] The body, e.g. `"Hello, World!"`, etc. See {Body::Buffered.wrap} for more information about .
84-
def self.[](method, path, headers = nil, body = nil)
84+
def self.[](method, path, _headers = nil, _body = nil, scheme: nil, authority: nil, headers: _headers, body: _body, protocol: nil)
8585
body = Body::Buffered.wrap(body)
86-
headers = ::Protocol::HTTP::Headers[headers]
86+
headers = Headers[headers]
8787

88-
self.new(nil, nil, method, path, nil, headers, body)
88+
self.new(scheme, authority, method, path, nil, headers, body, protocol)
8989
end
9090

9191
# Whether the request can be replayed without side-effects.

lib/protocol/http/response.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ def internal_server_error?
130130
# @parameter status [Integer] The HTTP status code, e.g. `200`, `404`, etc.
131131
# @parameter headers [Hash] The headers, e.g. `{"content-type" => "text/html"}`, etc.
132132
# @parameter body [String | Array(String) | Body::Readable] The body, e.g. `"Hello, World!"`, etc. See {Body::Buffered.wrap} for more information about .
133-
def self.[](status, headers = nil, body = nil, protocol = nil)
133+
def self.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil)
134134
body = Body::Buffered.wrap(body)
135-
headers = ::Protocol::HTTP::Headers[headers]
135+
headers = Headers[headers]
136136

137137
self.new(nil, status, headers, body, protocol)
138138
end

test/protocol/http/request.rb

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,57 @@
1111
let(:headers) {Protocol::HTTP::Headers.new}
1212
let(:body) {nil}
1313

14+
with ".[]" do
15+
let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")}
16+
let(:headers) {Protocol::HTTP::Headers[{"accept" => "text/html"}]}
17+
18+
it "creates a new request" do
19+
request = subject["GET", "/index.html", headers]
20+
21+
expect(request).to have_attributes(
22+
scheme: be_nil,
23+
authority: be_nil,
24+
method: be == "GET",
25+
path: be == "/index.html",
26+
version: be_nil,
27+
headers: be == headers,
28+
body: be_nil,
29+
protocol: be_nil
30+
)
31+
end
32+
33+
it "creates a new request with keyword arguments" do
34+
request = subject["GET", "/index.html", scheme: "http", authority: "localhost", headers: headers, body: body]
35+
36+
expect(request).to have_attributes(
37+
scheme: be == "http",
38+
authority: be == "localhost",
39+
method: be == "GET",
40+
path: be == "/index.html",
41+
version: be_nil,
42+
headers: be == headers,
43+
body: be == body,
44+
protocol: be_nil
45+
)
46+
end
47+
48+
it "converts header hash to headers instance" do
49+
request = subject["GET", "/index.html", {"accept" => "text/html"}]
50+
51+
expect(request).to have_attributes(
52+
headers: be == headers,
53+
)
54+
end
55+
56+
it "converts array body to buffered body" do
57+
request = subject["GET", "/index.html", headers: headers, body: ["Hello, World!"]]
58+
59+
expect(request).to have_attributes(
60+
body: be_a(Protocol::HTTP::Body::Buffered)
61+
)
62+
end
63+
end
64+
1465
with "simple GET request" do
1566
let(:request) {subject.new("http", "localhost", "GET", "/index.html", "HTTP/1.0", headers, body)}
1667

@@ -23,7 +74,7 @@
2374
version: be == "HTTP/1.0",
2475
headers: be == headers,
2576
body: be == body,
26-
protocol: be == nil
77+
protocol: be_nil
2778
)
2879
end
2980

test/protocol/http/response.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,49 @@
275275
expect(response).to be(:not_modified?)
276276
end
277277
end
278+
279+
with ".[]" do
280+
let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")}
281+
let(:headers) {Protocol::HTTP::Headers[{"accept" => "text/html"}]}
282+
283+
it "creates a new response" do
284+
response = subject[200, headers]
285+
286+
expect(response).to have_attributes(
287+
version: be_nil,
288+
status: be == 200,
289+
headers: be == headers,
290+
body: be_nil,
291+
protocol: be_nil
292+
)
293+
end
294+
295+
it "creates a new response with keyword arguments" do
296+
response = subject[200, headers: headers, body: body]
297+
298+
expect(response).to have_attributes(
299+
version: be_nil,
300+
status: be == 200,
301+
headers: be == headers,
302+
body: be == body,
303+
protocol: be_nil
304+
)
305+
end
306+
307+
it "converts header hash to headers instance" do
308+
response = subject[200, {"accept" => "text/html"}]
309+
310+
expect(response).to have_attributes(
311+
headers: be == headers,
312+
)
313+
end
314+
315+
it "converts array body to buffered body" do
316+
response = subject[200, headers: headers, body: ["Hello, World!"]]
317+
318+
expect(response).to have_attributes(
319+
body: be_a(Protocol::HTTP::Body::Buffered)
320+
)
321+
end
322+
end
278323
end

0 commit comments

Comments
 (0)