File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -446,9 +446,9 @@ public String getRawHost() {
446446 return host ;
447447 }
448448
449- /** Returns the "port" component of this URI, or -1 if not present. */
449+ /** Returns the "port" component of this URI, or -1 if empty or not present. */
450450 public int getPort () {
451- return port != null ? Integer .parseInt (port ) : -1 ;
451+ return port != null && ! port . isEmpty () ? Integer .parseInt (port ) : -1 ;
452452 }
453453
454454 /** Returns the raw port component of this URI in its originally parsed form. */
@@ -943,10 +943,12 @@ public Builder setPort(int port) {
943943
944944 @ CanIgnoreReturnValue
945945 Builder setRawPort (String port ) {
946- try {
947- Integer .parseInt (port ); // Result unused.
948- } catch (NumberFormatException e ) {
949- throw new IllegalArgumentException ("Invalid port" , e );
946+ if (port != null && !port .isEmpty ()) {
947+ try {
948+ Integer .parseInt (port ); // Result unused.
949+ } catch (NumberFormatException e ) {
950+ throw new IllegalArgumentException ("Invalid port" , e );
951+ }
950952 }
951953 this .port = port ;
952954 return this ;
Original file line number Diff line number Diff line change @@ -185,6 +185,18 @@ public void parse_emptyUserInfo() {
185185 assertThat (uri .toString ()).isEqualTo ("scheme://@host" );
186186 }
187187
188+ @ Test
189+ public void parse_emptyPort () {
190+ Uri uri = Uri .create ("scheme://host:" );
191+ assertThat (uri .getScheme ()).isEqualTo ("scheme" );
192+ assertThat (uri .getAuthority ()).isEqualTo ("host:" );
193+ assertThat (uri .getRawAuthority ()).isEqualTo ("host:" );
194+ assertThat (uri .getHost ()).isEqualTo ("host" );
195+ assertThat (uri .getPort ()).isEqualTo (-1 );
196+ assertThat (uri .getRawPort ()).isEqualTo ("" );
197+ assertThat (uri .toString ()).isEqualTo ("scheme://host:" );
198+ }
199+
188200 @ Test
189201 public void parse_invalidScheme_throws () {
190202 URISyntaxException e =
@@ -259,13 +271,6 @@ public void parse_invalidBackslashScope_throws() {
259271 assertThat (e ).hasMessageThat ().contains ("Invalid character in scope" );
260272 }
261273
262- @ Test
263- public void parse_emptyPort_throws () {
264- URISyntaxException e =
265- assertThrows (URISyntaxException .class , () -> Uri .parse ("scheme://user@host:/path" ));
266- assertThat (e ).hasMessageThat ().contains ("Invalid port" );
267- }
268-
269274 @ Test
270275 public void parse_invalidCharactersInPort_throws () {
271276 URISyntaxException e =
You can’t perform that action at this time.
0 commit comments