From 6baf44000942f68d39858dff07550c732efbf88c Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Sat, 13 Aug 2022 19:24:51 +0200 Subject: [PATCH 1/2] raise HTTP::Request::InvalidURIError for wrong url's --- lib/http/client.rb | 2 ++ lib/http/request.rb | 4 ++++ spec/lib/http/request_spec.rb | 2 +- spec/lib/http_spec.rb | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/http/client.rb b/lib/http/client.rb index 48a76bc0..fc113599 100644 --- a/lib/http/client.rb +++ b/lib/http/client.rb @@ -154,6 +154,8 @@ def make_request_uri(uri, opts) uri.path = "/" if uri.path.empty? uri + rescue Addressable::URI::InvalidURIError => e + raise HTTP::Request::InvalidURIError, e.message end # Creates request headers with cookies (if any) merged in diff --git a/lib/http/request.rb b/lib/http/request.rb index 1372cd11..8192dfb5 100644 --- a/lib/http/request.rb +++ b/lib/http/request.rb @@ -23,6 +23,9 @@ class UnsupportedMethodError < RequestError; end # The scheme of given URI was not understood class UnsupportedSchemeError < RequestError; end + # Provided url is missing or wrong + class InvalidURIError < RequestError; end + # Default User-Agent header value USER_AGENT = "http.rb/#{HTTP::VERSION}" @@ -90,6 +93,7 @@ def initialize(opts) @uri = @uri_normalizer.call(opts.fetch(:uri)) @scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme + raise(InvalidURIError, "empty uri provided") if @uri.host.nil? raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb) raise(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme) diff --git a/spec/lib/http/request_spec.rb b/spec/lib/http/request_spec.rb index da4ba074..cde81191 100644 --- a/spec/lib/http/request_spec.rb +++ b/spec/lib/http/request_spec.rb @@ -21,7 +21,7 @@ it "requires URI to have scheme part" do expect { HTTP::Request.new(:verb => :get, :uri => "example.com/") }.to \ - raise_error(HTTP::Request::UnsupportedSchemeError) + raise_error(HTTP::Request::InvalidURIError) end it "provides a #scheme accessor" do diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index f7a1bb21..857dc234 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -391,6 +391,23 @@ def setsockopt(*args) end end + describe "gives a proper error for invalid URLs" do + it "throws InvalidURIError on nil" do + expect { HTTP.get(nil) }.to raise_error(HTTP::Request::InvalidURIError) + expect { HTTP.post(nil, :body => "Hello") }.to raise_error(HTTP::Request::InvalidURIError) + end + + it "throws InvalidURIError on empty" do + expect { HTTP.get("") }.to raise_error(HTTP::Request::InvalidURIError) + expect { HTTP.post("", :body => "Hello") }.to raise_error(HTTP::Request::InvalidURIError) + end + + it "throws InvalidURIError for incorrect url" do + expect { HTTP.get("/") }.to raise_error(HTTP::Request::InvalidURIError) + expect { HTTP.get(":") }.to raise_error(HTTP::Request::InvalidURIError) + end + end + describe ".use" do it "turns on given feature" do client = HTTP.use :auto_deflate From 6a246b0739d70b9fa8817eb79e093eb4afda5594 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Sat, 13 Aug 2022 19:34:46 +0200 Subject: [PATCH 2/2] solve rubocop reported Complexity issue in Request.initializer method --- lib/http/request.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/http/request.rb b/lib/http/request.rb index 8192dfb5..fd98ce1e 100644 --- a/lib/http/request.rb +++ b/lib/http/request.rb @@ -93,9 +93,7 @@ def initialize(opts) @uri = @uri_normalizer.call(opts.fetch(:uri)) @scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme - raise(InvalidURIError, "empty uri provided") if @uri.host.nil? - raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb) - raise(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme) + check_errors @proxy = opts[:proxy] || {} @version = opts[:version] || "1.1" @@ -226,6 +224,12 @@ def inspect # @return [String] def_delegator :@uri, :host + def check_errors + raise(InvalidURIError, "empty uri provided") if @uri.host.nil? + raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb) + raise(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme) + end + # @!attribute [r] port # @return [Fixnum] def port