From a410c5b4ac350e65faaeea36fd3fdaa05eee3203 Mon Sep 17 00:00:00 2001 From: Erik Berlin Date: Mon, 16 Mar 2026 20:20:35 -0700 Subject: [PATCH] Support http.rb 6.0 --- down.gemspec | 2 +- lib/down/http.rb | 16 ++++++++++++---- test/http_test.rb | 4 ++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/down.gemspec b/down.gemspec index 2b38e8c..94cb8eb 100644 --- a/down.gemspec +++ b/down.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "mocha", "~> 1.5" spec.add_development_dependency "rake" spec.add_development_dependency "httpx", "~> 1.0", "< 1.4.4" - spec.add_development_dependency "http", "~> 5.0" + spec.add_development_dependency "http", "~> 6.0" spec.add_development_dependency "warning" spec.add_development_dependency "csv" end diff --git a/lib/down/http.rb b/lib/down/http.rb index 0dd7a19..a95056d 100644 --- a/lib/down/http.rb +++ b/lib/down/http.rb @@ -1,8 +1,9 @@ # frozen-string-literal: true -gem "http", ">= 2.1.0", "< 6" +gem "http", ">= 2.1.0", "< 7" require "http" +require "addressable/uri" require "down/backend" @@ -19,7 +20,10 @@ def initialize(**options, &block) .follow(max_hops: 2) .timeout(connect: 30, write: 30, read: 30) - @client = HTTP::Client.new(@client.default_options.merge(options)) if options.any? + if options.any? + client_class = defined?(HTTP::Session) ? HTTP::Session : HTTP::Client + @client = client_class.new(@client.default_options.merge(options)) + end @client = block.call(@client) if block end @@ -94,7 +98,7 @@ def send_request(method, url, **options, &block) client = client.basic_auth(user: uri.user, pass: uri.password) if uri.user || uri.password client = block.call(client) if block - client.request(method, url, options) + client.request(method, url, **options) rescue => exception request_error!(exception) end @@ -123,7 +127,7 @@ def response_error!(response) # Re-raise HTTP.rb exceptions as Down::Error exceptions. def request_error!(exception) case exception - when HTTP::Request::UnsupportedSchemeError, Addressable::URI::InvalidURIError + when HTTP::Request::UnsupportedSchemeError, Addressable::URI::InvalidURIError, *invalid_url_errors raise Down::InvalidUrl, exception.message when HTTP::ConnectionError raise Down::ConnectionError, exception.message @@ -138,6 +142,10 @@ def request_error!(exception) end end + def invalid_url_errors + defined?(HTTP::URI::InvalidError) ? [HTTP::URI::InvalidError] : [] + end + # Defines some additional attributes for the returned Tempfile. module DownloadedFile attr_accessor :url, :headers diff --git a/test/http_test.rb b/test/http_test.rb index 2d5bd00..299777d 100644 --- a/test/http_test.rb +++ b/test/http_test.rb @@ -312,8 +312,8 @@ assert_equal "500 Internal Server Error", error.message assert_instance_of HTTP::Response, error.response - error = assert_raises(Down::ResponseError) { Down::Http.open("#{$httpbin}/status/100") } - assert_equal "100 Continue", error.message + error = assert_raises(Down::ResponseError) { Down::Http.open("#{$httpbin}/status/305") } + assert_equal "305 Use Proxy", error.message assert_instance_of HTTP::Response, error.response end