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 2021/04/13 11:57:27 UTC

[httpcomponents-core] branch master updated: HTTPCLIENT-1916: Add URIBuilder.removeParameter (#283)

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

olegk 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 60d8940  HTTPCLIENT-1916: Add URIBuilder.removeParameter (#283)
60d8940 is described below

commit 60d89400a2964ca1182ee76041224c653042ffdb
Author: Peter Dettman <pe...@bouncycastle.org>
AuthorDate: Tue Apr 13 18:57:18 2021 +0700

    HTTPCLIENT-1916: Add URIBuilder.removeParameter (#283)
---
 .../java/org/apache/hc/core5/net/URIBuilder.java   | 24 +++++++++++++++++
 .../org/apache/hc/core5/net/TestURIBuilder.java    | 31 +++++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)

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 ab864d5..94df9c0 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
@@ -44,6 +44,7 @@ import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.message.BasicNameValuePair;
 import org.apache.hc.core5.http.message.ParserCursor;
+import org.apache.hc.core5.util.Args;
 import org.apache.hc.core5.util.TextUtils;
 import org.apache.hc.core5.util.Tokenizer;
 
@@ -695,6 +696,28 @@ public class URIBuilder {
     }
 
     /**
+     * Removes parameter of URI query if set. The parameter name is expected to be unescaped and may
+     * contain non ASCII characters.
+     * <p>
+     * Please note query parameters and custom query component are mutually exclusive. This method
+     * will remove custom query if present, even when no parameter was actually removed.
+     * </p>
+     *
+     * @return this.
+     * @since 5.2
+     */
+    public URIBuilder removeParameter(final String param) {
+        Args.notNull(param, "param");
+        if (this.queryParams != null && !this.queryParams.isEmpty()) {
+            this.queryParams.removeIf(nvp -> nvp.getName().equals(param));
+        }
+        this.encodedQuery = null;
+        this.encodedSchemeSpecificPart = null;
+        this.query = null;
+        return this;
+    }
+
+    /**
      * Sets parameter of URI query overriding existing value if set. The parameter name and value
      * are expected to be unescaped and may contain non ASCII characters.
      * <p>
@@ -850,6 +873,7 @@ public class URIBuilder {
      *  <li>characters of scheme and host components are converted to lower case</li>
      *  <li>dot segments of the path component are removed if the path has a root</li>
      *  <li>percent encoding of all components is normalized</li>
+     * </ul>
      *
      * @since 5.1
      */
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 739634a..6bd189a 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
@@ -290,7 +290,36 @@ public class TestURIBuilder {
     }
 
     @Test
-    public void testRemoveParameters() throws Exception {
+    public void testRemoveParameter() throws Exception {
+        final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff&blah&blah", null);
+        URIBuilder uribuilder = new URIBuilder(uri);
+        Assert.assertFalse(uribuilder.isQueryEmpty());
+
+        try {
+            uribuilder.removeParameter(null);
+            Assert.fail("NullPointerException expected");
+        } catch (final NullPointerException e) {
+        }
+
+        uribuilder.removeParameter("DoesNotExist");
+        Assert.assertEquals("stuff", uribuilder.getFirstQueryParam("param").getValue());
+        Assert.assertNull(uribuilder.getFirstQueryParam("blah").getValue());
+
+        uribuilder = uribuilder.removeParameter("blah");
+        Assert.assertEquals("stuff", uribuilder.getFirstQueryParam("param").getValue());
+        Assert.assertNull(uribuilder.getFirstQueryParam("blah"));
+
+        uribuilder = uribuilder.removeParameter("param");
+        Assert.assertNull(uribuilder.getFirstQueryParam("param"));
+        Assert.assertTrue(uribuilder.isQueryEmpty());
+
+        uribuilder = uribuilder.removeParameter("AlreadyEmpty");
+        Assert.assertTrue(uribuilder.isQueryEmpty());
+        Assert.assertEquals(new URI("http://localhost:80/"), uribuilder.build());
+    }
+
+    @Test
+    public void testRemoveQuery() throws Exception {
         final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
         final URIBuilder uribuilder = new URIBuilder(uri).removeQuery();
         final URI result = uribuilder.build();