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/09 12:56:25 UTC
[httpcomponents-core] branch master updated: Allow setting
parameters to null arrays and lists to behave like empty (#300)
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 42350ee Allow setting parameters to null arrays and lists to behave like empty (#300)
42350ee is described below
commit 42350eed591d195df858116161e0f1114716e46d
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Sat Oct 9 08:56:20 2021 -0400
Allow setting parameters to null arrays and lists to behave like empty (#300)
arrays and lists instead of throwing exceptions.
Co-authored-by: Gary Gregory <gg...@rocketsoftware.com>
---
.../java/org/apache/hc/core5/net/URIBuilder.java | 24 +++++++++++------
.../org/apache/hc/core5/net/TestURIBuilder.java | 30 +++++++++++++++++++---
2 files changed, 42 insertions(+), 12 deletions(-)
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 e059fda..c1322a8 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
@@ -632,13 +632,15 @@ public class URIBuilder {
*
* @return this.
*/
- public URIBuilder setParameters(final List <NameValuePair> nvps) {
+ public URIBuilder setParameters(final List <NameValuePair> nameValuePairs) {
if (this.queryParams == null) {
this.queryParams = new ArrayList<>();
} else {
this.queryParams.clear();
}
- this.queryParams.addAll(nvps);
+ if (nameValuePairs != null) {
+ this.queryParams.addAll(nameValuePairs);
+ }
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
@@ -655,11 +657,13 @@ public class URIBuilder {
*
* @return this.
*/
- public URIBuilder addParameters(final List <NameValuePair> nvps) {
+ public URIBuilder addParameters(final List<NameValuePair> nameValuePairs) {
if (this.queryParams == null) {
this.queryParams = new ArrayList<>();
}
- this.queryParams.addAll(nvps);
+ if (nameValuePairs != null) {
+ this.queryParams.addAll(nameValuePairs);
+ }
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
@@ -676,13 +680,15 @@ public class URIBuilder {
*
* @return this.
*/
- public URIBuilder setParameters(final NameValuePair... nvps) {
+ public URIBuilder setParameters(final NameValuePair... nameValuePairs) {
if (this.queryParams == null) {
this.queryParams = new ArrayList<>();
} else {
this.queryParams.clear();
}
- Collections.addAll(this.queryParams, nvps);
+ if (nameValuePairs != null) {
+ Collections.addAll(this.queryParams, nameValuePairs);
+ }
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
@@ -714,11 +720,13 @@ public class URIBuilder {
* @return this.
* @since 5.2
*/
- public URIBuilder addParameter(final NameValuePair nvp) {
+ public URIBuilder addParameter(final NameValuePair nameValuePair) {
if (this.queryParams == null) {
this.queryParams = new ArrayList<>();
}
- this.queryParams.add(nvp);
+ if (nameValuePair != null) {
+ this.queryParams.add(nameValuePair);
+ }
this.encodedQuery = null;
this.encodedSchemeSpecificPart = null;
this.query = null;
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 80466ad..4c331b6 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
@@ -346,7 +346,7 @@ public class TestURIBuilder {
}
@Test
- public void testSetParametersWithEmptyArg() throws Exception {
+ public void testSetParametersWithEmptyArrayArg() throws Exception {
final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
final URIBuilder uribuilder = new URIBuilder(uri).setParameters();
final URI result = uribuilder.build();
@@ -354,6 +354,14 @@ public class TestURIBuilder {
}
@Test
+ public void testSetParametersWithNullArrayArg() throws Exception {
+ final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
+ final URIBuilder uribuilder = new URIBuilder(uri).setParameters((NameValuePair[]) null);
+ final URI result = uribuilder.build();
+ Assert.assertEquals(new URI("http://localhost:80/test"), result);
+ }
+
+ @Test
public void testSetParametersWithEmptyList() throws Exception {
final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
final URIBuilder uribuilder = new URIBuilder(uri).setParameters(Collections.emptyList());
@@ -362,6 +370,14 @@ public class TestURIBuilder {
}
@Test
+ public void testSetParametersWithNullList() throws Exception {
+ final URI uri = new URI("http", null, "localhost", 80, "/test", "param=test", null);
+ final URIBuilder uribuilder = new URIBuilder(uri).setParameters((List<NameValuePair>) null);
+ final URI result = uribuilder.build();
+ Assert.assertEquals(new URI("http://localhost:80/test"), result);
+ }
+
+ @Test
public void testParameterWithSpecialChar() throws Exception {
final URI uri = new URI("http", null, "localhost", 80, "/", "param=stuff", null);
final URIBuilder uribuilder = new URIBuilder(uri).addParameter("param", "1 + 1 = 2")
@@ -454,9 +470,15 @@ public class TestURIBuilder {
public void assertAddParameters(final Charset charset) throws Exception {
final URI uri = new URIBuilder("https://somehost.com/stuff")
.setCharset(charset)
- .addParameters(createParameters()).build();
+ .addParameters(createParameterList()).build();
assertBuild(charset, uri);
+ // null addParameters
+ final URI uri2 = new URIBuilder("https://somehost.com/stuff")
+ .setCharset(charset)
+ .addParameters(null).build();
+
+ Assert.assertEquals("https://somehost.com/stuff", uri2.toString());
}
@Test
@@ -472,7 +494,7 @@ public class TestURIBuilder {
public void assertSetParameters(final Charset charset) throws Exception {
final URI uri = new URIBuilder("https://somehost.com/stuff")
.setCharset(charset)
- .setParameters(createParameters()).build();
+ .setParameters(createParameterList()).build();
assertBuild(charset, uri);
}
@@ -486,7 +508,7 @@ public class TestURIBuilder {
Assert.assertEquals(uriExpected, uri.toString());
}
- private List<NameValuePair> createParameters() {
+ private List<NameValuePair> createParameterList() {
final List<NameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("parameter1", "value1"));
parameters.add(new BasicNameValuePair("parameter2", "\"1\u00aa position\""));