Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ protected String withQueryWithParams(final String query, final List<Param> query
@Override
protected String withQueryWithoutParams(final String query) {
// encode query
StringBuilder sb = StringBuilderPool.DEFAULT.stringBuilder();
encodeAndAppendQuery(sb, query);
return sb.toString();
return Utf8UrlEncoder.encodeQuery(query);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public static String encodePath(String input) {
return sb == null ? input : sb.toString();
}

// package-private: only UriEncoder (same package) needs this; keep API surface minimal
static String encodeQuery(String input) {
Comment thread
hyperxpro marked this conversation as resolved.
StringBuilder sb = lazyAppendEncoded(null, input, BUILT_QUERY_UNTOUCHED_CHARS, false);
return sb == null ? input : sb.toString();
}

public static StringBuilder encodeAndAppendQuery(StringBuilder sb, String query) {
return appendEncoded(sb, query, BUILT_QUERY_UNTOUCHED_CHARS, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,39 @@
*/
package org.asynchttpclient.util;

import io.github.artsok.RepeatedIfExceptionsTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;

public class Utf8UrlEncoderTest {

@RepeatedIfExceptionsTest(repeats = 5)
@Test
public void testBasics() {
assertEquals("foobar", Utf8UrlEncoder.encodeQueryElement("foobar"));
assertEquals("a%26b", Utf8UrlEncoder.encodeQueryElement("a&b"));
assertEquals("a%2Bb", Utf8UrlEncoder.encodeQueryElement("a+b"));
}

@RepeatedIfExceptionsTest(repeats = 5)
@Test
public void encodeQueryReusesInputWhenNothingNeedsEscaping() {
String query = "a=1&b=/two?c%20d";

assertSame(query, Utf8UrlEncoder.encodeQuery(query));
}

@Test
public void encodeQueryEscapesWhenNeeded() {
String query = "a=one two";
String encoded = Utf8UrlEncoder.encodeQuery(query);

assertNotSame(query, encoded);
assertEquals("a=one%20two", encoded);
Comment thread
hyperxpro marked this conversation as resolved.
assertEquals("a=%C3%A9", Utf8UrlEncoder.encodeQuery("a=\u00e9"));
}

@Test
public void testPercentageEncoding() {
assertEquals("foobar", Utf8UrlEncoder.percentEncodeQueryElement("foobar"));
assertEquals("foo%2Abar", Utf8UrlEncoder.percentEncodeQueryElement("foo*bar"));
Expand Down
Loading