regression test for bug fixed in 0.11.3

created a test server which removes the content-length. Taken extra
condition into account, that the close might manifest itself while
selecting on the socket; at that point we're out of the consumption
loop, so better not to deal with throwing :called
This commit is contained in:
HoneyryderChuck 2021-02-17 00:55:05 +00:00
parent 4bcaf1e121
commit a2895d456f
3 changed files with 61 additions and 2 deletions

View File

@ -163,13 +163,13 @@ module HTTPX
end
def handle_error(ex)
if ex.is_a?(EOFError) && @request && @request.response &&
if (ex.is_a?(EOFError) || ex.is_a?(TimeoutError)) && @request && @request.response &&
!@request.response.headers.key?("content-length") &&
!@request.response.headers.key?("transfer-encoding")
# if the response does not contain a content-length header, the server closing the
# connnection is the indicator of response consumed.
# https://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.4.4
on_complete
catch(:called) { on_complete }
return
end

View File

@ -2,6 +2,8 @@
require "webrick"
require "logger"
require "zlib"
require "stringio"
class TestServer < WEBrick::HTTPServer
def initialize(options = {})
@ -62,3 +64,40 @@ class Expect100Server < TestServer
mount("/delay", DelayedExpect100App)
end
end
class NoContentLengthServer < TestServer
module NoContentLength
def self.extended(obj)
super
obj.singleton_class.class_eval do
alias_method(:setup_header_without_clength, :setup_header)
alias_method(:setup_header, :setup_header_with_clength)
end
end
def setup_header_with_clength
setup_header_without_clength
header.delete("content-length")
end
end
class NoContentLengthApp < WEBrick::HTTPServlet::AbstractServlet
def do_GET(_req, res) # rubocop:disable Naming/MethodName
zipped = StringIO.new
Zlib::GzipWriter.wrap(zipped) do |gz|
gz.write("helloworld")
end
res.body = zipped.string
res.status = 200
res["Content-Encoding"] = "gzip"
res.extend(NoContentLength)
end
end
def initialize(options = {})
super
mount("/", NoContentLengthApp)
end
end

View File

@ -85,6 +85,26 @@ module Requests
assert compressed_data.bytesize < 8012, "body hasn't been compressed"
end
# regression test
def test_plugin_compression_no_content_length
# run this only for http/1.1 mode, as this is a local test server
return unless origin.start_with?("http://")
server = NoContentLengthServer.new
th = Thread.new { server.start }
begin
http = HTTPX.plugin(:compression)
uri = build_uri("/", server.origin)
response = http.get(uri)
verify_status(response, 200)
body = response.body.to_s
assert body == "helloworld"
ensure
server.shutdown
th.join
end
end
unless RUBY_ENGINE == "jruby"
def test_plugin_compression_brotli
session = HTTPX.plugin(:"compression/brotli")