diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 0f79d0d9f..4f59dd37a 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -11,6 +11,10 @@ Changelog: * [HTTPCLIENT-1886] Update HttpClient 4.5.x from HttpCore 4.4.7 to 4.4.8 Contributed by Gary Gregory +* [HTTPCLIENT-1889] org.apache.http.client.utils.URLEncodedUtils.parse() + should return a new ArrayList when there are no query parameters. + Contributed by Gary Gregory + Release 4.5.4 ------------------- diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java index 8547cf0b4..02b9735b3 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java @@ -24,7 +24,7 @@ * . * */ - + package org.apache.http.client.utils; import java.io.IOException; @@ -37,7 +37,6 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.BitSet; -import java.util.Collections; import java.util.List; import java.util.Scanner; @@ -100,7 +99,7 @@ public static List parse(final URI uri, final Charset charset) { if (query != null && !query.isEmpty()) { return parse(query, charset); } - return Collections.emptyList(); + return createEmptyList(); } /** @@ -120,14 +119,14 @@ public static List parse( Args.notNull(entity, "HTTP entity"); final ContentType contentType = ContentType.get(entity); if (contentType == null || !contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) { - return Collections.emptyList(); + return createEmptyList(); } final long len = entity.getContentLength(); Args.check(len <= Integer.MAX_VALUE, "HTTP entity is too large"); final Charset charset = contentType.getCharset() != null ? contentType.getCharset() : HTTP.DEF_CONTENT_CHARSET; final InputStream instream = entity.getContent(); if (instream == null) { - return Collections.emptyList(); + return createEmptyList(); } final CharArrayBuffer buf; try { @@ -143,7 +142,7 @@ public static List parse( instream.close(); } if (buf.length() == 0) { - return Collections.emptyList(); + return createEmptyList(); } return parse(buf, charset, QP_SEP_A); } @@ -243,7 +242,7 @@ public static void parse( */ public static List parse(final String s, final Charset charset) { if (s == null) { - return Collections.emptyList(); + return createEmptyList(); } final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); @@ -266,7 +265,7 @@ public static List parse(final String s, final Charset charset) { */ public static List parse(final String s, final Charset charset, final char... separators) { if (s == null) { - return Collections.emptyList(); + return createEmptyList(); } final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); @@ -520,6 +519,10 @@ public static String format( private static final int RADIX = 16; + private static List createEmptyList() { + return new ArrayList(0); + } + private static String urlEncode( final String content, final Charset charset, diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java index b0111ca21..0041f0f00 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURLEncodedUtils.java @@ -169,6 +169,14 @@ public void testParseInvalidURLCodedContent() throws Exception { assertNameValuePair(result.get(0), "name", "%wa "); } + @Test + public void testEmptyQuery() throws Exception { + final List result = URLEncodedUtils.parse("", Consts.UTF_8); + Assert.assertEquals(0, result.size()); + // [HTTPCLIENT-1889]: + result.add(new BasicNameValuePair("key", "value")); + } + @Test public void testParseEntity() throws Exception { final StringEntity entity = new StringEntity("Name1=Value1");