You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2018/07/14 10:12:58 UTC

httpcomponents-core git commit: URIBuilder to tolerate null and blank input

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master e6f3d649c -> c4342118e


URIBuilder to tolerate null and blank input


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/c4342118
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/c4342118
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/c4342118

Branch: refs/heads/master
Commit: c4342118e1cde12b91e630e6a5a4df8f7f69bb43
Parents: e6f3d64
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Sat Jul 14 12:10:29 2018 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Jul 14 12:10:29 2018 +0200

----------------------------------------------------------------------
 .../org/apache/hc/core5/net/URIBuilder.java     | 13 +++++----
 .../org/apache/hc/core5/net/TestURIBuilder.java | 30 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/c4342118/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
----------------------------------------------------------------------
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 89ef6df..6990eb6 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
@@ -241,7 +241,7 @@ public class URIBuilder {
      * Sets URI scheme.
      */
     public URIBuilder setScheme(final String scheme) {
-        this.scheme = scheme;
+        this.scheme = !TextUtils.isBlank(scheme) ? scheme : null;
         return this;
     }
 
@@ -250,7 +250,7 @@ public class URIBuilder {
      * characters.
      */
     public URIBuilder setUserInfo(final String userInfo) {
-        this.userInfo = userInfo;
+        this.userInfo = !TextUtils.isBlank(userInfo) ? userInfo : null;
         this.encodedSchemeSpecificPart = null;
         this.encodedAuthority = null;
         this.encodedUserInfo = null;
@@ -271,7 +271,7 @@ public class URIBuilder {
      * @since 4.6
      */
     public URIBuilder setHost(final InetAddress host) {
-        this.host = host.getHostAddress();
+        this.host = host != null ? host.getHostAddress() : null;
         this.encodedSchemeSpecificPart = null;
         this.encodedAuthority = null;
         return this;
@@ -281,6 +281,7 @@ public class URIBuilder {
      * Sets URI host.
      */
     public URIBuilder setHost(final String host) {
+        this.host = !TextUtils.isBlank(host) ? host : null;
         this.host = host;
         this.encodedSchemeSpecificPart = null;
         this.encodedAuthority = null;
@@ -301,7 +302,7 @@ public class URIBuilder {
      * Sets URI path. The value is expected to be unescaped and may contain non ASCII characters.
      */
     public URIBuilder setPath(final String path) {
-        this.path = path;
+        this.path = !TextUtils.isBlank(path) ? path : null;
         this.encodedSchemeSpecificPart = null;
         this.encodedPath = null;
         return this;
@@ -456,7 +457,7 @@ public class URIBuilder {
      * @since 4.3
      */
     public URIBuilder setCustomQuery(final String query) {
-        this.query = query;
+        this.query = !TextUtils.isBlank(query) ? query : null;
         this.encodedQuery = null;
         this.encodedSchemeSpecificPart = null;
         this.queryParams = null;
@@ -468,7 +469,7 @@ public class URIBuilder {
      * characters.
      */
     public URIBuilder setFragment(final String fragment) {
-        this.fragment = fragment;
+        this.fragment = !TextUtils.isBlank(fragment) ? fragment : null;
         this.encodedFragment = null;
         return this;
     }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/c4342118/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
----------------------------------------------------------------------
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 ae376b1..c069ea1 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
@@ -37,6 +37,7 @@ import java.util.List;
 
 import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
+import org.hamcrest.CoreMatchers;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -383,4 +384,33 @@ public class TestURIBuilder {
         Assert.assertEquals(new URI("http://somehost/./mypath"), uri);
     }
 
+    @Test
+    public void testTolerateNullInput() throws Exception {
+        Assert.assertThat(new URIBuilder()
+                        .setScheme(null)
+                        .setHost("localhost")
+                        .setUserInfo(null)
+                        .setPort(8443)
+                        .setPath(null)
+                        .setCustomQuery(null)
+                        .setFragment(null)
+                        .build(),
+                CoreMatchers.equalTo(URI.create("//localhost:8443")));
+    }
+
+    @Test
+    public void testTolerateBlankInput() throws Exception {
+        Assert.assertThat(new URIBuilder()
+                        .setScheme("")
+                        .setHost("localhost")
+                        .setUserInfo("")
+                        .setPort(8443)
+                        .setPath("")
+                        .setPath("")
+                        .setCustomQuery("")
+                        .setFragment("")
+                        .build(),
+                CoreMatchers.equalTo(URI.create("//localhost:8443")));
+    }
+
 }