Skip to content

Commit 8dabf28

Browse files
committed
adding tests
1 parent 448c117 commit 8dabf28

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

instrumentation/okhttp/okhttp-3.0/src/test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/okhttp/v3_0/OkHttpTracingInterceptorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public Response doGetRequest(String uri, Map<String, String> headersMap) throws
6767

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

70-
String responseBody =
71-
(response.body() != null && response.body().contentLength() > 0)
72-
? response.body().string()
73-
: null;
74-
return new Response(responseBody, response.code());
70+
if (response.body() != null) {
71+
String responseBody = response.body().string();
72+
return new Response(!responseBody.isEmpty() ? responseBody : null, response.code());
73+
}
74+
return new Response(null, response.code());
7575
}
7676
}

testing-common/src/main/java/org/hypertrace/agent/testing/AbstractHttpClientTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,66 @@ public void getJson()
308308
}
309309
}
310310

311+
@Test
312+
public void getGzipResponse()
313+
throws IOException, TimeoutException, InterruptedException, ExecutionException {
314+
315+
Response response = doGetRequest("http://httpbin.org/gzip", headers);
316+
317+
Assertions.assertEquals(200, response.statusCode);
318+
319+
// Verify that the response contains the expected structure received from httpbin server gzip
320+
// response
321+
Assertions.assertTrue(response.body.contains("\"gzipped\": true"));
322+
Assertions.assertTrue(response.body.contains("\"method\": \"GET\""));
323+
Assertions.assertTrue(response.body.contains("\"Host\": \"httpbin.org\""));
324+
325+
TEST_WRITER.waitForTraces(1);
326+
List<List<Span>> traces;
327+
if (hasResponseBodySpan) {
328+
traces =
329+
TEST_WRITER.waitForSpans(
330+
2, span -> span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER));
331+
} else {
332+
traces =
333+
TEST_WRITER.waitForSpans(
334+
1,
335+
span ->
336+
!span.getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)
337+
|| span.getAttributesList().stream()
338+
.noneMatch(
339+
keyValue ->
340+
keyValue.getKey().equals("http.url")
341+
&& keyValue.getValue().getStringValue().contains("/gzip")));
342+
}
343+
344+
Assertions.assertEquals(1, traces.size());
345+
Span clientSpan = traces.get(0).get(0);
346+
if (hasResponseBodySpan) {
347+
Assertions.assertEquals(2, traces.get(0).size());
348+
Span responseBodySpan = traces.get(0).get(1);
349+
if (traces.get(0).get(1).getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)) {
350+
responseBodySpan = traces.get(0).get(0);
351+
}
352+
Assertions.assertNull(TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body"));
353+
354+
String respBodyCapturedInSpan =
355+
TEST_WRITER.getAttributesMap(responseBodySpan).get("http.response.body").getStringValue();
356+
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"gzipped\": true"));
357+
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"method\": \"GET\""));
358+
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"Host\": \"httpbin.org\""));
359+
} else {
360+
Assertions.assertNull(TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body"));
361+
362+
Assertions.assertEquals(1, traces.get(0).size());
363+
String respBodyCapturedInSpan =
364+
TEST_WRITER.getAttributesMap(clientSpan).get("http.response.body").getStringValue();
365+
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"gzipped\": true"));
366+
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"method\": \"GET\""));
367+
Assertions.assertTrue(respBodyCapturedInSpan.contains("\"Host\": \"httpbin.org\""));
368+
}
369+
}
370+
311371
private void assertHeaders(Span span) {
312372
Assertions.assertEquals(
313373
TestHttpServer.RESPONSE_HEADER_VALUE,

0 commit comments

Comments
 (0)