diff --git a/lib/addressable/uri.rb b/lib/addressable/uri.rb index 71a806bf..26b02253 100644 --- a/lib/addressable/uri.rb +++ b/lib/addressable/uri.rb @@ -74,6 +74,9 @@ module CharacterClasses "ldap" => 389, "prospero" => 1525 } + ## + # @@avoid_host_omitted is to avoid host validate when is set to be omitted + @@avoid_host_omitted = true ## # Returns a URI object based on the parsed string. @@ -2292,11 +2295,15 @@ def omit(*components) :scheme, :user, :password, :userinfo, :host, :port, :authority, :path, :query, :fragment ] + unless invalid_components.empty? raise ArgumentError, "Invalid component names: #{invalid_components.inspect}." end duplicated_uri = self.dup + + @avoid_host_omitted = !components.include?(:host) + duplicated_uri.defer_validation do components.each do |component| duplicated_uri.send((component.to_s + "=").to_sym, nil) @@ -2449,10 +2456,8 @@ def validate raise InvalidURIError, "Absolute URI missing hierarchical segment: '#{self.to_s}'" end - if self.host == nil - if self.port != nil || - self.user != nil || - self.password != nil + if host.nil? && @@avoid_host_omitted + if !port.nil? || !user.nil? || !password.nil? raise InvalidURIError, "Hostname not supplied: '#{self.to_s}'" end end diff --git a/spec/addressable/uri_spec.rb b/spec/addressable/uri_spec.rb index 2a938a84..5b51676a 100644 --- a/spec/addressable/uri_spec.rb +++ b/spec/addressable/uri_spec.rb @@ -1033,6 +1033,19 @@ def to_s end end +describe Addressable::URI, "when created has a host but is setup to \ + be omitted" do + + before do + @uri = Addressable::URI.parse("https://example").omit(:scheme, :host, :port, + :user, :password) + end + + it "avoid to be raised" do + expect(@uri).to be_instance_of(Addressable::URI) + end +end + describe Addressable::URI, "when created with both an authority and a user" do it "should raise an error" do expect(lambda do