Skip to content

Commit 08a8082

Browse files
committed
Add yield interface for read_request and read_response.
1 parent db96687 commit 08a8082

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/protocol/http1/connection.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ def read_request
330330

331331
@count += 1
332332

333-
return headers.delete(HOST), method, path, version, headers, body
333+
if block_given?
334+
yield headers.delete(HOST), method, path, version, headers, body
335+
else
336+
return headers.delete(HOST), method, path, version, headers, body
337+
end
334338
end
335339

336340
def read_response_line
@@ -366,7 +370,11 @@ def read_response(method)
366370
@count += 1
367371
end
368372

369-
return version, status, reason, headers, body
373+
if block_given?
374+
yield version, status, reason, headers, body
375+
else
376+
return version, status, reason, headers, body
377+
end
370378
end
371379

372380
def read_headers

test/protocol/http1/connection.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@
107107
server.read_request
108108
end.to raise_exception(Protocol::HTTP1::InvalidRequest)
109109
end
110+
111+
it "yields to block" do
112+
client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"
113+
client.stream.close
114+
115+
result = server.read_request do |authority, method, target, version, headers, body|
116+
expect(authority).to be == "localhost"
117+
expect(method).to be == "GET"
118+
expect(target).to be == "/"
119+
expect(version).to be == "HTTP/1.1"
120+
expect(headers).to be == {}
121+
expect(body).to be_nil
122+
123+
:yielded
124+
end
125+
126+
expect(result).to be == :yielded
127+
end
110128
end
111129

112130
with "#write_response" do
@@ -207,6 +225,25 @@
207225
expect(headers).to be == {}
208226
expect(body).to be_nil
209227
end
228+
229+
it "should yield to block" do
230+
client.open!
231+
232+
server.stream.write("HTTP/1.1 200 Hello\r\nContent-Length: 0\r\n\r\n")
233+
server.stream.close
234+
235+
result = client.read_response("GET") do |version, status, reason, headers, body|
236+
expect(version).to be == "HTTP/1.1"
237+
expect(status).to be == 200
238+
expect(reason).to be == "Hello"
239+
expect(headers).to be == {}
240+
expect(body).to be_nil
241+
242+
:yielded
243+
end
244+
245+
expect(result).to be == :yielded
246+
end
210247
end
211248

212249
with "#read_response_body" do

0 commit comments

Comments
 (0)