Skip to content

Commit b1817d0

Browse files
committed
Add tests for inflation failures.
1 parent 63d66ec commit b1817d0

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

lib/protocol/http/body/inflate.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ def read
2323
return if @stream.finished?
2424

2525
# The stream might have been closed while waiting for the chunk to come in.
26-
if chunk = super
26+
while chunk = super
2727
@input_length += chunk.bytesize
2828

2929
# It's possible this triggers the stream to finish.
3030
chunk = @stream.inflate(chunk)
3131

32+
break unless chunk&.empty?
33+
end
34+
35+
if chunk
36+
@output_length += chunk.bytesize
37+
elsif !@stream.closed?
38+
chunk = @stream.finish
3239
@output_length += chunk.bytesize
33-
34-
# chunk = @stream.finish
35-
36-
# @output_length += chunk.bytesize
3740
end
3841

3942
if chunk.empty? and @stream.finished?

test/protocol/http/body/inflate.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2019-2023, by Samuel Williams.
6+
7+
require 'protocol/http/body/buffered'
8+
require 'protocol/http/body/deflate'
9+
require 'protocol/http/body/inflate'
10+
11+
require 'securerandom'
12+
13+
describe Protocol::HTTP::Body::Inflate do
14+
let(:sample) {"The quick brown fox jumps over the lazy dog."}
15+
let(:chunks) {[sample] * 1024}
16+
17+
let(:body) {Protocol::HTTP::Body::Buffered.for(chunks)}
18+
let(:deflate_body) {Protocol::HTTP::Body::Deflate.for(body)}
19+
let(:compressed_chunks) {deflate_body.join.each_char.to_a}
20+
let(:compressed_body_chunks) {compressed_chunks}
21+
let(:compressed_body) {Protocol::HTTP::Body::Buffered.new(compressed_body_chunks)}
22+
let(:decompressed_body) {subject.for(compressed_body)}
23+
24+
it "can decompress a body" do
25+
expect(decompressed_body.join).to be == chunks.join
26+
end
27+
28+
with "incomplete input" do
29+
let(:compressed_body_chunks) {compressed_chunks.first(compressed_chunks.size/2)}
30+
31+
it "raises error when input is incomplete" do
32+
expect{decompressed_body.join}.to raise_exception(Zlib::BufError)
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)