Skip to content

Commit

Permalink
Merge pull request #4 from janko-m/make-rack-input-behave-like-io-read
Browse files Browse the repository at this point in the history
Make Falcon::Adapters::Input#read behave like IO#read
  • Loading branch information
ioquatix committed Jun 7, 2018
2 parents b7a2e6a + ed798f8 commit f39437a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/falcon/adapters/input.rb
Expand Up @@ -49,22 +49,23 @@ def rewind
end

def read(length = nil, buffer = nil)
buffer ||= Async::IO::BinaryString.new
buffer.clear

if length
fill_buffer(length) if @buffer.bytesize <= length

return @buffer.slice!(0, length)
buffer << @buffer.slice!(0, length)
else
buffer ||= Async::IO::BinaryString.new

buffer << @buffer
@buffer.clear

while chunk = read_next
buffer << chunk
end

return buffer
end

buffer unless length && length > 0 && buffer.empty?
end

def eof?
Expand Down
31 changes: 30 additions & 1 deletion spec/falcon/adapters/input_spec.rb
Expand Up @@ -30,6 +30,7 @@
context '#read' do
it "can read all input" do
expect(subject.read).to be == sample_data.join
expect(subject.read).to be == ""
end

it "can read partial input" do
Expand All @@ -45,8 +46,36 @@
expect(subject.read(15)).to be == sample_data.join[0...15]
expect(subject.read).to be == sample_data.join[15..-1]

expect(subject.read(1)).to be == nil
expect(subject).to be_eof
end

it "can read partial input with buffer" do
buffer = String.new

2.times do
expect(subject.read(3, buffer)).to be == "The"
expect(subject.read(3, buffer)).to be == "qui"
expect(subject.read(3, buffer)).to be == "ckb"
expect(subject.read(3, buffer)).to be == "row"

expect(buffer).to be == "row"

subject.rewind
end

data = subject.read(15, buffer)
expect(data).to be == sample_data.join[0...15]
expect(buffer).to equal(data)

expect(subject.read).to be == sample_data.join[15..-1]

expect(subject.read(1, buffer)).to be == nil
expect(buffer).to be == ""

expect(subject).to be_eof
end

end

context '#each' do
Expand All @@ -73,7 +102,7 @@
end

it "can read partial input" do
expect(subject.read(2)).to be == ""
expect(subject.read(2)).to be == nil
end
end

Expand Down

0 comments on commit f39437a

Please sign in to comment.