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 2026/09/12 10:39:19 UTC
(httpcomponents-core) branch 5.4.x updated: Fix URIBuilder encoding policy documentation and null reset
This is an automated email from the ASF dual-hosted git repository.
ok2c pushed a commit to branch 5.4.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/5.4.x by this push:
new e871fa077 Fix URIBuilder encoding policy documentation and null reset
e871fa077 is described below
commit e871fa077ff112f3f26fdd881b02ac5ac9f1b07e
Author: Praful Gupta <pr...@gmail.com>
AuthorDate: Sat Sep 12 00:30:58 2026 +0100
Fix URIBuilder encoding policy documentation and null reset
---
.../java/org/apache/hc/core5/net/URIBuilder.java | 4 +--
.../org/apache/hc/core5/net/TestURIBuilder.java | 33 ++++++++++++++++++++++
2 files changed, 35 insertions(+), 2 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 cbc41993c..c7fd7f249 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
@@ -207,7 +207,7 @@ public URIBuilder setCharset(final Charset charset) {
* Sets the encoding policy for this {@link URIBuilder}.
* The encoding policy determines how URI components (e.g., query, fragment) are
* percent-encoded when building the URI string. If not set, the default policy
- * is {@link EncodingPolicy#RFC_3986}.
+ * is {@link EncodingPolicy#ALL_RESERVED}.
*
* @param encodingPolicy the encoding policy to apply, or {@code null} to reset
* to the default ({@link EncodingPolicy#ALL_RESERVED})
@@ -215,7 +215,7 @@ public URIBuilder setCharset(final Charset charset) {
* @since 5.4
*/
public URIBuilder setEncodingPolicy(final EncodingPolicy encodingPolicy) {
- this.encodingPolicy = encodingPolicy;
+ this.encodingPolicy = encodingPolicy != null ? encodingPolicy : EncodingPolicy.ALL_RESERVED;
return this;
}
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 74e7a6016..765a535e4 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
@@ -998,6 +998,39 @@ void testSetPlusAsBlank() throws Exception {
Assertions.assertEquals("hello world", params.get(0).getValue());
}
+ @Test
+ void testDefaultEncodingPolicy() throws Exception {
+ final URIBuilder builder = new URIBuilder()
+ .setScheme("http")
+ .setHost("example.com")
+ .setPath("/a:b")
+ .addParameter("key", "a/b")
+ .setFragment("a/b");
+ final URI expected = new URI("http://example.com/a%3Ab?key=a%2Fb#a%2Fb");
+
+ Assertions.assertEquals(expected, builder.build());
+ builder.setEncodingPolicy(URIBuilder.EncodingPolicy.ALL_RESERVED);
+ Assertions.assertEquals(expected, builder.build());
+ builder.setEncodingPolicy(null);
+ Assertions.assertEquals(expected, builder.build());
+ }
+
+ @Test
+ void testResetEncodingPolicy() throws Exception {
+ final URIBuilder builder = new URIBuilder()
+ .setScheme("http")
+ .setUserInfo("user!")
+ .setHost("example.com")
+ .setPath("/a:b")
+ .setCustomQuery("a/b")
+ .setFragment("a/b")
+ .setEncodingPolicy(URIBuilder.EncodingPolicy.RFC_3986);
+
+ Assertions.assertEquals(new URI("http://user!@example.com/a:b?a/b#a/b"), builder.build());
+ Assertions.assertSame(builder, builder.setEncodingPolicy(null));
+ Assertions.assertEquals(new URI("http://user%21@example.com/a%3Ab?a%2Fb#a%2Fb"), builder.build());
+ }
+
@Test
void testCustomQueryEncoding() throws Exception {
final String query = "query param:!@/?\"";