|
9 | 9 | import com.sinch.sdk.core.http.HttpRequest; |
10 | 10 | import com.sinch.sdk.core.http.HttpResponse; |
11 | 11 | import com.sinch.sdk.core.models.ServerConfiguration; |
| 12 | +import java.io.ByteArrayInputStream; |
12 | 13 | import java.lang.reflect.Field; |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.nio.charset.StandardCharsets; |
13 | 16 | import java.util.*; |
| 17 | +import java.util.Scanner; |
14 | 18 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 19 | +import org.apache.hc.core5.http.ClassicHttpResponse; |
| 20 | +import org.apache.hc.core5.http.Header; |
| 21 | +import org.apache.hc.core5.http.HttpEntity; |
15 | 22 | import org.junit.jupiter.api.BeforeEach; |
16 | 23 | import org.junit.jupiter.api.Test; |
17 | 24 |
|
@@ -75,4 +82,32 @@ void testInvokeApiHandles401WithEmptyHeaders() throws Exception { |
75 | 82 | // AND: resetToken() is not called (no headers to check) |
76 | 83 | verify(mockAuthManager, never()).resetToken(); |
77 | 84 | } |
| 85 | + |
| 86 | + @Test |
| 87 | + void processResponseDecodesBodyAsUtf8() throws Exception { |
| 88 | + String nonAscii = "Ünïcödé resülts"; |
| 89 | + byte[] utf8Bytes = nonAscii.getBytes(StandardCharsets.UTF_8); |
| 90 | + |
| 91 | + HttpEntity entity = mock(HttpEntity.class); |
| 92 | + when(entity.getContent()).thenReturn(new ByteArrayInputStream(utf8Bytes)); |
| 93 | + |
| 94 | + ClassicHttpResponse httpResponse = mock(ClassicHttpResponse.class); |
| 95 | + when(httpResponse.getCode()).thenReturn(200); |
| 96 | + when(httpResponse.getReasonPhrase()).thenReturn("OK"); |
| 97 | + when(httpResponse.getHeaders()).thenReturn(new Header[0]); |
| 98 | + when(httpResponse.getEntity()).thenReturn(entity); |
| 99 | + |
| 100 | + Method processResponse = |
| 101 | + HttpClientApache.class.getDeclaredMethod("processResponse", ClassicHttpResponse.class); |
| 102 | + processResponse.setAccessible(true); |
| 103 | + |
| 104 | + HttpResponse result = (HttpResponse) processResponse.invoke(null, httpResponse); |
| 105 | + |
| 106 | + assertNotNull(result.getContent()); |
| 107 | + try (Scanner s = |
| 108 | + new Scanner(result.getContent(), StandardCharsets.UTF_8.name()).useDelimiter("\\A")) { |
| 109 | + String decoded = s.hasNext() ? s.next() : ""; |
| 110 | + assertEquals(nonAscii, decoded, "Response body must round-trip through UTF-8 correctly"); |
| 111 | + } |
| 112 | + } |
78 | 113 | } |
0 commit comments