You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2021/10/22 11:58:41 UTC

[httpcomponents-core] branch master updated: Set a URIBuilder authority with with a URIAuthority or NamedEndpoint. (#308)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new c1bd88b  Set a URIBuilder authority with with a URIAuthority or NamedEndpoint. (#308)
c1bd88b is described below

commit c1bd88ba5589430e1cb5d6ab2a5ff28c3f8f6964
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Fri Oct 22 07:57:37 2021 -0400

    Set a URIBuilder authority with with a URIAuthority or NamedEndpoint. (#308)
    
    Co-authored-by: Gary Gregory <gg...@rocketsoftware.com>
---
 .../java/org/apache/hc/core5/net/URIAuthority.java |  4 +-
 .../java/org/apache/hc/core5/net/URIBuilder.java   | 40 +++++++++++++++++-
 .../org/apache/hc/core5/net/TestURIBuilder.java    | 48 ++++++++++++++++++++++
 3 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
index 939f27d..5fa2553 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java
@@ -87,6 +87,8 @@ public final class URIAuthority implements NamedEndpoint, Serializable {
     }
 
     /**
+     * Constructs a new instance.
+     *
      * @throws IllegalArgumentException
      *             If the port parameter is outside the specified range of valid port values, which is between 0 and
      *             65535, inclusive. {@code -1} indicates the scheme default port.
@@ -133,7 +135,7 @@ public final class URIAuthority implements NamedEndpoint, Serializable {
     }
 
     /**
-     * Creates {@code URIHost} instance from string. Text may not contain any blanks.
+     * Creates a {@code URIAuthority} instance from a string. Text may not contain any blanks.
      */
     public static URIAuthority create(final String s) throws URISyntaxException {
         if (TextUtils.isBlank(s)) {
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
index c1322a8..74ece31 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
@@ -136,6 +136,34 @@ public class URIBuilder {
     }
 
     /**
+     * Sets the authority.
+     *
+     * @param charset the authority.
+     * @return this.
+     * @since 5.2
+     */
+    public URIBuilder setAuthority(final NamedEndpoint authority) {
+        setUserInfo(null);
+        setHost(authority.getHostName());
+        setPort(authority.getPort());
+        return this;
+    }
+
+    /**
+     * Sets the authority.
+     *
+     * @param charset the authority.
+     * @return this.
+     * @since 5.2
+     */
+    public URIBuilder setAuthority(final URIAuthority authority) {
+        setUserInfo(authority.getUserInfo());
+        setHost(authority.getHostName());
+        setPort(authority.getPort());
+        return this;
+    }
+
+    /**
      * Sets the Charset.
      *
      * @param charset the Charset.
@@ -147,6 +175,16 @@ public class URIBuilder {
     }
 
     /**
+     * Gets the authority.
+     *
+     * @return the authority.
+     * @since 5.2
+     */
+    public URIAuthority getAuthority() {
+        return new URIAuthority(getUserInfo(), getHost(), getPort());
+    }
+
+    /**
      * Gets the Charset.
      *
      * @return the Charset.
@@ -491,7 +529,7 @@ public class URIBuilder {
      * @param httpHost the scheme, host name, and port.
      * @return this.
      */
-    public URIBuilder setHttpHost(final HttpHost httpHost ) {
+    public URIBuilder setHttpHost(final HttpHost httpHost) {
         setScheme(httpHost.getSchemeName());
         setHost(httpHost.getHostName());
         setPort(httpHost.getPort());
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
index 4c331b6..49e129b 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
@@ -38,6 +38,7 @@ import java.util.List;
 import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.NameValuePairListMatcher;
+import org.apache.hc.core5.http.URIScheme;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
 import org.hamcrest.CoreMatchers;
 import org.hamcrest.MatcherAssert;
@@ -323,6 +324,53 @@ public class TestURIBuilder {
     }
 
     @Test
+    public void testSetAuthorityFromNamedEndpointHost() throws Exception {
+        final Host host = Host.create("localhost:88");
+        final URIBuilder uribuilder = new URIBuilder().setScheme(URIScheme.HTTP.id).setAuthority(host);
+        // Check builder
+        Assert.assertNull(uribuilder.getUserInfo());
+        Assert.assertEquals(host.getHostName(), uribuilder.getAuthority().getHostName());
+        Assert.assertEquals(host.getHostName(), uribuilder.getHost());
+        // Check result
+        final URI result = uribuilder.build();
+        Assert.assertEquals(host.getHostName(), result.getHost());
+        Assert.assertEquals(host.getPort(), result.getPort());
+        Assert.assertEquals(new URI("http://localhost:88"), result);
+    }
+
+    @Test
+    public void testSetAuthorityFromNamedEndpointHttpHost() throws Exception {
+        final HttpHost httpHost = HttpHost.create("localhost:88");
+        final URIBuilder uribuilder = new URIBuilder().setScheme(URIScheme.HTTP.id).setAuthority(httpHost);
+        // Check builder
+        Assert.assertNull(uribuilder.getUserInfo());
+        Assert.assertEquals(httpHost.getHostName(), uribuilder.getAuthority().getHostName());
+        Assert.assertEquals(httpHost.getHostName(), uribuilder.getHost());
+        // Check result
+        final URI result = uribuilder.build();
+        Assert.assertEquals(httpHost.getHostName(), result.getHost());
+        Assert.assertEquals(httpHost.getPort(), result.getPort());
+        Assert.assertEquals(new URI("http://localhost:88"), result);
+    }
+
+    @Test
+    public void testSetAuthorityFromURIAuthority() throws Exception {
+        final URIAuthority authority = URIAuthority.create("u:p@localhost:88");
+        final URIBuilder uribuilder = new URIBuilder().setScheme(URIScheme.HTTP.id).setAuthority(authority);
+        // Check builder
+        Assert.assertEquals(authority.getUserInfo(), uribuilder.getAuthority().getUserInfo());
+        Assert.assertEquals(authority.getHostName(), uribuilder.getAuthority().getHostName());
+        Assert.assertEquals(authority.getHostName(), uribuilder.getHost());
+        // Check result
+        final URI result = uribuilder.build();
+        Assert.assertEquals(authority.getUserInfo(), result.getUserInfo());
+        Assert.assertEquals(authority.getHostName(), result.getHost());
+        Assert.assertEquals(authority.getPort(), result.getPort());
+        Assert.assertEquals(authority.toString(), result.getAuthority());
+        Assert.assertEquals(new URI("http://u:p@localhost:88"), result);
+    }
+
+    @Test
     public void testSetParameter() throws Exception {
         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
         final URIBuilder uribuilder = new URIBuilder(uri).setParameter("param", "some other stuff")