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

Compatibility fix of URI#hostname and URI#hostname= #89

Merged
merged 1 commit into from
May 29, 2013
Merged
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
16 changes: 12 additions & 4 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,20 @@ def host=(new_host)
end

##
# @see Addressable::URI#host
alias_method :hostname, :host
# This method is same as URI::Generic#host except
# brackets for IPv6 (andn future IP) addresses are removed.
def hostname
v = self.host
/\A\[(.*)\]\z/ =~ v ? $1 : v
end

##
# @see Addressable::URI#host=
alias_method :hostname=, :host=
# This method is same as URI::Generic#host= except
# the argument can be bare IPv6 address.
def hostname=(v)
v = "[#{v}]" if /\A\[.*\]\z/ !~ v && /:/ =~ v
self.host = v
end

##
# The authority component for this URI.
Expand Down
22 changes: 22 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,28 @@ def to_s
end
end

describe Addressable::URI, "when parsing IPv6 address" do
subject { Addressable::URI.parse("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/") }
its(:host) { should == '[3ffe:1900:4545:3:200:f8ff:fe21:67cf]' }
its(:hostname) { should == '3ffe:1900:4545:3:200:f8ff:fe21:67cf' }
end

describe Addressable::URI, "when assigning IPv6 address" do
it "should allow to set bare IPv6 address as hostname" do
uri = Addressable::URI.parse("http://[::1]/")
uri.hostname = '3ffe:1900:4545:3:200:f8ff:fe21:67cf'
uri.to_s.should == 'http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'
end

it "should not allow to set bare IPv6 address as host" do
uri = Addressable::URI.parse("http://[::1]/")
pending "not checked"
(lambda do
uri.host = '3ffe:1900:4545:3:200:f8ff:fe21:67cf'
end).should raise_error(Addressable::URI::InvalidURIError)
end
end

describe Addressable::URI, "when parsing IPvFuture addresses" do
it "should not raise an error for " +
"'http://[v9.3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'" do
Expand Down