Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewind IO-alike request body after it was consumed. #468

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/http/request/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def each(&block)
yield @source
elsif @source.respond_to?(:read)
IO.copy_stream(@source, ProcIO.new(block))
@source.rewind if @source.respond_to?(:rewind)
elsif @source.is_a?(Enumerable)
@source.each(&block)
end
Expand Down
20 changes: 18 additions & 2 deletions spec/lib/http/request/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,26 @@
end

context "when body is an Enumerable IO" do
let(:body) { StringIO.new("a" * 16 * 1024 + "b" * 10 * 1024) }
let(:data) { "a" * 16 * 1024 + "b" * 10 * 1024 }
let(:body) { StringIO.new data }

it "yields chunks of content" do
expect(chunks.inject("", :+)).to eq "a" * 16 * 1024 + "b" * 10 * 1024
expect(chunks.inject("", :+)).to eq data
end

it "allows to enumerate multiple times" do
results = []

2.times do
result = ""
subject.each { |chunk| result += chunk }
results << result
end

aggregate_failures do
expect(results.count).to eq 2
expect(results).to all eq data
end
end
end

Expand Down