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 29, 2022
1 parent 8657465 commit 4d66c27
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
30 changes: 15 additions & 15 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 # enforce 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 Expand Up @@ -2408,7 +2407,8 @@ def defer_validation
yield
@validation_deferred = false
validate
return nil
ensure
@validation_deferred = false
end

protected
Expand Down
25 changes: 25 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6743,3 +6743,28 @@ def to_str
).to eq(main)
end
end

describe Addressable::URI, "when deferring validation" do
subject(:deferred) { uri.instance_variable_get(:@validation_deferred) }

let(:uri) { Addressable::URI.parse("http://example.com") }

it "defers validation within the block" do
uri.defer_validation do
expect(deferred).to be true
end
end

it "always resets deferal afterward" do
expect {
uri.defer_validation { raise "boom" }
}.to raise_error("boom")

expect(deferred).to be false
end

it "returns nil" do
res = uri.defer_validation {}
expect(res).to be nil
end
end

0 comments on commit 4d66c27

Please sign in to comment.