File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -747,6 +747,45 @@ def port=(v)
747747 port
748748 end
749749
750+ #
751+ # == Description
752+ #
753+ # Returns the authority of the URI, as defined in
754+ # https://www.rfc-editor.org/rfc/rfc3986#section-3.2.
755+ #
756+ # authority = [ userinfo "@" ] host [ ":" port ]
757+ #
758+ # Returns an empty string if no authority is present.
759+ #
760+ # == Usage
761+ #
762+ # require 'uri'
763+ #
764+ # URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority
765+ # #=> "www.example.com"
766+ # URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority
767+ # #=> "www.example.com:8000"
768+ # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority
769+ # #=> "www.example.com"
770+ # URI::HTTP.build(host: 'www.example.com', userinfo: 'user:password').authority
771+ # #=> "user:password@www.example.com"
772+ #
773+ def authority
774+ str = '' . dup
775+ if self . userinfo
776+ str << self . userinfo
777+ str << '@'
778+ end
779+ if @host
780+ str << @host
781+ end
782+ if @port && @port != self . default_port
783+ str << ':'
784+ str << @port . to_s
785+ end
786+ str
787+ end
788+
750789 def check_registry ( v ) # :nodoc:
751790 raise InvalidURIError , "cannot set registry"
752791 end
@@ -1365,17 +1404,7 @@ def to_s
13651404 if @host || %w[ file postgres ] . include? ( @scheme )
13661405 str << '//'
13671406 end
1368- if self . userinfo
1369- str << self . userinfo
1370- str << '@'
1371- end
1372- if @host
1373- str << @host
1374- end
1375- if @port && @port != self . default_port
1376- str << ':'
1377- str << @port . to_s
1378- end
1407+ str << self . authority
13791408 if ( @host || @port ) && !@path . empty? && !@path . start_with? ( '/' )
13801409 str << '/'
13811410 end
Original file line number Diff line number Diff line change @@ -93,27 +93,6 @@ def request_uri
9393 url . start_with? ( ?/. freeze ) ? url : ?/ + url
9494 end
9595
96- #
97- # == Description
98- #
99- # Returns the authority for an HTTP uri, as defined in
100- # https://www.rfc-editor.org/rfc/rfc3986#section-3.2.
101- #
102- #
103- # Example:
104- #
105- # URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority #=> "www.example.com"
106- # URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority #=> "www.example.com:8000"
107- # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com"
108- #
109- def authority
110- if port == default_port
111- host
112- else
113- "#{ host } :#{ port } "
114- end
115- end
116-
11796 #
11897 # == Description
11998 #
Original file line number Diff line number Diff line change @@ -73,9 +73,9 @@ def test_authority
7373 assert_equal ( 'a.b.c' , URI . parse ( 'http://a.b.c/' ) . authority )
7474 assert_equal ( 'a.b.c:8081' , URI . parse ( 'http://a.b.c:8081/' ) . authority )
7575 assert_equal ( 'a.b.c' , URI . parse ( 'http://a.b.c:80/' ) . authority )
76+ assert_equal ( 'userinfo@a.b.c' , URI . parse ( 'https://userinfo@a.b.c/' ) . authority )
7677 end
7778
78-
7979 def test_origin
8080 assert_equal ( 'http://a.b.c' , URI . parse ( 'http://a.b.c/' ) . origin )
8181 assert_equal ( 'http://a.b.c:8081' , URI . parse ( 'http://a.b.c:8081/' ) . origin )
You can’t perform that action at this time.
0 commit comments