From 286d4778d0977cc81269aaae2db8e0b2c8a6d9a0 Mon Sep 17 00:00:00 2001 From: pastor Date: Sun, 17 Nov 2019 18:37:30 +0300 Subject: [PATCH] KEYCLOAK-12002. SimpleHttp: considering encoding --- .../broker/provider/util/SimpleHttp.java | 15 ++-- .../broker/provider/util/SimpleHttpTest.java | 68 +++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 server-spi-private/src/test/java/org/keycloak/broker/provider/util/SimpleHttpTest.java diff --git a/server-spi-private/src/main/java/org/keycloak/broker/provider/util/SimpleHttp.java b/server-spi-private/src/main/java/org/keycloak/broker/provider/util/SimpleHttp.java index 771567ca06..cfdf51457a 100755 --- a/server-spi-private/src/main/java/org/keycloak/broker/provider/util/SimpleHttp.java +++ b/server-spi-private/src/main/java/org/keycloak/broker/provider/util/SimpleHttp.java @@ -34,6 +34,7 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; import org.keycloak.common.util.Base64; @@ -47,6 +48,7 @@ import java.io.InputStreamReader; import java.io.StringWriter; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -185,7 +187,7 @@ public class SimpleHttp { boolean delete = method.equals("DELETE"); HttpRequestBase httpRequest = new HttpPost(url); - + if (get) { httpRequest = new HttpGet(appendParameterToUrl(url)); } @@ -273,6 +275,8 @@ public class SimpleHttp { HttpEntity entity = response.getEntity(); if (entity != null) { is = entity.getContent(); + ContentType contentType = ContentType.getOrDefault(entity); + Charset charset = contentType.getCharset(); try { HeaderIterator it = response.headerIterator(); while (it.hasNext()) { @@ -282,7 +286,8 @@ public class SimpleHttp { } } - InputStreamReader reader = new InputStreamReader(is); + InputStreamReader reader = charset == null ? new InputStreamReader(is) : + new InputStreamReader(is, charset); StringWriter writer = new StringWriter(); @@ -326,11 +331,11 @@ public class SimpleHttp { public String getFirstHeader(String name) throws IOException { readResponse(); Header[] headers = response.getHeaders(name); - + if (headers != null && headers.length > 0) { - return headers[0].getValue(); + return headers[0].getValue(); } - + return null; } diff --git a/server-spi-private/src/test/java/org/keycloak/broker/provider/util/SimpleHttpTest.java b/server-spi-private/src/test/java/org/keycloak/broker/provider/util/SimpleHttpTest.java new file mode 100644 index 0000000000..e6311fb3a8 --- /dev/null +++ b/server-spi-private/src/test/java/org/keycloak/broker/provider/util/SimpleHttpTest.java @@ -0,0 +1,68 @@ +package org.keycloak.broker.provider.util; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpVersion; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicHttpResponse; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * @author Andrey Khlebnikov + * @version $Revision: 1 $ + */ +public final class SimpleHttpTest { + + @RunWith(Parameterized.class) + public static final class ResponseConsideringCharsetTest { + private final StringEntity entity; + private final String original; + private final boolean success; + + public ResponseConsideringCharsetTest(String original, Charset charset, boolean success) { + this.original = original; + this.success = success; + this.entity = new StringEntity(original, ContentType.create("text/plain", charset)); + } + + @Parameters(name = "{index}: withCharset({0}, {1}) = {2}") + public static Collection entities() { + return Arrays.asList(new Object[][]{ + {"English", StandardCharsets.UTF_8, true}, + {"Русский", StandardCharsets.UTF_8, true}, + {"Русский", StandardCharsets.ISO_8859_1, false}, + {"Русский", null, false}, + }); + } + + @Test + public void withCharset() throws IOException { + HttpResponse httpResponse = createBasicResponse(entity); + SimpleHttp.Response response = new SimpleHttp.Response(httpResponse); + if (success) { + assertEquals(original, response.asString()); + } else { + assertNotEquals(original, response.asString()); + } + } + + private HttpResponse createBasicResponse(HttpEntity entity) { + BasicHttpResponse response = new BasicHttpResponse(new HttpVersion(1, 1), 200, "OK"); + response.setEntity(entity); + return response; + } + } +} \ No newline at end of file