diff --git a/net/src/main/java/org/xbib/net/URL.java b/net/src/main/java/org/xbib/net/URL.java index 869aed8..ea635eb 100755 --- a/net/src/main/java/org/xbib/net/URL.java +++ b/net/src/main/java/org/xbib/net/URL.java @@ -611,7 +611,7 @@ public class URL implements Comparable { 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 if (!isNullOrEmpty(builder.query)) { if (withQuestionMark) { @@ -627,10 +627,10 @@ public class URL implements Comparable { while (it.hasNext()) { Pair queryParam = it.next(); try { - String k = withEncoding ? queryParamEncoder.encode(queryParam.getKey()) : queryParam.getKey(); + String k = encoded ? queryParamEncoder.encode(queryParam.getKey()) : queryParam.getKey(); sb.append(k); 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(); sb.append(EQUAL_CHAR).append(v); } diff --git a/net/src/test/java/org/xbib/net/PercentEncoderTest.java b/net/src/test/java/org/xbib/net/PercentEncoderTest.java index c36dde9..d221f39 100644 --- a/net/src/test/java/org/xbib/net/PercentEncoderTest.java +++ b/net/src/test/java/org/xbib/net/PercentEncoderTest.java @@ -74,4 +74,10 @@ class PercentEncoderTest { void testUrlEncodedUtf16SurrogatePair() throws Exception { 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 ")); + } } diff --git a/net/src/test/java/org/xbib/net/URLBuilderTest.java b/net/src/test/java/org/xbib/net/URLBuilderTest.java index 34611eb..62c67d7 100644 --- a/net/src/test/java/org/xbib/net/URLBuilderTest.java +++ b/net/src/test/java/org/xbib/net/URLBuilderTest.java @@ -74,6 +74,21 @@ class URLBuilderTest { .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 void testQueryParamsDuplicated() { assertUrl(URL.http().resolveFromHost("foo.com") diff --git a/net/src/test/java/org/xbib/net/URLParserTest.java b/net/src/test/java/org/xbib/net/URLParserTest.java index 826857d..2c872c1 100644 --- a/net/src/test/java/org/xbib/net/URLParserTest.java +++ b/net/src/test/java/org/xbib/net/URLParserTest.java @@ -411,6 +411,20 @@ class URLParserTest { 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 void testAnotherPlus() throws Exception { URL url = URL.parser().parse("http://foo.com/has+plus;plusMtx=pl+us?plusQp=pl%2Bus#plus+frag");