[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
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
-------------------

View File

@ -24,7 +24,7 @@
* <http://www.apache.org/>.
*
*/
package org.apache.http.client.utils;
import java.io.IOException;
@ -37,7 +37,6 @@ import java.nio.CharBuffer;
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 class URLEncodedUtils {
if (query != null && !query.isEmpty()) {
return parse(query, charset);
}
return Collections.emptyList();
return createEmptyList();
}
/**
@ -120,14 +119,14 @@ public class URLEncodedUtils {
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 class URLEncodedUtils {
instream.close();
}
if (buf.length() == 0) {
return Collections.emptyList();
return createEmptyList();
}
return parse(buf, charset, QP_SEP_A);
}
@ -243,7 +242,7 @@ public class URLEncodedUtils {
*/
public static List<NameValuePair> 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 class URLEncodedUtils {
*/
public static List<NameValuePair> 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 class URLEncodedUtils {
private static final int RADIX = 16;
private static List<NameValuePair> createEmptyList() {
return new ArrayList<NameValuePair>(0);
}
private static String urlEncode(
final String content,
final Charset charset,

View File

@ -169,6 +169,14 @@ public class TestURLEncodedUtils {
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
public void testParseEntity() throws Exception {
final StringEntity entity = new StringEntity("Name1=Value1");