Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid host validate when is omitted #369

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
JuarezLustosa marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this?

Copy link
Author

@JuarezLustosa JuarezLustosa Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because, when the method "omit" is called, the host is nullified, and then call the method "validate" the error InvalidURIError, "Hostname not supplied:. So I don't like this approach to use @@ in this case, but I don't see any solutions to solve this problem without a huge refactory. Do you have any idea?


##
# Returns a URI object based on the parsed string.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
JuarezLustosa marked this conversation as resolved.
Show resolved Hide resolved
if !port.nil? || !user.nil? || !password.nil?
raise InvalidURIError, "Hostname not supplied: '#{self.to_s}'"
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,19 @@ def to_s
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
Expand Down