Skip to content

Commit 0f1fa9d

Browse files
committed
Protocol::HTTP::Body::File should be rewindable?.
1 parent f564127 commit 0f1fa9d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/protocol/http/body/file.rb

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def self.open(path, *arguments, **options)
1818

1919
def initialize(file, range = nil, size: file.size, block_size: BLOCK_SIZE)
2020
@file = file
21+
@range = range
2122

2223
@block_size = block_size
2324

@@ -26,6 +27,7 @@ def initialize(file, range = nil, size: file.size, block_size: BLOCK_SIZE)
2627
@offset = range.min
2728
@length = @remaining = range.size
2829
else
30+
@file.seek(0)
2931
@offset = 0
3032
@length = @remaining = size
3133
end
@@ -51,11 +53,19 @@ def ready?
5153
true
5254
end
5355

56+
def buffered
57+
self.class.new(@file.dup, @range, block_size: @block_size)
58+
end
59+
5460
def rewind
5561
@file.seek(@offset)
5662
@remaining = @length
5763
end
5864

65+
def rewindable?
66+
true
67+
end
68+
5969
def read
6070
if @remaining > 0
6171
amount = [@remaining, @block_size].min

test/protocol/http/body/file.rb

+22-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
let(:path) {File.expand_path("file_spec.txt", __dir__)}
1010
let(:body) {subject.open(path)}
1111

12+
after do
13+
@body&.close
14+
end
15+
1216
# with '#stream?' do
1317
# it "should be streamable" do
1418
# expect(body).to be(:stream?)
@@ -23,11 +27,16 @@
2327

2428
with "#close" do
2529
it "should close file" do
26-
expect(body.file).to receive(:close)
27-
2830
body.close
2931

3032
expect(body).to be(:empty?)
33+
expect(body.file).to be(:closed?)
34+
end
35+
end
36+
37+
with "#rewindable?" do
38+
it "should be rewindable" do
39+
expect(body).to be(:rewindable?)
3140
end
3241
end
3342

@@ -43,6 +52,17 @@
4352
end
4453
end
4554

55+
with "#buffered" do
56+
it "should return a new instance" do
57+
buffered = body.buffered
58+
59+
expect(buffered).to be_a(Protocol::HTTP::Body::File)
60+
expect(buffered).not.to be_equal(body)
61+
ensure
62+
buffered&.close
63+
end
64+
end
65+
4666
with "#inspect" do
4767
it "generates a string representation" do
4868
expect(body.inspect).to be =~ /Protocol::HTTP::Body::File file=(.*?) offset=\d+ remaining=\d+/

0 commit comments

Comments
 (0)