Permalink
Fetching contributors…
Cannot retrieve contributors at this time
6465 lines (5283 sloc) 178 KB
# coding: utf-8
# Copyright (C) Bob Aman
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "spec_helper"
require "addressable/uri"
require "uri"
require "ipaddr"
if !"".respond_to?("force_encoding")
class String
def force_encoding(encoding)
@encoding = encoding
end
def encoding
@encoding ||= Encoding::ASCII_8BIT
end
end
class Encoding
def initialize(name)
@name = name
end
def to_s
return @name
end
UTF_8 = Encoding.new("UTF-8")
ASCII_8BIT = Encoding.new("US-ASCII")
end
end
module Fake
module URI
class HTTP
def initialize(uri)
@uri = uri
end
def to_s
return @uri.to_s
end
alias :to_str :to_s
end
end
end
describe Addressable::URI, "when created with a non-numeric port number" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:port => "bogus")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with a invalid encoded port number" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:port => "%eb")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with a non-string scheme" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:scheme => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string user" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:user => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string password" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:password => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string userinfo" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:userinfo => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string host" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:host => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string authority" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:authority => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string path" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:path => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string query" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:query => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a non-string fragment" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:fragment => :bogus)
end).to raise_error(TypeError)
end
end
describe Addressable::URI, "when created with a scheme but no hierarchical " +
"segment" do
it "should raise an error" do
expect(lambda do
Addressable::URI.parse("http:")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "quote handling" do
describe 'in host name' do
it "should raise an error for single quote" do
expect(lambda do
Addressable::URI.parse("http://local\"host/")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
end
describe Addressable::URI, "newline normalization" do
it "should not accept newlines in scheme" do
expect(lambda do
Addressable::URI.parse("ht%0atp://localhost/")
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should not unescape newline in path" do
uri = Addressable::URI.parse("http://localhost/%0a").normalize
expect(uri.to_s).to eq("http://localhost/%0A")
end
it "should not unescape newline in hostname" do
uri = Addressable::URI.parse("http://local%0ahost/").normalize
expect(uri.to_s).to eq("http://local%0Ahost/")
end
it "should not unescape newline in username" do
uri = Addressable::URI.parse("http://foo%0abar@localhost/").normalize
expect(uri.to_s).to eq("http://foo%0Abar@localhost/")
end
it "should not unescape newline in username" do
uri = Addressable::URI.parse("http://example:foo%0abar@example/").normalize
expect(uri.to_s).to eq("http://example:foo%0Abar@example/")
end
it "should not accept newline in hostname" do
uri = Addressable::URI.parse("http://localhost/")
expect(lambda do
uri.host = "local\nhost"
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with ambiguous path" do
it "should raise an error" do
expect(lambda do
Addressable::URI.parse("::http")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with an invalid host" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:host => "<invalid>")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with a host consisting of " +
"sub-delims characters" do
it "should not raise an error" do
expect(lambda do
Addressable::URI.new(
:host => Addressable::URI::CharacterClasses::SUB_DELIMS.gsub(/\\/, '')
)
end).not_to raise_error
end
end
describe Addressable::URI, "when created with a host consisting of " +
"unreserved characters" do
it "should not raise an error" do
expect(lambda do
Addressable::URI.new(
:host => Addressable::URI::CharacterClasses::UNRESERVED.gsub(/\\/, '')
)
end).not_to raise_error
end
end
describe Addressable::URI, "when created from nil components" do
before do
@uri = Addressable::URI.new
end
it "should have a nil site value" do
expect(@uri.site).to eq(nil)
end
it "should have an empty path" do
expect(@uri.path).to eq("")
end
it "should be an empty uri" do
expect(@uri.to_s).to eq("")
end
it "should have a nil default port" do
expect(@uri.default_port).to eq(nil)
end
it "should be empty" do
expect(@uri).to be_empty
end
it "should raise an error if the scheme is set to whitespace" do
expect(lambda do
@uri.scheme = "\t \n"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme is set to all digits" do
expect(lambda do
@uri.scheme = "123"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme begins with a digit" do
expect(lambda do
@uri.scheme = "1scheme"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme begins with a plus" do
expect(lambda do
@uri.scheme = "+scheme"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme begins with a dot" do
expect(lambda do
@uri.scheme = ".scheme"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme begins with a dash" do
expect(lambda do
@uri.scheme = "-scheme"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme contains an illegal character" do
expect(lambda do
@uri.scheme = "scheme!"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme contains whitespace" do
expect(lambda do
@uri.scheme = "sch eme"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if the scheme contains a newline" do
expect(lambda do
@uri.scheme = "sch\neme"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if set into an invalid state" do
expect(lambda do
@uri.user = "user"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if set into an invalid state" do
expect(lambda do
@uri.password = "pass"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if set into an invalid state" do
expect(lambda do
@uri.scheme = "http"
@uri.fragment = "fragment"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should raise an error if set into an invalid state" do
expect(lambda do
@uri.fragment = "fragment"
@uri.scheme = "http"
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when initialized from individual components" do
before do
@uri = Addressable::URI.new(
:scheme => "http",
:user => "user",
:password => "password",
:host => "example.com",
:port => 8080,
:path => "/path",
:query => "query=value",
:fragment => "fragment"
)
end
it "returns 'http' for #scheme" do
expect(@uri.scheme).to eq("http")
end
it "returns 'http' for #normalized_scheme" do
expect(@uri.normalized_scheme).to eq("http")
end
it "returns 'user' for #user" do
expect(@uri.user).to eq("user")
end
it "returns 'user' for #normalized_user" do
expect(@uri.normalized_user).to eq("user")
end
it "returns 'password' for #password" do
expect(@uri.password).to eq("password")
end
it "returns 'password' for #normalized_password" do
expect(@uri.normalized_password).to eq("password")
end
it "returns 'user:password' for #userinfo" do
expect(@uri.userinfo).to eq("user:password")
end
it "returns 'user:password' for #normalized_userinfo" do
expect(@uri.normalized_userinfo).to eq("user:password")
end
it "returns 'example.com' for #host" do
expect(@uri.host).to eq("example.com")
end
it "returns 'example.com' for #normalized_host" do
expect(@uri.normalized_host).to eq("example.com")
end
it "returns 'com' for #tld" do
expect(@uri.tld).to eq("com")
end
it "returns 'user:password@example.com:8080' for #authority" do
expect(@uri.authority).to eq("user:password@example.com:8080")
end
it "returns 'user:password@example.com:8080' for #normalized_authority" do
expect(@uri.normalized_authority).to eq("user:password@example.com:8080")
end
it "returns 8080 for #port" do
expect(@uri.port).to eq(8080)
end
it "returns 8080 for #normalized_port" do
expect(@uri.normalized_port).to eq(8080)
end
it "returns 80 for #default_port" do
expect(@uri.default_port).to eq(80)
end
it "returns 'http://user:password@example.com:8080' for #site" do
expect(@uri.site).to eq("http://user:password@example.com:8080")
end
it "returns 'http://user:password@example.com:8080' for #normalized_site" do
expect(@uri.normalized_site).to eq("http://user:password@example.com:8080")
end
it "returns '/path' for #path" do
expect(@uri.path).to eq("/path")
end
it "returns '/path' for #normalized_path" do
expect(@uri.normalized_path).to eq("/path")
end
it "returns 'query=value' for #query" do
expect(@uri.query).to eq("query=value")
end
it "returns 'query=value' for #normalized_query" do
expect(@uri.normalized_query).to eq("query=value")
end
it "returns 'fragment' for #fragment" do
expect(@uri.fragment).to eq("fragment")
end
it "returns 'fragment' for #normalized_fragment" do
expect(@uri.normalized_fragment).to eq("fragment")
end
it "returns #hash" do
expect(@uri.hash).not_to be nil
end
it "returns #to_s" do
expect(@uri.to_s).to eq(
"http://user:password@example.com:8080/path?query=value#fragment"
)
end
it "should not be empty" do
expect(@uri).not_to be_empty
end
it "should not be frozen" do
expect(@uri).not_to be_frozen
end
it "should allow destructive operations" do
expect { @uri.normalize! }.not_to raise_error
end
end
describe Addressable::URI, "when initialized from " +
"frozen individual components" do
before do
@uri = Addressable::URI.new(
:scheme => "http".freeze,
:user => "user".freeze,
:password => "password".freeze,
:host => "example.com".freeze,
:port => "8080".freeze,
:path => "/path".freeze,
:query => "query=value".freeze,
:fragment => "fragment".freeze
)
end
it "returns 'http' for #scheme" do
expect(@uri.scheme).to eq("http")
end
it "returns 'http' for #normalized_scheme" do
expect(@uri.normalized_scheme).to eq("http")
end
it "returns 'user' for #user" do
expect(@uri.user).to eq("user")
end
it "returns 'user' for #normalized_user" do
expect(@uri.normalized_user).to eq("user")
end
it "returns 'password' for #password" do
expect(@uri.password).to eq("password")
end
it "returns 'password' for #normalized_password" do
expect(@uri.normalized_password).to eq("password")
end
it "returns 'user:password' for #userinfo" do
expect(@uri.userinfo).to eq("user:password")
end
it "returns 'user:password' for #normalized_userinfo" do
expect(@uri.normalized_userinfo).to eq("user:password")
end
it "returns 'example.com' for #host" do
expect(@uri.host).to eq("example.com")
end
it "returns 'example.com' for #normalized_host" do
expect(@uri.normalized_host).to eq("example.com")
end
it "returns 'user:password@example.com:8080' for #authority" do
expect(@uri.authority).to eq("user:password@example.com:8080")
end
it "returns 'user:password@example.com:8080' for #normalized_authority" do
expect(@uri.normalized_authority).to eq("user:password@example.com:8080")
end
it "returns 8080 for #port" do
expect(@uri.port).to eq(8080)
end
it "returns 8080 for #normalized_port" do
expect(@uri.normalized_port).to eq(8080)
end
it "returns 80 for #default_port" do
expect(@uri.default_port).to eq(80)
end
it "returns 'http://user:password@example.com:8080' for #site" do
expect(@uri.site).to eq("http://user:password@example.com:8080")
end
it "returns 'http://user:password@example.com:8080' for #normalized_site" do
expect(@uri.normalized_site).to eq("http://user:password@example.com:8080")
end
it "returns '/path' for #path" do
expect(@uri.path).to eq("/path")
end
it "returns '/path' for #normalized_path" do
expect(@uri.normalized_path).to eq("/path")
end
it "returns 'query=value' for #query" do
expect(@uri.query).to eq("query=value")
end
it "returns 'query=value' for #normalized_query" do
expect(@uri.normalized_query).to eq("query=value")
end
it "returns 'fragment' for #fragment" do
expect(@uri.fragment).to eq("fragment")
end
it "returns 'fragment' for #normalized_fragment" do
expect(@uri.normalized_fragment).to eq("fragment")
end
it "returns #hash" do
expect(@uri.hash).not_to be nil
end
it "returns #to_s" do
expect(@uri.to_s).to eq(
"http://user:password@example.com:8080/path?query=value#fragment"
)
end
it "should not be empty" do
expect(@uri).not_to be_empty
end
it "should not be frozen" do
expect(@uri).not_to be_frozen
end
it "should allow destructive operations" do
expect { @uri.normalize! }.not_to raise_error
end
end
describe Addressable::URI, "when parsed from a frozen string" do
before do
@uri = Addressable::URI.parse(
"http://user:password@example.com:8080/path?query=value#fragment".freeze
)
end
it "returns 'http' for #scheme" do
expect(@uri.scheme).to eq("http")
end
it "returns 'http' for #normalized_scheme" do
expect(@uri.normalized_scheme).to eq("http")
end
it "returns 'user' for #user" do
expect(@uri.user).to eq("user")
end
it "returns 'user' for #normalized_user" do
expect(@uri.normalized_user).to eq("user")
end
it "returns 'password' for #password" do
expect(@uri.password).to eq("password")
end
it "returns 'password' for #normalized_password" do
expect(@uri.normalized_password).to eq("password")
end
it "returns 'user:password' for #userinfo" do
expect(@uri.userinfo).to eq("user:password")
end
it "returns 'user:password' for #normalized_userinfo" do
expect(@uri.normalized_userinfo).to eq("user:password")
end
it "returns 'example.com' for #host" do
expect(@uri.host).to eq("example.com")
end
it "returns 'example.com' for #normalized_host" do
expect(@uri.normalized_host).to eq("example.com")
end
it "returns 'user:password@example.com:8080' for #authority" do
expect(@uri.authority).to eq("user:password@example.com:8080")
end
it "returns 'user:password@example.com:8080' for #normalized_authority" do
expect(@uri.normalized_authority).to eq("user:password@example.com:8080")
end
it "returns 8080 for #port" do
expect(@uri.port).to eq(8080)
end
it "returns 8080 for #normalized_port" do
expect(@uri.normalized_port).to eq(8080)
end
it "returns 80 for #default_port" do
expect(@uri.default_port).to eq(80)
end
it "returns 'http://user:password@example.com:8080' for #site" do
expect(@uri.site).to eq("http://user:password@example.com:8080")
end
it "returns 'http://user:password@example.com:8080' for #normalized_site" do
expect(@uri.normalized_site).to eq("http://user:password@example.com:8080")
end
it "returns '/path' for #path" do
expect(@uri.path).to eq("/path")
end
it "returns '/path' for #normalized_path" do
expect(@uri.normalized_path).to eq("/path")
end
it "returns 'query=value' for #query" do
expect(@uri.query).to eq("query=value")
end
it "returns 'query=value' for #normalized_query" do
expect(@uri.normalized_query).to eq("query=value")
end
it "returns 'fragment' for #fragment" do
expect(@uri.fragment).to eq("fragment")
end
it "returns 'fragment' for #normalized_fragment" do
expect(@uri.normalized_fragment).to eq("fragment")
end
it "returns #hash" do
expect(@uri.hash).not_to be nil
end
it "returns #to_s" do
expect(@uri.to_s).to eq(
"http://user:password@example.com:8080/path?query=value#fragment"
)
end
it "should not be empty" do
expect(@uri).not_to be_empty
end
it "should not be frozen" do
expect(@uri).not_to be_frozen
end
it "should allow destructive operations" do
expect { @uri.normalize! }.not_to raise_error
end
end
describe Addressable::URI, "when frozen" do
before do
@uri = Addressable::URI.new.freeze
end
it "returns nil for #scheme" do
expect(@uri.scheme).to eq(nil)
end
it "returns nil for #normalized_scheme" do
expect(@uri.normalized_scheme).to eq(nil)
end
it "returns nil for #user" do
expect(@uri.user).to eq(nil)
end
it "returns nil for #normalized_user" do
expect(@uri.normalized_user).to eq(nil)
end
it "returns nil for #password" do
expect(@uri.password).to eq(nil)
end
it "returns nil for #normalized_password" do
expect(@uri.normalized_password).to eq(nil)
end
it "returns nil for #userinfo" do
expect(@uri.userinfo).to eq(nil)
end
it "returns nil for #normalized_userinfo" do
expect(@uri.normalized_userinfo).to eq(nil)
end
it "returns nil for #host" do
expect(@uri.host).to eq(nil)
end
it "returns nil for #normalized_host" do
expect(@uri.normalized_host).to eq(nil)
end
it "returns nil for #authority" do
expect(@uri.authority).to eq(nil)
end
it "returns nil for #normalized_authority" do
expect(@uri.normalized_authority).to eq(nil)
end
it "returns nil for #port" do
expect(@uri.port).to eq(nil)
end
it "returns nil for #normalized_port" do
expect(@uri.normalized_port).to eq(nil)
end
it "returns nil for #default_port" do
expect(@uri.default_port).to eq(nil)
end
it "returns nil for #site" do
expect(@uri.site).to eq(nil)
end
it "returns nil for #normalized_site" do
expect(@uri.normalized_site).to eq(nil)
end
it "returns '' for #path" do
expect(@uri.path).to eq('')
end
it "returns '' for #normalized_path" do
expect(@uri.normalized_path).to eq('')
end
it "returns nil for #query" do
expect(@uri.query).to eq(nil)
end
it "returns nil for #normalized_query" do
expect(@uri.normalized_query).to eq(nil)
end
it "returns nil for #fragment" do
expect(@uri.fragment).to eq(nil)
end
it "returns nil for #normalized_fragment" do
expect(@uri.normalized_fragment).to eq(nil)
end
it "returns #hash" do
expect(@uri.hash).not_to be nil
end
it "returns #to_s" do
expect(@uri.to_s).to eq('')
end
it "should be empty" do
expect(@uri).to be_empty
end
it "should be frozen" do
expect(@uri).to be_frozen
end
it "should not be frozen after duping" do
expect(@uri.dup).not_to be_frozen
end
it "should not allow destructive operations" do
expect { @uri.normalize! }.to raise_error { |error|
expect(error.message).to match(/can't modify frozen/)
expect(error).to satisfy { |e| RuntimeError === e || TypeError === e }
}
end
end
describe Addressable::URI, "when frozen" do
before do
@uri = Addressable::URI.parse(
"HTTP://example.com.:%38%30/%70a%74%68?a=%31#1%323"
).freeze
end
it "returns 'HTTP' for #scheme" do
expect(@uri.scheme).to eq("HTTP")
end
it "returns 'http' for #normalized_scheme" do
expect(@uri.normalized_scheme).to eq("http")
expect(@uri.normalize.scheme).to eq("http")
end
it "returns nil for #user" do
expect(@uri.user).to eq(nil)
end
it "returns nil for #normalized_user" do
expect(@uri.normalized_user).to eq(nil)
end
it "returns nil for #password" do
expect(@uri.password).to eq(nil)
end
it "returns nil for #normalized_password" do
expect(@uri.normalized_password).to eq(nil)
end
it "returns nil for #userinfo" do
expect(@uri.userinfo).to eq(nil)
end
it "returns nil for #normalized_userinfo" do
expect(@uri.normalized_userinfo).to eq(nil)
end
it "returns 'example.com.' for #host" do
expect(@uri.host).to eq("example.com.")
end
it "returns nil for #normalized_host" do
expect(@uri.normalized_host).to eq("example.com")
expect(@uri.normalize.host).to eq("example.com")
end
it "returns 'example.com.:80' for #authority" do
expect(@uri.authority).to eq("example.com.:80")
end
it "returns 'example.com:80' for #normalized_authority" do
expect(@uri.normalized_authority).to eq("example.com")
expect(@uri.normalize.authority).to eq("example.com")
end
it "returns 80 for #port" do
expect(@uri.port).to eq(80)
end
it "returns nil for #normalized_port" do
expect(@uri.normalized_port).to eq(nil)
expect(@uri.normalize.port).to eq(nil)
end
it "returns 80 for #default_port" do
expect(@uri.default_port).to eq(80)
end
it "returns 'HTTP://example.com.:80' for #site" do
expect(@uri.site).to eq("HTTP://example.com.:80")
end
it "returns 'http://example.com' for #normalized_site" do
expect(@uri.normalized_site).to eq("http://example.com")
expect(@uri.normalize.site).to eq("http://example.com")
end
it "returns '/%70a%74%68' for #path" do
expect(@uri.path).to eq("/%70a%74%68")
end
it "returns '/path' for #normalized_path" do
expect(@uri.normalized_path).to eq("/path")
expect(@uri.normalize.path).to eq("/path")
end
it "returns 'a=%31' for #query" do
expect(@uri.query).to eq("a=%31")
end
it "returns 'a=1' for #normalized_query" do
expect(@uri.normalized_query).to eq("a=1")
expect(@uri.normalize.query).to eq("a=1")
end
it "returns '1%323' for #fragment" do
expect(@uri.fragment).to eq("1%323")
end
it "returns '123' for #normalized_fragment" do
expect(@uri.normalized_fragment).to eq("123")
expect(@uri.normalize.fragment).to eq("123")
end
it "returns #hash" do
expect(@uri.hash).not_to be nil
end
it "returns #to_s" do
expect(@uri.to_s).to eq('HTTP://example.com.:80/%70a%74%68?a=%31#1%323')
expect(@uri.normalize.to_s).to eq('http://example.com/path?a=1#123')
end
it "should not be empty" do
expect(@uri).not_to be_empty
end
it "should be frozen" do
expect(@uri).to be_frozen
end
it "should not be frozen after duping" do
expect(@uri.dup).not_to be_frozen
end
it "should not allow destructive operations" do
expect { @uri.normalize! }.to raise_error { |error|
expect(error.message).to match(/can't modify frozen/)
expect(error).to satisfy { |e| RuntimeError === e || TypeError === e }
}
end
end
describe Addressable::URI, "when created from string components" do
before do
@uri = Addressable::URI.new(
:scheme => "http", :host => "example.com"
)
end
it "should have a site value of 'http://example.com'" do
expect(@uri.site).to eq("http://example.com")
end
it "should be equal to the equivalent parsed URI" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com"))
end
it "should raise an error if invalid components omitted" do
expect(lambda do
@uri.omit(:bogus)
end).to raise_error(ArgumentError)
expect(lambda do
@uri.omit(:scheme, :bogus, :path)
end).to raise_error(ArgumentError)
end
end
describe Addressable::URI, "when created with a nil host but " +
"non-nil authority components" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:user => "user", :password => "pass", :port => 80)
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with both an authority and a user" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(
:user => "user", :authority => "user@example.com:80"
)
end).to raise_error(ArgumentError)
end
end
describe Addressable::URI, "when created with an authority and no port" do
before do
@uri = Addressable::URI.new(:authority => "user@example.com")
end
it "should not infer a port" do
expect(@uri.port).to eq(nil)
expect(@uri.default_port).to eq(nil)
expect(@uri.inferred_port).to eq(nil)
end
it "should have a site value of '//user@example.com'" do
expect(@uri.site).to eq("//user@example.com")
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
describe Addressable::URI, "when created with a host with trailing dots" do
before do
@uri = Addressable::URI.new(:authority => "example...")
end
it "should have a stable normalized form" do
expect(@uri.normalize.normalize.normalize.host).to eq(
@uri.normalize.host
)
end
end
describe Addressable::URI, "when created with a host with a backslash" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:authority => "example\\example")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with a host with a slash" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:authority => "example/example")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with a host with a space" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:authority => "example example")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when created with both a userinfo and a user" do
it "should raise an error" do
expect(lambda do
Addressable::URI.new(:user => "user", :userinfo => "user:pass")
end).to raise_error(ArgumentError)
end
end
describe Addressable::URI, "when created with a path that hasn't been " +
"prefixed with a '/' but a host specified" do
before do
@uri = Addressable::URI.new(
:scheme => "http", :host => "example.com", :path => "path"
)
end
it "should prefix a '/' to the path" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com/path"))
end
it "should have a site value of 'http://example.com'" do
expect(@uri.site).to eq("http://example.com")
end
it "should have an origin of 'http://example.com" do
expect(@uri.origin).to eq('http://example.com')
end
end
describe Addressable::URI, "when created with a path that hasn't been " +
"prefixed with a '/' but no host specified" do
before do
@uri = Addressable::URI.new(
:scheme => "http", :path => "path"
)
end
it "should not prefix a '/' to the path" do
expect(@uri).to eq(Addressable::URI.parse("http:path"))
end
it "should have a site value of 'http:'" do
expect(@uri.site).to eq("http:")
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
describe Addressable::URI, "when parsed from an Addressable::URI object" do
it "should not have unexpected side-effects" do
original_uri = Addressable::URI.parse("http://example.com/")
new_uri = Addressable::URI.parse(original_uri)
new_uri.host = 'www.example.com'
expect(new_uri.host).to eq('www.example.com')
expect(new_uri.to_s).to eq('http://www.example.com/')
expect(original_uri.host).to eq('example.com')
expect(original_uri.to_s).to eq('http://example.com/')
end
it "should not have unexpected side-effects" do
original_uri = Addressable::URI.parse("http://example.com/")
new_uri = Addressable::URI.heuristic_parse(original_uri)
new_uri.host = 'www.example.com'
expect(new_uri.host).to eq('www.example.com')
expect(new_uri.to_s).to eq('http://www.example.com/')
expect(original_uri.host).to eq('example.com')
expect(original_uri.to_s).to eq('http://example.com/')
end
it "should not have unexpected side-effects" do
original_uri = Addressable::URI.parse("http://example.com/")
new_uri = Addressable::URI.parse(original_uri)
new_uri.origin = 'https://www.example.com:8080'
expect(new_uri.host).to eq('www.example.com')
expect(new_uri.to_s).to eq('https://www.example.com:8080/')
expect(original_uri.host).to eq('example.com')
expect(original_uri.to_s).to eq('http://example.com/')
end
it "should not have unexpected side-effects" do
original_uri = Addressable::URI.parse("http://example.com/")
new_uri = Addressable::URI.heuristic_parse(original_uri)
new_uri.origin = 'https://www.example.com:8080'
expect(new_uri.host).to eq('www.example.com')
expect(new_uri.to_s).to eq('https://www.example.com:8080/')
expect(original_uri.host).to eq('example.com')
expect(original_uri.to_s).to eq('http://example.com/')
end
end
describe Addressable::URI, "when parsed from something that looks " +
"like a URI object" do
it "should parse without error" do
uri = Addressable::URI.parse(Fake::URI::HTTP.new("http://example.com/"))
expect(lambda do
Addressable::URI.parse(uri)
end).not_to raise_error
end
end
describe Addressable::URI, "when parsed from a standard library URI object" do
it "should parse without error" do
uri = Addressable::URI.parse(URI.parse("http://example.com/"))
expect(lambda do
Addressable::URI.parse(uri)
end).not_to raise_error
end
end
describe Addressable::URI, "when parsed from ''" do
before do
@uri = Addressable::URI.parse("")
end
it "should have no scheme" do
expect(@uri.scheme).to eq(nil)
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should have a path of ''" do
expect(@uri.path).to eq("")
end
it "should have a request URI of '/'" do
expect(@uri.request_uri).to eq("/")
end
it "should be considered relative" do
expect(@uri).to be_relative
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'ftp://ftp.is.co.za/rfc/rfc1808.txt'" do
before do
@uri = Addressable::URI.parse("ftp://ftp.is.co.za/rfc/rfc1808.txt")
end
it "should use the 'ftp' scheme" do
expect(@uri.scheme).to eq("ftp")
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have a host of 'ftp.is.co.za'" do
expect(@uri.host).to eq("ftp.is.co.za")
end
it "should have inferred_port of 21" do
expect(@uri.inferred_port).to eq(21)
end
it "should have a path of '/rfc/rfc1808.txt'" do
expect(@uri.path).to eq("/rfc/rfc1808.txt")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have an origin of 'ftp://ftp.is.co.za'" do
expect(@uri.origin).to eq('ftp://ftp.is.co.za')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'http://www.ietf.org/rfc/rfc2396.txt'" do
before do
@uri = Addressable::URI.parse("http://www.ietf.org/rfc/rfc2396.txt")
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have a host of 'www.ietf.org'" do
expect(@uri.host).to eq("www.ietf.org")
end
it "should have inferred_port of 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a path of '/rfc/rfc2396.txt'" do
expect(@uri.path).to eq("/rfc/rfc2396.txt")
end
it "should have a request URI of '/rfc/rfc2396.txt'" do
expect(@uri.request_uri).to eq("/rfc/rfc2396.txt")
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should correctly omit components" do
expect(@uri.omit(:scheme).to_s).to eq("//www.ietf.org/rfc/rfc2396.txt")
expect(@uri.omit(:path).to_s).to eq("http://www.ietf.org")
end
it "should correctly omit components destructively" do
@uri.omit!(:scheme)
expect(@uri.to_s).to eq("//www.ietf.org/rfc/rfc2396.txt")
end
it "should have an origin of 'http://www.ietf.org'" do
expect(@uri.origin).to eq('http://www.ietf.org')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'ldap://[2001:db8::7]/c=GB?objectClass?one'" do
before do
@uri = Addressable::URI.parse("ldap://[2001:db8::7]/c=GB?objectClass?one")
end
it "should use the 'ldap' scheme" do
expect(@uri.scheme).to eq("ldap")
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have a host of '[2001:db8::7]'" do
expect(@uri.host).to eq("[2001:db8::7]")
end
it "should have inferred_port of 389" do
expect(@uri.inferred_port).to eq(389)
end
it "should have a path of '/c=GB'" do
expect(@uri.path).to eq("/c=GB")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should not allow request URI assignment" do
expect(lambda do
@uri.request_uri = "/"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should have a query of 'objectClass?one'" do
expect(@uri.query).to eq("objectClass?one")
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should correctly omit components" do
expect(@uri.omit(:scheme, :authority).to_s).to eq("/c=GB?objectClass?one")
expect(@uri.omit(:path).to_s).to eq("ldap://[2001:db8::7]?objectClass?one")
end
it "should correctly omit components destructively" do
@uri.omit!(:scheme, :authority)
expect(@uri.to_s).to eq("/c=GB?objectClass?one")
end
it "should raise an error if omission would create an invalid URI" do
expect(lambda do
@uri.omit(:authority, :path)
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should have an origin of 'ldap://[2001:db8::7]'" do
expect(@uri.origin).to eq('ldap://[2001:db8::7]')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'mailto:John.Doe@example.com'" do
before do
@uri = Addressable::URI.parse("mailto:John.Doe@example.com")
end
it "should use the 'mailto' scheme" do
expect(@uri.scheme).to eq("mailto")
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should not have an inferred_port" do
expect(@uri.inferred_port).to eq(nil)
end
it "should have a path of 'John.Doe@example.com'" do
expect(@uri.path).to eq("John.Doe@example.com")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
# Section 2 of RFC 6068
describe Addressable::URI, "when parsed from " +
"'mailto:?to=addr1@an.example,addr2@an.example'" do
before do
@uri = Addressable::URI.parse(
"mailto:?to=addr1@an.example,addr2@an.example"
)
end
it "should use the 'mailto' scheme" do
expect(@uri.scheme).to eq("mailto")
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should not have an inferred_port" do
expect(@uri.inferred_port).to eq(nil)
end
it "should have a path of ''" do
expect(@uri.path).to eq("")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should have the To: field value parameterized" do
expect(@uri.query_values(Hash)["to"]).to eq(
"addr1@an.example,addr2@an.example"
)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'news:comp.infosystems.www.servers.unix'" do
before do
@uri = Addressable::URI.parse("news:comp.infosystems.www.servers.unix")
end
it "should use the 'news' scheme" do
expect(@uri.scheme).to eq("news")
end
it "should not have an inferred_port" do
expect(@uri.inferred_port).to eq(nil)
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should have a path of 'comp.infosystems.www.servers.unix'" do
expect(@uri.path).to eq("comp.infosystems.www.servers.unix")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'tel:+1-816-555-1212'" do
before do
@uri = Addressable::URI.parse("tel:+1-816-555-1212")
end
it "should use the 'tel' scheme" do
expect(@uri.scheme).to eq("tel")
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should not have an inferred_port" do
expect(@uri.inferred_port).to eq(nil)
end
it "should have a path of '+1-816-555-1212'" do
expect(@uri.path).to eq("+1-816-555-1212")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'telnet://192.0.2.16:80/'" do
before do
@uri = Addressable::URI.parse("telnet://192.0.2.16:80/")
end
it "should use the 'telnet' scheme" do
expect(@uri.scheme).to eq("telnet")
end
it "should have a host of '192.0.2.16'" do
expect(@uri.host).to eq("192.0.2.16")
end
it "should have a port of 80" do
expect(@uri.port).to eq(80)
end
it "should have a inferred_port of 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a default_port of 23" do
expect(@uri.default_port).to eq(23)
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have a path of '/'" do
expect(@uri.path).to eq("/")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have an origin of 'telnet://192.0.2.16:80'" do
expect(@uri.origin).to eq('telnet://192.0.2.16:80')
end
end
# Section 1.1.2 of RFC 3986
describe Addressable::URI, "when parsed from " +
"'urn:oasis:names:specification:docbook:dtd:xml:4.1.2'" do
before do
@uri = Addressable::URI.parse(
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2")
end
it "should use the 'urn' scheme" do
expect(@uri.scheme).to eq("urn")
end
it "should not have an inferred_port" do
expect(@uri.inferred_port).to eq(nil)
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should have a path of " +
"'oasis:names:specification:docbook:dtd:xml:4.1.2'" do
expect(@uri.path).to eq("oasis:names:specification:docbook:dtd:xml:4.1.2")
end
it "should not have a request URI" do
expect(@uri.request_uri).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have a 'null' origin" do
expect(@uri.origin).to eq('null')
end
end
describe Addressable::URI, "when heuristically parsed from " +
"'192.0.2.16:8000/path'" do
before do
@uri = Addressable::URI.heuristic_parse("192.0.2.16:8000/path")
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should have a host of '192.0.2.16'" do
expect(@uri.host).to eq("192.0.2.16")
end
it "should have a port of '8000'" do
expect(@uri.port).to eq(8000)
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have a path of '/path'" do
expect(@uri.path).to eq("/path")
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should have an origin of 'http://192.0.2.16:8000'" do
expect(@uri.origin).to eq('http://192.0.2.16:8000')
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com'" do
before do
@uri = Addressable::URI.parse("http://example.com")
end
it "when inspected, should have the correct URI" do
expect(@uri.inspect).to include("http://example.com")
end
it "when inspected, should have the correct class name" do
expect(@uri.inspect).to include("Addressable::URI")
end
it "when inspected, should have the correct object id" do
expect(@uri.inspect).to include("%#0x" % @uri.object_id)
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have an authority segment of 'example.com'" do
expect(@uri.authority).to eq("example.com")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should be considered ip-based" do
expect(@uri).to be_ip_based
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should not have a specified port" do
expect(@uri.port).to eq(nil)
end
it "should have an empty path" do
expect(@uri.path).to eq("")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
expect(@uri.query_values).to eq(nil)
end
it "should have a request URI of '/'" do
expect(@uri.request_uri).to eq("/")
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should be considered absolute" do
expect(@uri).to be_absolute
end
it "should not be considered relative" do
expect(@uri).not_to be_relative
end
it "should not be exactly equal to 42" do
expect(@uri.eql?(42)).to eq(false)
end
it "should not be equal to 42" do
expect(@uri == 42).to eq(false)
end
it "should not be roughly equal to 42" do
expect(@uri === 42).to eq(false)
end
it "should be exactly equal to http://example.com" do
expect(@uri.eql?(Addressable::URI.parse("http://example.com"))).to eq(true)
end
it "should be roughly equal to http://example.com/" do
expect(@uri === Addressable::URI.parse("http://example.com/")).to eq(true)
end
it "should be roughly equal to the string 'http://example.com/'" do
expect(@uri === "http://example.com/").to eq(true)
end
it "should not be roughly equal to the string " +
"'http://example.com:bogus/'" do
expect(lambda do
expect(@uri === "http://example.com:bogus/").to eq(false)
end).not_to raise_error
end
it "should result in itself when joined with itself" do
expect(@uri.join(@uri).to_s).to eq("http://example.com")
expect(@uri.join!(@uri).to_s).to eq("http://example.com")
end
it "should be equivalent to http://EXAMPLE.com" do
expect(@uri).to eq(Addressable::URI.parse("http://EXAMPLE.com"))
end
it "should be equivalent to http://EXAMPLE.com:80/" do
expect(@uri).to eq(Addressable::URI.parse("http://EXAMPLE.com:80/"))
end
it "should have the same hash as http://example.com" do
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com").hash)
end
it "should have the same hash as http://EXAMPLE.com after assignment" do
@uri.origin = "http://EXAMPLE.com"
expect(@uri.hash).to eq(Addressable::URI.parse("http://EXAMPLE.com").hash)
end
it "should have a different hash from http://EXAMPLE.com" do
expect(@uri.hash).not_to eq(Addressable::URI.parse("http://EXAMPLE.com").hash)
end
it "should not allow origin assignment without scheme" do
expect(lambda do
@uri.origin = "example.com"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should not allow origin assignment without host" do
expect(lambda do
@uri.origin = "http://"
end).to raise_error(Addressable::URI::InvalidURIError)
end
it "should not allow origin assignment with bogus type" do
expect(lambda do
@uri.origin = :bogus
end).to raise_error(TypeError)
end
# Section 6.2.3 of RFC 3986
it "should be equivalent to http://example.com/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com/"))
end
# Section 6.2.3 of RFC 3986
it "should be equivalent to http://example.com:/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com:/"))
end
# Section 6.2.3 of RFC 3986
it "should be equivalent to http://example.com:80/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com:80/"))
end
# Section 6.2.2.1 of RFC 3986
it "should be equivalent to http://EXAMPLE.COM/" do
expect(@uri).to eq(Addressable::URI.parse("http://EXAMPLE.COM/"))
end
it "should have a route of '/path/' to 'http://example.com/path/'" do
expect(@uri.route_to("http://example.com/path/")).to eq(
Addressable::URI.parse("/path/")
)
end
it "should have a route of '..' from 'http://example.com/path/'" do
expect(@uri.route_from("http://example.com/path/")).to eq(
Addressable::URI.parse("..")
)
end
it "should have a route of '#' to 'http://example.com/'" do
expect(@uri.route_to("http://example.com/")).to eq(
Addressable::URI.parse("#")
)
end
it "should have a route of 'http://elsewhere.com/' to " +
"'http://elsewhere.com/'" do
expect(@uri.route_to("http://elsewhere.com/")).to eq(
Addressable::URI.parse("http://elsewhere.com/")
)
end
it "when joined with 'relative/path' should be " +
"'http://example.com/relative/path'" do
expect(@uri.join('relative/path')).to eq(
Addressable::URI.parse("http://example.com/relative/path")
)
end
it "when joined with a bogus object a TypeError should be raised" do
expect(lambda do
@uri.join(42)
end).to raise_error(TypeError)
end
it "should have the correct username after assignment" do
@uri.user = "newuser"
expect(@uri.user).to eq("newuser")
expect(@uri.password).to eq(nil)
expect(@uri.to_s).to eq("http://newuser@example.com")
end
it "should have the correct username after assignment" do
@uri.user = "user@123!"
expect(@uri.user).to eq("user@123!")
expect(@uri.normalized_user).to eq("user%40123%21")
expect(@uri.password).to eq(nil)
expect(@uri.normalize.to_s).to eq("http://user%40123%21@example.com/")
end
it "should have the correct password after assignment" do
@uri.password = "newpass"
expect(@uri.password).to eq("newpass")
expect(@uri.user).to eq("")
expect(@uri.to_s).to eq("http://:newpass@example.com")
end
it "should have the correct password after assignment" do
@uri.password = "#secret@123!"
expect(@uri.password).to eq("#secret@123!")
expect(@uri.normalized_password).to eq("%23secret%40123%21")
expect(@uri.user).to eq("")
expect(@uri.normalize.to_s).to eq("http://:%23secret%40123%21@example.com/")
expect(@uri.omit(:password).to_s).to eq("http://example.com")
end
it "should have the correct user/pass after repeated assignment" do
@uri.user = nil
expect(@uri.user).to eq(nil)
@uri.password = "newpass"
expect(@uri.password).to eq("newpass")
# Username cannot be nil if the password is set
expect(@uri.user).to eq("")
expect(@uri.to_s).to eq("http://:newpass@example.com")
@uri.user = "newuser"
expect(@uri.user).to eq("newuser")
@uri.password = nil
expect(@uri.password).to eq(nil)
expect(@uri.to_s).to eq("http://newuser@example.com")
@uri.user = "newuser"
expect(@uri.user).to eq("newuser")
@uri.password = ""
expect(@uri.password).to eq("")
expect(@uri.to_s).to eq("http://newuser:@example.com")
@uri.password = "newpass"
expect(@uri.password).to eq("newpass")
@uri.user = nil
# Username cannot be nil if the password is set
expect(@uri.user).to eq("")
expect(@uri.to_s).to eq("http://:newpass@example.com")
end
it "should have the correct user/pass after userinfo assignment" do
@uri.user = "newuser"
expect(@uri.user).to eq("newuser")
@uri.password = "newpass"
expect(@uri.password).to eq("newpass")
@uri.userinfo = nil
expect(@uri.userinfo).to eq(nil)
expect(@uri.user).to eq(nil)
expect(@uri.password).to eq(nil)
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "example.com",
:port => nil,
:path => "",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
end
# Section 5.1.2 of RFC 2616
describe Addressable::URI, "when parsed from " +
"'http://www.w3.org/pub/WWW/TheProject.html'" do
before do
@uri = Addressable::URI.parse("http://www.w3.org/pub/WWW/TheProject.html")
end
it "should have the correct request URI" do
expect(@uri.request_uri).to eq("/pub/WWW/TheProject.html")
end
it "should have the correct request URI after assignment" do
@uri.request_uri = "/some/where/else.html?query?string"
expect(@uri.request_uri).to eq("/some/where/else.html?query?string")
expect(@uri.path).to eq("/some/where/else.html")
expect(@uri.query).to eq("query?string")
end
it "should have the correct request URI after assignment" do
@uri.request_uri = "?x=y"
expect(@uri.request_uri).to eq("/?x=y")
expect(@uri.path).to eq("/")
expect(@uri.query).to eq("x=y")
end
it "should raise an error if the site value is set to something bogus" do
expect(lambda do
@uri.site = 42
end).to raise_error(TypeError)
end
it "should raise an error if the request URI is set to something bogus" do
expect(lambda do
@uri.request_uri = 42
end).to raise_error(TypeError)
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "www.w3.org",
:port => nil,
:path => "/pub/WWW/TheProject.html",
:query => nil,
:fragment => nil
})
end
it "should have an origin of 'http://www.w3.org'" do
expect(@uri.origin).to eq('http://www.w3.org')
end
end
describe Addressable::URI, "when parsing IPv6 addresses" do
it "should not raise an error for " +
"'http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'" do
Addressable::URI.parse("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/")
end
it "should not raise an error for " +
"'http://[fe80:0:0:0:200:f8ff:fe21:67cf]/'" do
Addressable::URI.parse("http://[fe80:0:0:0:200:f8ff:fe21:67cf]/")
end
it "should not raise an error for " +
"'http://[fe80::200:f8ff:fe21:67cf]/'" do
Addressable::URI.parse("http://[fe80::200:f8ff:fe21:67cf]/")
end
it "should not raise an error for " +
"'http://[::1]/'" do
Addressable::URI.parse("http://[::1]/")
end
it "should not raise an error for " +
"'http://[fe80::1]/'" do
Addressable::URI.parse("http://[fe80::1]/")
end
it "should raise an error for " +
"'http://[<invalid>]/'" do
expect(lambda do
Addressable::URI.parse("http://[<invalid>]/")
end).to raise_error(Addressable::URI::InvalidURIError)
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'
expect(uri.to_s).to eq('http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/')
end
it "should allow to set bare IPv6 address as hostname with IPAddr object" do
uri = Addressable::URI.parse("http://[::1]/")
uri.hostname = IPAddr.new('3ffe:1900:4545:3:200:f8ff:fe21:67cf')
expect(uri.to_s).to eq('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]/")
skip "not checked"
expect(lambda do
uri.host = '3ffe:1900:4545:3:200:f8ff:fe21:67cf'
end).to 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
Addressable::URI.parse("http://[v9.3ffe:1900:4545:3:200:f8ff:fe21:67cf]/")
end
it "should not raise an error for " +
"'http://[vff.fe80:0:0:0:200:f8ff:fe21:67cf]/'" do
Addressable::URI.parse("http://[vff.fe80:0:0:0:200:f8ff:fe21:67cf]/")
end
it "should not raise an error for " +
"'http://[v12.fe80::200:f8ff:fe21:67cf]/'" do
Addressable::URI.parse("http://[v12.fe80::200:f8ff:fe21:67cf]/")
end
it "should not raise an error for " +
"'http://[va0.::1]/'" do
Addressable::URI.parse("http://[va0.::1]/")
end
it "should not raise an error for " +
"'http://[v255.fe80::1]/'" do
Addressable::URI.parse("http://[v255.fe80::1]/")
end
it "should raise an error for " +
"'http://[v0.<invalid>]/'" do
expect(lambda do
Addressable::URI.parse("http://[v0.<invalid>]/")
end).to raise_error(Addressable::URI::InvalidURIError)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/'" do
before do
@uri = Addressable::URI.parse("http://example.com/")
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to http://example.com" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com"))
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to HTTP://example.com/" do
expect(@uri).to eq(Addressable::URI.parse("HTTP://example.com/"))
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to http://example.com:/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com:/"))
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to http://example.com:80/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com:80/"))
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to http://Example.com/" do
expect(@uri).to eq(Addressable::URI.parse("http://Example.com/"))
end
it "should have the correct username after assignment" do
@uri.user = nil
expect(@uri.user).to eq(nil)
expect(@uri.password).to eq(nil)
expect(@uri.to_s).to eq("http://example.com/")
end
it "should have the correct password after assignment" do
@uri.password = nil
expect(@uri.password).to eq(nil)
expect(@uri.user).to eq(nil)
expect(@uri.to_s).to eq("http://example.com/")
end
it "should have a request URI of '/'" do
expect(@uri.request_uri).to eq("/")
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "example.com",
:port => nil,
:path => "/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have the same hash as its duplicate" do
expect(@uri.hash).to eq(@uri.dup.hash)
end
it "should have a different hash from its equivalent String value" do
expect(@uri.hash).not_to eq(@uri.to_s.hash)
end
it "should have the same hash as an equal URI" do
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com/").hash)
end
it "should be equivalent to http://EXAMPLE.com" do
expect(@uri).to eq(Addressable::URI.parse("http://EXAMPLE.com"))
end
it "should be equivalent to http://EXAMPLE.com:80/" do
expect(@uri).to eq(Addressable::URI.parse("http://EXAMPLE.com:80/"))
end
it "should have the same hash as http://example.com/" do
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com/").hash)
end
it "should have the same hash as http://example.com after assignment" do
@uri.path = ""
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com").hash)
end
it "should have the same hash as http://example.com/? after assignment" do
@uri.query = ""
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com/?").hash)
end
it "should have the same hash as http://example.com/? after assignment" do
@uri.query_values = {}
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com/?").hash)
end
it "should have the same hash as http://example.com/# after assignment" do
@uri.fragment = ""
expect(@uri.hash).to eq(Addressable::URI.parse("http://example.com/#").hash)
end
it "should have a different hash from http://example.com" do
expect(@uri.hash).not_to eq(Addressable::URI.parse("http://example.com").hash)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com?#'" do
before do
@uri = Addressable::URI.parse("http://example.com?#")
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "example.com",
:port => nil,
:path => "",
:query => "",
:fragment => ""
})
end
it "should have a request URI of '/?'" do
expect(@uri.request_uri).to eq("/?")
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq("http://example.com")
end
end
describe Addressable::URI, "when parsed from " +
"'http://@example.com/'" do
before do
@uri = Addressable::URI.parse("http://@example.com/")
end
it "should be equivalent to http://example.com" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com"))
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => "",
:password => nil,
:host => "example.com",
:port => nil,
:path => "/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com./'" do
before do
@uri = Addressable::URI.parse("http://example.com./")
end
it "should be equivalent to http://example.com" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com"))
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
end
describe Addressable::URI, "when parsed from " +
"'http://:@example.com/'" do
before do
@uri = Addressable::URI.parse("http://:@example.com/")
end
it "should be equivalent to http://example.com" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com"))
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => "",
:password => "",
:host => "example.com",
:port => nil,
:path => "/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
end
describe Addressable::URI, "when parsed from " +
"'HTTP://EXAMPLE.COM/'" do
before do
@uri = Addressable::URI.parse("HTTP://EXAMPLE.COM/")
end
it "should be equivalent to http://example.com" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com"))
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "HTTP",
:user => nil,
:password => nil,
:host => "EXAMPLE.COM",
:port => nil,
:path => "/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
it "should have a tld of 'com'" do
expect(@uri.tld).to eq('com')
end
end
describe Addressable::URI, "when parsed from " +
"'http://www.example.co.uk/'" do
before do
@uri = Addressable::URI.parse("http://www.example.co.uk/")
end
it "should have an origin of 'http://www.example.co.uk'" do
expect(@uri.origin).to eq('http://www.example.co.uk')
end
it "should have a tld of 'co.uk'" do
expect(@uri.tld).to eq('co.uk')
end
it "should have a domain of 'example.co.uk'" do
expect(@uri.domain).to eq('example.co.uk')
end
end
describe Addressable::URI, "when parsed from " +
"'http://sub_domain.blogspot.com/'" do
before do
@uri = Addressable::URI.parse("http://sub_domain.blogspot.com/")
end
it "should have an origin of 'http://sub_domain.blogspot.com'" do
expect(@uri.origin).to eq('http://sub_domain.blogspot.com')
end
it "should have a tld of 'com'" do
expect(@uri.tld).to eq('com')
end
it "should have a domain of 'blogspot.com'" do
expect(@uri.domain).to eq('blogspot.com')
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/~smith/'" do
before do
@uri = Addressable::URI.parse("http://example.com/~smith/")
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to http://example.com/%7Esmith/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com/%7Esmith/"))
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to http://example.com/%7esmith/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com/%7esmith/"))
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/%E8'" do
before do
@uri = Addressable::URI.parse("http://example.com/%E8")
end
it "should not raise an exception when normalized" do
expect(lambda do
@uri.normalize
end).not_to raise_error
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should not change if encoded with the normalizing algorithm" do
expect(Addressable::URI.normalized_encode(@uri).to_s).to eq(
"http://example.com/%E8"
)
expect(Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s).to be ===
"http://example.com/%E8"
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/path%2Fsegment/'" do
before do
@uri = Addressable::URI.parse("http://example.com/path%2Fsegment/")
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should be equal to 'http://example.com/path%2Fsegment/'" do
expect(@uri.normalize).to be_eql(
Addressable::URI.parse("http://example.com/path%2Fsegment/")
)
end
it "should not be equal to 'http://example.com/path/segment/'" do
expect(@uri).not_to eq(
Addressable::URI.parse("http://example.com/path/segment/")
)
end
it "should not be equal to 'http://example.com/path/segment/'" do
expect(@uri.normalize).not_to be_eql(
Addressable::URI.parse("http://example.com/path/segment/")
)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/?%F6'" do
before do
@uri = Addressable::URI.parse("http://example.com/?%F6")
end
it "should not raise an exception when normalized" do
expect(lambda do
@uri.normalize
end).not_to raise_error
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should not change if encoded with the normalizing algorithm" do
expect(Addressable::URI.normalized_encode(@uri).to_s).to eq(
"http://example.com/?%F6"
)
expect(Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s).to be ===
"http://example.com/?%F6"
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/#%F6'" do
before do
@uri = Addressable::URI.parse("http://example.com/#%F6")
end
it "should not raise an exception when normalized" do
expect(lambda do
@uri.normalize
end).not_to raise_error
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should not change if encoded with the normalizing algorithm" do
expect(Addressable::URI.normalized_encode(@uri).to_s).to eq(
"http://example.com/#%F6"
)
expect(Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s).to be ===
"http://example.com/#%F6"
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/%C3%87'" do
before do
@uri = Addressable::URI.parse("http://example.com/%C3%87")
end
# Based on http://intertwingly.net/blog/2004/07/31/URI-Equivalence
it "should be equivalent to 'http://example.com/C%CC%A7'" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com/C%CC%A7"))
end
it "should not change if encoded with the normalizing algorithm" do
expect(Addressable::URI.normalized_encode(@uri).to_s).to eq(
"http://example.com/%C3%87"
)
expect(Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s).to be ===
"http://example.com/%C3%87"
end
it "should raise an error if encoding with an unexpected return type" do
expect(lambda do
Addressable::URI.normalized_encode(@uri, Integer)
end).to raise_error(TypeError)
end
it "if percent encoded should be 'http://example.com/C%25CC%25A7'" do
expect(Addressable::URI.encode(@uri).to_s).to eq(
"http://example.com/%25C3%2587"
)
end
it "if percent encoded should be 'http://example.com/C%25CC%25A7'" do
expect(Addressable::URI.encode(@uri, Addressable::URI)).to eq(
Addressable::URI.parse("http://example.com/%25C3%2587")
)
end
it "should raise an error if encoding with an unexpected return type" do
expect(lambda do
Addressable::URI.encode(@uri, Integer)
end).to raise_error(TypeError)
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/?q=string'" do
before do
@uri = Addressable::URI.parse("http://example.com/?q=string")
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com'" do
expect(@uri.authority).to eq("example.com")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a path of '/'" do
expect(@uri.path).to eq("/")
end
it "should have a query string of 'q=string'" do
expect(@uri.query).to eq("q=string")
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should be considered absolute" do
expect(@uri).to be_absolute
end
it "should not be considered relative" do
expect(@uri).not_to be_relative
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com:80/'" do
before do
@uri = Addressable::URI.parse("http://example.com:80/")
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com:80'" do
expect(@uri.authority).to eq("example.com:80")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have explicit port 80" do
expect(@uri.port).to eq(80)
end
it "should have a path of '/'" do
expect(@uri.path).to eq("/")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should be considered absolute" do
expect(@uri).to be_absolute
end
it "should not be considered relative" do
expect(@uri).not_to be_relative
end
it "should be exactly equal to http://example.com:80/" do
expect(@uri.eql?(Addressable::URI.parse("http://example.com:80/"))).to eq(true)
end
it "should be roughly equal to http://example.com/" do
expect(@uri === Addressable::URI.parse("http://example.com/")).to eq(true)
end
it "should be roughly equal to the string 'http://example.com/'" do
expect(@uri === "http://example.com/").to eq(true)
end
it "should not be roughly equal to the string " +
"'http://example.com:bogus/'" do
expect(lambda do
expect(@uri === "http://example.com:bogus/").to eq(false)
end).not_to raise_error
end
it "should result in itself when joined with itself" do
expect(@uri.join(@uri).to_s).to eq("http://example.com:80/")
expect(@uri.join!(@uri).to_s).to eq("http://example.com:80/")
end
# Section 6.2.3 of RFC 3986
it "should be equal to http://example.com/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com/"))
end
# Section 6.2.3 of RFC 3986
it "should be equal to http://example.com:/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com:/"))
end
# Section 6.2.3 of RFC 3986
it "should be equal to http://example.com:80/" do
expect(@uri).to eq(Addressable::URI.parse("http://example.com:80/"))
end
# Section 6.2.2.1 of RFC 3986
it "should be equal to http://EXAMPLE.COM/" do
expect(@uri).to eq(Addressable::URI.parse("http://EXAMPLE.COM/"))
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "example.com",
:port => 80,
:path => "/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
it "should not change if encoded with the normalizing algorithm" do
expect(Addressable::URI.normalized_encode(@uri).to_s).to eq(
"http://example.com:80/"
)
expect(Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s).to be ===
"http://example.com:80/"
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com:8080/'" do
before do
@uri = Addressable::URI.parse("http://example.com:8080/")
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com:8080'" do
expect(@uri.authority).to eq("example.com:8080")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 8080" do
expect(@uri.inferred_port).to eq(8080)
end
it "should have explicit port 8080" do
expect(@uri.port).to eq(8080)
end
it "should have default port 80" do
expect(@uri.default_port).to eq(80)
end
it "should have a path of '/'" do
expect(@uri.path).to eq("/")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should be considered absolute" do
expect(@uri).to be_absolute
end
it "should not be considered relative" do
expect(@uri).not_to be_relative
end
it "should be exactly equal to http://example.com:8080/" do
expect(@uri.eql?(Addressable::URI.parse(
"http://example.com:8080/"))).to eq(true)
end
it "should have a route of 'http://example.com:8080/' from " +
"'http://example.com/path/to/'" do
expect(@uri.route_from("http://example.com/path/to/")).to eq(
Addressable::URI.parse("http://example.com:8080/")
)
end
it "should have a route of 'http://example.com:8080/' from " +
"'http://example.com:80/path/to/'" do
expect(@uri.route_from("http://example.com:80/path/to/")).to eq(
Addressable::URI.parse("http://example.com:8080/")
)
end
it "should have a route of '../../' from " +
"'http://example.com:8080/path/to/'" do
expect(@uri.route_from("http://example.com:8080/path/to/")).to eq(
Addressable::URI.parse("../../")
)
end
it "should have a route of 'http://example.com:8080/' from " +
"'http://user:pass@example.com/path/to/'" do
expect(@uri.route_from("http://user:pass@example.com/path/to/")).to eq(
Addressable::URI.parse("http://example.com:8080/")
)
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "example.com",
:port => 8080,
:path => "/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
it "should have an origin of 'http://example.com:8080'" do
expect(@uri.origin).to eq('http://example.com:8080')
end
it "should not change if encoded with the normalizing algorithm" do
expect(Addressable::URI.normalized_encode(@uri).to_s).to eq(
"http://example.com:8080/"
)
expect(Addressable::URI.normalized_encode(@uri, Addressable::URI).to_s).to be ===
"http://example.com:8080/"
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com:%38%30/'" do
before do
@uri = Addressable::URI.parse("http://example.com:%38%30/")
end
it "should have the correct port" do
expect(@uri.port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
it "should have an origin of 'http://example.com'" do
expect(@uri.origin).to eq('http://example.com')
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/%2E/'" do
before do
@uri = Addressable::URI.parse("http://example.com/%2E/")
end
it "should be considered to be in normal form" do
skip(
'path segment normalization should happen before ' +
'percent escaping normalization'
)
@uri.normalize.should be_eql(@uri)
end
it "should normalize to 'http://example.com/%2E/'" do
skip(
'path segment normalization should happen before ' +
'percent escaping normalization'
)
expect(@uri.normalize).to eq("http://example.com/%2E/")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/..'" do
before do
@uri = Addressable::URI.parse("http://example.com/..")
end
it "should have the correct port" do
expect(@uri.inferred_port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/../..'" do
before do
@uri = Addressable::URI.parse("http://example.com/../..")
end
it "should have the correct port" do
expect(@uri.inferred_port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/path(/..'" do
before do
@uri = Addressable::URI.parse("http://example.com/path(/..")
end
it "should have the correct port" do
expect(@uri.inferred_port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/(path)/..'" do
before do
@uri = Addressable::URI.parse("http://example.com/(path)/..")
end
it "should have the correct port" do
expect(@uri.inferred_port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/path(/../'" do
before do
@uri = Addressable::URI.parse("http://example.com/path(/../")
end
it "should have the correct port" do
expect(@uri.inferred_port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/(path)/../'" do
before do
@uri = Addressable::URI.parse("http://example.com/(path)/../")
end
it "should have the correct port" do
expect(@uri.inferred_port).to eq(80)
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://example.com/'" do
expect(@uri.normalize.to_s).to eq("http://example.com/")
end
end
describe Addressable::URI, "when parsed from " +
"'/..//example.com'" do
before do
@uri = Addressable::URI.parse("/..//example.com")
end
it "should become invalid when normalized" do
expect(lambda do
@uri.normalize
end).to raise_error(Addressable::URI::InvalidURIError, /authority/)
end
it "should have a path of '/..//example.com'" do
expect(@uri.path).to eq("/..//example.com")
end
end
describe Addressable::URI, "when parsed from '/a/b/c/./../../g'" do
before do
@uri = Addressable::URI.parse("/a/b/c/./../../g")
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
# Section 5.2.4 of RFC 3986
it "should normalize to '/a/g'" do
expect(@uri.normalize.to_s).to eq("/a/g")
end
end
describe Addressable::URI, "when parsed from 'mid/content=5/../6'" do
before do
@uri = Addressable::URI.parse("mid/content=5/../6")
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
# Section 5.2.4 of RFC 3986
it "should normalize to 'mid/6'" do
expect(@uri.normalize.to_s).to eq("mid/6")
end
end
describe Addressable::URI, "when parsed from " +
"'http://www.example.com///../'" do
before do
@uri = Addressable::URI.parse('http://www.example.com///../')
end
it "should not be considered to be in normal form" do
expect(@uri.normalize).not_to be_eql(@uri)
end
it "should normalize to 'http://www.example.com//'" do
expect(@uri.normalize.to_s).to eq("http://www.example.com//")
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/path/to/resource/'" do
before do
@uri = Addressable::URI.parse("http://example.com/path/to/resource/")
end
it "should use the 'http' scheme" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com'" do
expect(@uri.authority).to eq("example.com")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a path of '/path/to/resource/'" do
expect(@uri.path).to eq("/path/to/resource/")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should be considered absolute" do
expect(@uri).to be_absolute
end
it "should not be considered relative" do
expect(@uri).not_to be_relative
end
it "should be exactly equal to http://example.com:8080/" do
expect(@uri.eql?(Addressable::URI.parse(
"http://example.com/path/to/resource/"))).to eq(true)
end
it "should have a route of 'resource/' from " +
"'http://example.com/path/to/'" do
expect(@uri.route_from("http://example.com/path/to/")).to eq(
Addressable::URI.parse("resource/")
)
end
it "should have a route of '../' from " +
"'http://example.com/path/to/resource/sub'" do
expect(@uri.route_from("http://example.com/path/to/resource/sub")).to eq(
Addressable::URI.parse("../")
)
end
it "should have a route of 'resource/' from " +
"'http://example.com/path/to/another'" do
expect(@uri.route_from("http://example.com/path/to/another")).to eq(
Addressable::URI.parse("resource/")
)
end
it "should have a route of 'resource/' from " +
"'http://example.com/path/to/res'" do
expect(@uri.route_from("http://example.com/path/to/res")).to eq(
Addressable::URI.parse("resource/")
)
end
it "should have a route of 'resource/' from " +
"'http://example.com:80/path/to/'" do
expect(@uri.route_from("http://example.com:80/path/to/")).to eq(
Addressable::URI.parse("resource/")
)
end
it "should have a route of 'http://example.com/path/to/' from " +
"'http://example.com:8080/path/to/'" do
expect(@uri.route_from("http://example.com:8080/path/to/")).to eq(
Addressable::URI.parse("http://example.com/path/to/resource/")
)
end
it "should have a route of 'http://example.com/path/to/' from " +
"'http://user:pass@example.com/path/to/'" do
expect(@uri.route_from("http://user:pass@example.com/path/to/")).to eq(
Addressable::URI.parse("http://example.com/path/to/resource/")
)
end
it "should have a route of '../../path/to/resource/' from " +
"'http://example.com/to/resource/'" do
expect(@uri.route_from("http://example.com/to/resource/")).to eq(
Addressable::URI.parse("../../path/to/resource/")
)
end
it "should correctly convert to a hash" do
expect(@uri.to_hash).to eq({
:scheme => "http",
:user => nil,
:password => nil,
:host => "example.com",
:port => nil,
:path => "/path/to/resource/",
:query => nil,
:fragment => nil
})
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
end
describe Addressable::URI, "when parsed from " +
"'relative/path/to/resource'" do
before do
@uri = Addressable::URI.parse("relative/path/to/resource")
end
it "should not have a scheme" do
expect(@uri.scheme).to eq(nil)
end
it "should not be considered ip-based" do
expect(@uri).not_to be_ip_based
end
it "should not have an authority segment" do
expect(@uri.authority).to eq(nil)
end
it "should not have a host" do
expect(@uri.host).to eq(nil)
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should not have a port" do
expect(@uri.port).to eq(nil)
end
it "should have a path of 'relative/path/to/resource'" do
expect(@uri.path).to eq("relative/path/to/resource")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should not be considered absolute" do
expect(@uri).not_to be_absolute
end
it "should be considered relative" do
expect(@uri).to be_relative
end
it "should raise an error if routing is attempted" do
expect(lambda do
@uri.route_to("http://example.com/")
end).to raise_error(ArgumentError, /relative\/path\/to\/resource/)
expect(lambda do
@uri.route_from("http://example.com/")
end).to raise_error(ArgumentError, /relative\/path\/to\/resource/)
end
it "when joined with 'another/relative/path' should be " +
"'relative/path/to/another/relative/path'" do
expect(@uri.join('another/relative/path')).to eq(
Addressable::URI.parse("relative/path/to/another/relative/path")
)
end
it "should be identical to its duplicate" do
expect(@uri).to eq(@uri.dup)
end
end
describe Addressable::URI, "when parsed from " +
"'relative_path_with_no_slashes'" do
before do
@uri = Addressable::URI.parse("relative_path_with_no_slashes")
end
it "should not have a scheme" do
expect(@uri.scheme).to eq(nil)
end
it "should not be considered ip-based" do
expect(@uri).not_to be_ip_based
end
it "should not have an authority segment" do
expect(@uri.authority).to eq(nil)
end
it "should not have a host" do
expect(@uri.host).to eq(nil)
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should not have a port" do
expect(@uri.port).to eq(nil)
end
it "should have a path of 'relative_path_with_no_slashes'" do
expect(@uri.path).to eq("relative_path_with_no_slashes")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should not be considered absolute" do
expect(@uri).not_to be_absolute
end
it "should be considered relative" do
expect(@uri).to be_relative
end
it "when joined with 'another_relative_path' should be " +
"'another_relative_path'" do
expect(@uri.join('another_relative_path')).to eq(
Addressable::URI.parse("another_relative_path")
)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/file.txt'" do
before do
@uri = Addressable::URI.parse("http://example.com/file.txt")
end
it "should have a scheme of 'http'" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com'" do
expect(@uri.authority).to eq("example.com")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a path of '/file.txt'" do
expect(@uri.path).to eq("/file.txt")
end
it "should have a basename of 'file.txt'" do
expect(@uri.basename).to eq("file.txt")
end
it "should have an extname of '.txt'" do
expect(@uri.extname).to eq(".txt")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/file.txt;parameter'" do
before do
@uri = Addressable::URI.parse("http://example.com/file.txt;parameter")
end
it "should have a scheme of 'http'" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com'" do
expect(@uri.authority).to eq("example.com")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a path of '/file.txt;parameter'" do
expect(@uri.path).to eq("/file.txt;parameter")
end
it "should have a basename of 'file.txt'" do
expect(@uri.basename).to eq("file.txt")
end
it "should have an extname of '.txt'" do
expect(@uri.extname).to eq(".txt")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
end
describe Addressable::URI, "when parsed from " +
"'http://example.com/file.txt;x=y'" do
before do
@uri = Addressable::URI.parse("http://example.com/file.txt;x=y")
end
it "should have a scheme of 'http'" do
expect(@uri.scheme).to eq("http")
end
it "should have a scheme of 'http'" do
expect(@uri.scheme).to eq("http")
end
it "should have an authority segment of 'example.com'" do
expect(@uri.authority).to eq("example.com")
end
it "should have a host of 'example.com'" do
expect(@uri.host).to eq("example.com")
end
it "should have no username" do
expect(@uri.user).to eq(nil)
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should use port 80" do
expect(@uri.inferred_port).to eq(80)
end
it "should have a path of '/file.txt;x=y'" do
expect(@uri.path).to eq("/file.txt;x=y")
end
it "should have an extname of '.txt'" do
expect(@uri.extname).to eq(".txt")
end
it "should have no query string" do
expect(@uri.query).to eq(nil)
end
it "should have no fragment" do
expect(@uri.fragment).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
end
describe Addressable::URI, "when parsed from " +
"'svn+ssh://developername@rubyforge.org/var/svn/project'" do
before do
@uri = Addressable::URI.parse(
"svn+ssh://developername@rubyforge.org/var/svn/project"
)
end
it "should have a scheme of 'svn+ssh'" do
expect(@uri.scheme).to eq("svn+ssh")
end
it "should be considered to be ip-based" do
expect(@uri).to be_ip_based
end
it "should have a path of '/var/svn/project'" do
expect(@uri.path).to eq("/var/svn/project")
end
it "should have a username of 'developername'" do
expect(@uri.user).to eq("developername")
end
it "should have no password" do
expect(@uri.password).to eq(nil)
end
it "should be considered to be in normal form" do
expect(@uri.normalize).to be_eql(@uri)
end
end
describe Addressable::URI, "when parsed from " +
"'ssh+svn://developername@RUBYFORGE.ORG/var/svn/project'" do
before do
@uri = Addressable::URI.parse(
"ssh+svn://developername@RUBYFORGE.ORG/var/svn/project"
)
end
it "should have a scheme of 'ssh+svn'" do
expect(@uri.scheme).to eq("ssh+svn")
end
it "should have a normalized scheme of 'svn+ssh'" do
expect(@uri.normalized_scheme).to eq("svn+ssh")
end
it "should have a normalized site of 'svn+ssh'" do
expect(@uri.normalized_site).to eq("svn+ssh://developername@rubyforge.org")
end
it "should not be considered to be ip-based" do
expect(@uri).not_to be_ip_based
end
it "should have a path of '/var/svn/project'" do
expect(@uri.path).to eq("/var/svn/project")
end
it "should have a username of 'developername'" do
expect(@uri.user).to eq("developername")
end
it "should have no password" do