[HTTPCLIENT-1889] org.apache.http.client.utils.URLEncodedUtils.parse()

should return a new ArrayList when there are no query parameters.
This commit is contained in:
Gary Gregory 2017-12-15 14:15:49 -07:00
parent 817d57a208
commit 1780ab2bf3
3 changed files with 23 additions and 8 deletions

View File

@ -11,6 +11,10 @@ Changelog:
* [HTTPCLIENT-1886] Update HttpClient 4.5.x from HttpCore 4.4.7 to 4.4.8 * [HTTPCLIENT-1886] Update HttpClient 4.5.x from HttpCore 4.4.7 to 4.4.8
Contributed by Gary Gregory <ggregory at apache.org> Contributed by Gary Gregory <ggregory at apache.org>
* [HTTPCLIENT-1889] org.apache.http.client.utils.URLEncodedUtils.parse()
should return a new ArrayList when there are no query parameters.
Contributed by Gary Gregory <ggregory at apache.org>
Release 4.5.4 Release 4.5.4
------------------- -------------------

View File

@ -37,7 +37,6 @@
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
@ -100,7 +99,7 @@ public static List <NameValuePair> parse(final URI uri, final Charset charset) {
if (query != null && !query.isEmpty()) { if (query != null && !query.isEmpty()) {
return parse(query, charset); return parse(query, charset);
} }
return Collections.emptyList(); return createEmptyList();
} }
/** /**
@ -120,14 +119,14 @@ public static List <NameValuePair> parse(
Args.notNull(entity, "HTTP entity"); Args.notNull(entity, "HTTP entity");
final ContentType contentType = ContentType.get(entity); final ContentType contentType = ContentType.get(entity);
if (contentType == null || !contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) { if (contentType == null || !contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
return Collections.emptyList(); return createEmptyList();
} }
final long len = entity.getContentLength(); final long len = entity.getContentLength();
Args.check(len <= Integer.MAX_VALUE, "HTTP entity is too large"); Args.check(len <= Integer.MAX_VALUE, "HTTP entity is too large");
final Charset charset = contentType.getCharset() != null ? contentType.getCharset() : HTTP.DEF_CONTENT_CHARSET; final Charset charset = contentType.getCharset() != null ? contentType.getCharset() : HTTP.DEF_CONTENT_CHARSET;
final InputStream instream = entity.getContent(); final InputStream instream = entity.getContent();
if (instream == null) { if (instream == null) {
return Collections.emptyList(); return createEmptyList();
} }
final CharArrayBuffer buf; final CharArrayBuffer buf;
try { try {
@ -143,7 +142,7 @@ public static List <NameValuePair> parse(
instream.close(); instream.close();
} }
if (buf.length() == 0) { if (buf.length() == 0) {
return Collections.emptyList(); return createEmptyList();
} }
return parse(buf, charset, QP_SEP_A); return parse(buf, charset, QP_SEP_A);
} }
@ -243,7 +242,7 @@ public static void parse(
*/ */
public static List<NameValuePair> parse(final String s, final Charset charset) { public static List<NameValuePair> parse(final String s, final Charset charset) {
if (s == null) { if (s == null) {
return Collections.emptyList(); return createEmptyList();
} }
final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
buffer.append(s); buffer.append(s);
@ -266,7 +265,7 @@ public static List<NameValuePair> parse(final String s, final Charset charset) {
*/ */
public static List<NameValuePair> parse(final String s, final Charset charset, final char... separators) { public static List<NameValuePair> parse(final String s, final Charset charset, final char... separators) {
if (s == null) { if (s == null) {
return Collections.emptyList(); return createEmptyList();
} }
final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
buffer.append(s); buffer.append(s);
@ -520,6 +519,10 @@ public static String format(
private static final int RADIX = 16; private static final int RADIX = 16;
private static List<NameValuePair> createEmptyList() {
return new ArrayList<NameValuePair>(0);
}
private static String urlEncode( private static String urlEncode(
final String content, final String content,
final Charset charset, final Charset charset,

View File

@ -169,6 +169,14 @@ public void testParseInvalidURLCodedContent() throws Exception {
assertNameValuePair(result.get(0), "name", "%wa "); assertNameValuePair(result.get(0), "name", "%wa ");
} }
@Test
public void testEmptyQuery() throws Exception {
final List<NameValuePair> result = URLEncodedUtils.parse("", Consts.UTF_8);
Assert.assertEquals(0, result.size());
// [HTTPCLIENT-1889]:
result.add(new BasicNameValuePair("key", "value"));
}
@Test @Test
public void testParseEntity() throws Exception { public void testParseEntity() throws Exception {
final StringEntity entity = new StringEntity("Name1=Value1"); final StringEntity entity = new StringEntity("Name1=Value1");