add some URL query parameter tests

This commit is contained in:
Jörg Prante 2023-04-03 17:58:14 +02:00
parent ab86306ee7
commit ce6ee1f69a
4 changed files with 38 additions and 3 deletions

View file

@ -611,7 +611,7 @@ public class URL implements Comparable<URL> {
return sb.length() == 0 ? null : sb.toString(); return sb.length() == 0 ? null : sb.toString();
} }
private void appendQuery(StringBuilder sb, boolean withEncoding, boolean withQuestionMark) { private void appendQuery(StringBuilder sb, boolean encoded, boolean withQuestionMark) {
// a given query has priority // a given query has priority
if (!isNullOrEmpty(builder.query)) { if (!isNullOrEmpty(builder.query)) {
if (withQuestionMark) { if (withQuestionMark) {
@ -627,10 +627,10 @@ public class URL implements Comparable<URL> {
while (it.hasNext()) { while (it.hasNext()) {
Pair<String, Object> queryParam = it.next(); Pair<String, Object> queryParam = it.next();
try { try {
String k = withEncoding ? queryParamEncoder.encode(queryParam.getKey()) : queryParam.getKey(); String k = encoded ? queryParamEncoder.encode(queryParam.getKey()) : queryParam.getKey();
sb.append(k); sb.append(k);
if (queryParam.getValue() != null) { if (queryParam.getValue() != null) {
Object v = withEncoding && queryParam.getValue() instanceof CharSequence ? Object v = encoded && queryParam.getValue() instanceof CharSequence ?
queryParamEncoder.encode((CharSequence) queryParam.getValue()) : queryParam.getValue(); queryParamEncoder.encode((CharSequence) queryParam.getValue()) : queryParam.getValue();
sb.append(EQUAL_CHAR).append(v); sb.append(EQUAL_CHAR).append(v);
} }

View file

@ -74,4 +74,10 @@ class PercentEncoderTest {
void testUrlEncodedUtf16SurrogatePair() throws Exception { void testUrlEncodedUtf16SurrogatePair() throws Exception {
assertEquals("clef%D8%34%DD%1E", alnum16.encode("clef\ud834\udd1e")); assertEquals("clef%D8%34%DD%1E", alnum16.encode("clef\ud834\udd1e"));
} }
@Test
void testQueryParameterEncoding() throws Exception {
PercentEncoder queryParamEncoder = PercentEncoders.getQueryParamEncoder(StandardCharsets.UTF_8);
assertEquals("%20a%20%3D%20b%20", queryParamEncoder.encode(" a = b "));
}
} }

View file

@ -74,6 +74,21 @@ class URLBuilderTest {
.toUrlString(), "http://foo.com?foo=bar"); .toUrlString(), "http://foo.com?foo=bar");
} }
@Test
void testQueryParamEncodedWithSpaces() {
assertUrl(URL.http()
.resolveFromHost("foo.com")
.queryParam(" foo ", " bar ")
.build()
.toExternalForm(), "http://foo.com?%20foo%20=%20bar%20");
}
@Test
void testFromUrlQueryParamEncodedWithSpaces() {
assertUrl(URL.from("http://foo.com? foo = bar ")
.toExternalForm(), "http://foo.com?%20foo%20=%20bar%20");
}
@Test @Test
void testQueryParamsDuplicated() { void testQueryParamsDuplicated() {
assertUrl(URL.http().resolveFromHost("foo.com") assertUrl(URL.http().resolveFromHost("foo.com")

View file

@ -411,6 +411,20 @@ class URLParserTest {
assertEquals("foo=bar&=#baz&foo=bar?/2", url.getDecodedQuery()); assertEquals("foo=bar&=#baz&foo=bar?/2", url.getDecodedQuery());
} }
@Test
void testQuerySpaces() throws Exception {
URL url = URL.parser().parse("http://foo.com? foo = bar ");
assertEquals(" foo = bar ", url.getQuery());
assertEquals(" foo = bar ", url.getDecodedQuery());
}
@Test
void testQuerySpacesPercentEncoded() throws Exception {
URL url = URL.parser().parse("http://foo.com?%20foo%20=%20bar%20");
assertEquals("%20foo%20=%20bar%20", url.getQuery());
assertEquals(" foo = bar ", url.getDecodedQuery());
}
@Test @Test
void testAnotherPlus() throws Exception { void testAnotherPlus() throws Exception {
URL url = URL.parser().parse("http://foo.com/has+plus;plusMtx=pl+us?plusQp=pl%2Bus#plus+frag"); URL url = URL.parser().parse("http://foo.com/has+plus;plusMtx=pl+us?plusQp=pl%2Bus#plus+frag");