Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Make RequestBody enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Aug 11, 2013
1 parent 0724c9b commit 9ed107e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
9 changes: 9 additions & 0 deletions lib/reel/request_body.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Reel
# Represents the bodies of Requests
class RequestBody
include Enumerable

def initialize(request)
@request = request
@streaming = nil
Expand All @@ -19,6 +21,13 @@ def readpartial(length = nil)
@request.readpartial(length)
end

# Iterate over the body, allowing it to be enumerable
def each
while chunk = readpartial
yield chunk
end
end

# Eagerly consume the entire body as a string
def to_s
return @contents if @contents
Expand Down
55 changes: 39 additions & 16 deletions spec/reel/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,27 +248,50 @@
end
end

it "streams request bodies" do
with_socket_pair(8) do |client, connection|
example_request = ExampleRequest.new
content = "I'm data you can stream!"
example_request['Content-Length'] = content.length
client << example_request.to_s
context "#readpartial" do
it "streams request bodies" do
with_socket_pair(8) do |client, connection|
example_request = ExampleRequest.new
content = "I'm data you can stream!"
example_request['Content-Length'] = content.length
client << example_request.to_s

request = connection.request
request.should be_a(Reel::Request)
request.should_not be_finished_reading
client << content
rebuilt = []
while chunk = request.body.readpartial(8)
rebuilt << chunk
request = connection.request
request.should be_a(Reel::Request)
request.should_not be_finished_reading
client << content
rebuilt = []
while chunk = request.body.readpartial(8)
rebuilt << chunk
end
request.should be_finished_reading
rebuilt.should == ["I'm data", " you can", " stream!"]
end
end
end

context "#each" do
it "streams request bodies" do
with_socket_pair(8) do |client, connection|
example_request = ExampleRequest.new
content = "I'm data you can stream!"
example_request['Content-Length'] = content.length
client << example_request.to_s

request = connection.request
request.should be_a(Reel::Request)
request.should_not be_finished_reading
client << content

data = ""
request.body.each { |chunk| data << chunk }
request.should be_finished_reading
data.should == "I'm data you can stream!"
end
request.should be_finished_reading
rebuilt.should == ["I'm data", " you can", " stream!"]
end
end

describe "Connection#read behaving like IO#read" do
describe "IO#read duck typing" do
it "raises an exception if length is a negative value" do
with_socket_pair do |client, connection|
example_request = ExampleRequest.new
Expand Down

0 comments on commit 9ed107e

Please sign in to comment.