diff --git a/lib/protocol/http/body/reader.rb b/lib/protocol/http/body/reader.rb index 4259ed1..8805ca2 100644 --- a/lib/protocol/http/body/reader.rb +++ b/lib/protocol/http/body/reader.rb @@ -41,7 +41,7 @@ def finish end # Write the body of the response to the given file path. - def save(path, mode = ::File::WRONLY|::File::CREAT, **options) + def save(path, mode = ::File::WRONLY|::File::CREAT|::File::TRUNC, **options) if @body ::File.open(path, mode, **options) do |file| self.each do |chunk| diff --git a/test/protocol/http/body/reader.rb b/test/protocol/http/body/reader.rb index bda9c9a..7546543 100644 --- a/test/protocol/http/body/reader.rb +++ b/test/protocol/http/body/reader.rb @@ -40,7 +40,13 @@ def initialize(body) reader.save(path) expect(File.read(path)).to be == 'thequickbrownfox' end - + + it 'saves by truncating an existing file if it exists' do + File.write(path, 'hello' * 100) + reader.save(path) + expect(File.read(path)).to be == 'thequickbrownfox' + end + it 'mirrors the interface of File.open' do reader.save(path, 'w') expect(File.read(path)).to be == 'thequickbrownfox'