Skip to content

Commit

Permalink
ensure validation_deferred
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Oct 30, 2022
1 parent 8657465 commit af2023a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def self.parse(uri)
uri = uri.to_str
rescue TypeError, NoMethodError
raise TypeError, "Can't convert #{uri.class} into String."
end if not uri.is_a? String
end unless uri.is_a?(String)

# This Regexp supplied as an example in RFC 3986, and it works great.
scan = uri.scan(URIREGEX)
Expand All @@ -144,9 +144,7 @@ def self.parse(uri)
/:([^:@\[\]]*?)$/, EMPTY_STR
)
port = authority[/:([^:@\[\]]*?)$/, 1]
end
if port == EMPTY_STR
port = nil
port = nil if port == EMPTY_STR
end

return new(
Expand Down Expand Up @@ -189,7 +187,7 @@ def self.heuristic_parse(uri, hints={})
uri = uri.to_s
end

if !uri.respond_to?(:to_str)
unless uri.respond_to?(:to_str)
raise TypeError, "Can't convert #{uri.class} into String."
end
# Otherwise, convert to a String
Expand Down Expand Up @@ -335,7 +333,7 @@ def self.join(*uris)
uri.kind_of?(self) ? uri : self.parse(uri.to_str)
end
result = uri_objects.shift.dup
for uri in uri_objects
uri_objects.each do |uri|
result.join!(uri)
end
return result
Expand Down Expand Up @@ -822,21 +820,21 @@ def self.form_unencode(encoded_value)
#
# @return [Addressable::URI] The constructed URI object.
def initialize(options={})
if options.has_key?(:authority)
if options.key?(:authority)
if (options.keys & [:userinfo, :user, :password, :host, :port]).any?
raise ArgumentError,
"Cannot specify both an authority and any of the components " +
"within the authority."
end
end
if options.has_key?(:userinfo)
if options.key?(:userinfo)
if (options.keys & [:user, :password]).any?
raise ArgumentError,
"Cannot specify both a userinfo and either the user or password."
end
end

self.defer_validation do
defer_validation do
# Bunch of crazy logic required because of the composite components
# like userinfo and authority.
self.scheme = options[:scheme] if options[:scheme]
Expand All @@ -851,7 +849,8 @@ def initialize(options={})
self.query_values = options[:query_values] if options[:query_values]
self.fragment = options[:fragment] if options[:fragment]
end
self.to_s

to_s # run path validation
end

##
Expand Down Expand Up @@ -879,7 +878,7 @@ def freeze
#
# @return [String] The scheme component.
def scheme
return defined?(@scheme) ? @scheme : nil
@scheme if defined?(@scheme)
end

##
Expand Down Expand Up @@ -932,7 +931,7 @@ def scheme=(new_scheme)
#
# @return [String] The user component.
def user
return defined?(@user) ? @user : nil
@user if defined?(@user)
end

##
Expand Down Expand Up @@ -989,7 +988,7 @@ def user=(new_user)
#
# @return [String] The password component.
def password
return defined?(@password) ? @password : nil
@password if defined?(@password)
end

##
Expand Down Expand Up @@ -1117,7 +1116,7 @@ def userinfo=(new_userinfo)
#
# @return [String] The host component.
def host
return defined?(@host) ? @host : nil
@host if defined?(@host)
end

##
Expand Down
14 changes: 14 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,20 @@ def to_s
end
end

describe Addressable::URI, "when parsed with empty port" do
subject(:uri) do
Addressable::URI.parse("//example.com:")
end

it "should not infer a port" do
expect(uri.port).to be(nil)
end

it "should have a site value of '//example.com'" do
expect(uri.site).to eq("//example.com")
end
end

describe Addressable::URI, "when parsed from " +
"'http://example.com:%38%30/'" do
before do
Expand Down

0 comments on commit af2023a

Please sign in to comment.