Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thugrock7 committed Aug 5, 2024
1 parent 448c117 commit 8dabf28
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
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);
}
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

0 comments on commit 8dabf28

Please sign in to comment.