Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OkHTTP3 - fix response charset and adding support for gzip compression #405

Merged
merged 11 commits into from
Sep 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public Response doGetRequest(String uri, Map<String, String> headersMap) throws

okhttp3.Response response = client.newCall(request).execute();

String responseBody =
(response.body() != null && response.body().contentLength() > 0)
? response.body().string()
: null;
return new Response(responseBody, response.code());
if (response.body() != null) {
String responseBody = response.body().string();
return new Response(!responseBody.isEmpty() ? responseBody : null, response.code());
}
return new Response(null, response.code());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,66 @@ public void getJson()
}
}

@Test
public void getGzipResponse()
throws IOException, TimeoutException, InterruptedException, ExecutionException {

Response response = doGetRequest("http://httpbin.org/gzip", headers);

Assertions.assertEquals(200, response.statusCode);

// Verify that the response contains the expected structure received from httpbin server gzip
// response
Assertions.assertTrue(response.body.contains("\"gzipped\": true"));
Assertions.assertTrue(response.body.contains("\"method\": \"GET\""));
Assertions.assertTrue(response.body.contains("\"Host\": \"httpbin.org\""));

TEST_WRITER.waitForTraces(1);
List<List<Span>> traces;
if (hasResponseBodySpan) {
traces =
TEST_WRITER.waitForSpans(
2, span -> span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER));
} else {
traces =
TEST_WRITER.waitForSpans(
1,
span ->
!span.getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)
|| span.getAttributesList().stream()
.noneMatch(
keyValue ->
keyValue.getKey().equals("http.url")
&& keyValue.getValue().getStringValue().contains("/gzip")));
}

Assertions.assertEquals(1, traces.size());
Span clientSpan = traces.get(0).get(0);
if (hasResponseBodySpan) {
Assertions.assertEquals(2, traces.get(0).size());
Span responseBodySpan = traces.get(0).get(1);
if (traces.get(0).get(1).getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)) {
responseBodySpan = traces.get(0).get(0);
thugrock7 marked this conversation as resolved.
Show resolved Hide resolved
}
Assertions.assertNull(TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body"));

String respBodyCapturedInSpan =
TEST_WRITER.getAttributesMap(responseBodySpan).get("http.response.body").getStringValue();
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"gzipped\": true"));
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"method\": \"GET\""));
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"Host\": \"httpbin.org\""));
} else {
Assertions.assertNull(TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body"));

Assertions.assertEquals(1, traces.get(0).size());
String respBodyCapturedInSpan =
TEST_WRITER.getAttributesMap(clientSpan).get("http.response.body").getStringValue();
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"gzipped\": true"));
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"method\": \"GET\""));
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"Host\": \"httpbin.org\""));
}
}

private void assertHeaders(Span span) {
Assertions.assertEquals(
TestHttpServer.RESPONSE_HEADER_VALUE,
Expand Down
Loading