diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/QueryStringEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/QueryStringEncoder.java index 89d2deb988..18bd7b5fc6 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/QueryStringEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/QueryStringEncoder.java @@ -108,7 +108,7 @@ public class QueryStringEncoder { private static String encodeComponent(String s, Charset charset) { // TODO: Optimize me. try { - return URLEncoder.encode(s, PATTERN.matcher(charset.name()).replaceAll("%20")); + return PATTERN.matcher(URLEncoder.encode(s, charset.name())).replaceAll("%20"); } catch (UnsupportedEncodingException ignored) { throw new UnsupportedCharsetException(charset.name()); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/QueryStringEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/QueryStringEncoderTest.java index b9f97a325a..a9f6f90155 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/QueryStringEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/QueryStringEncoderTest.java @@ -59,4 +59,12 @@ public class QueryStringEncoderTest { Assert.assertEquals("/foo/\u00A5?a=%FE%FF%00%A5", e.toString()); Assert.assertEquals(new URI("/foo/\u00A5?a=%FE%FF%00%A5"), e.toUri()); } + + @Test + public void testWhitespaceEncoding() throws Exception { + QueryStringEncoder e = new QueryStringEncoder("/foo"); + e.addParam("a", "b c"); + Assert.assertEquals("/foo?a=b%20c", e.toString()); + Assert.assertEquals(new URI("/foo?a=b%20c"), e.toUri()); + } }