KEYCLOAK-12002. SimpleHttp: considering encoding

This commit is contained in:
pastor 2019-11-17 18:37:30 +03:00 committed by Stian Thorgersen
parent ea268af511
commit 286d4778d0
2 changed files with 78 additions and 5 deletions

View file

@ -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;
}

View file

@ -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 <a href="mailto:viruszold@gmail.com">Andrey Khlebnikov</a>
* @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<Object[]> 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;
}
}
}