Skip to content

Commit e092f55

Browse files
committed
fixing apacheasync http client test
1 parent 94e7225 commit e092f55

File tree

2 files changed

+127
-47
lines changed

2 files changed

+127
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.instrumentation.hypertrace.apachehttpasyncclient;
18+
19+
import io.opentelemetry.proto.trace.v1.Span;
20+
import java.io.BufferedReader;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.InputStreamReader;
24+
import java.nio.charset.StandardCharsets;
25+
import java.util.List;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.Future;
28+
import java.util.concurrent.TimeoutException;
29+
import java.util.zip.GZIPInputStream;
30+
import org.apache.http.HttpResponse;
31+
import org.apache.http.client.methods.HttpGet;
32+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
33+
import org.apache.http.impl.nio.client.HttpAsyncClients;
34+
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
35+
import org.hypertrace.agent.testing.TestHttpServer;
36+
import org.junit.jupiter.api.AfterAll;
37+
import org.junit.jupiter.api.Assertions;
38+
import org.junit.jupiter.api.BeforeAll;
39+
import org.junit.jupiter.api.Test;
40+
41+
class ApacheAsyncClientGzipHandlingTest extends AbstractInstrumenterTest {
42+
43+
private static final TestHttpServer testHttpServer = new TestHttpServer();
44+
45+
private static final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
46+
47+
@BeforeAll
48+
public static void startServer() throws Exception {
49+
testHttpServer.start();
50+
client.start();
51+
}
52+
53+
@AfterAll
54+
public static void closeServer() throws Exception {
55+
testHttpServer.close();
56+
}
57+
58+
@Test
59+
public void getGzipResponse()
60+
throws ExecutionException, InterruptedException, TimeoutException, IOException {
61+
HttpGet getRequest =
62+
new HttpGet(String.format("http://localhost:%s/gzip", testHttpServer.port()));
63+
getRequest.addHeader("foo", "bar");
64+
Future<HttpResponse> futureResponse =
65+
client.execute(
66+
getRequest, new ApacheAsyncClientInstrumentationModuleTest.NoopFutureCallback());
67+
68+
HttpResponse response = futureResponse.get();
69+
Assertions.assertEquals(200, response.getStatusLine().getStatusCode());
70+
try (InputStream gzipStream = new GZIPInputStream(response.getEntity().getContent())) {
71+
String responseBody = readInputStream(gzipStream);
72+
Assertions.assertEquals(TestHttpServer.GzipHandler.RESPONSE_BODY, responseBody);
73+
}
74+
75+
TEST_WRITER.waitForTraces(1);
76+
// exclude server spans
77+
List<List<Span>> traces =
78+
TEST_WRITER.waitForSpans(
79+
2,
80+
span ->
81+
span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER)
82+
|| span.getAttributesList().stream()
83+
.noneMatch(
84+
keyValue ->
85+
keyValue.getKey().equals("http.response.header.content-encoding")
86+
&& keyValue.getValue().getStringValue().contains("gzip")));
87+
Assertions.assertEquals(1, traces.size());
88+
Assertions.assertEquals(2, traces.get(0).size());
89+
Span clientSpan = traces.get(0).get(1);
90+
Span responseBodySpan = traces.get(0).get(0);
91+
if (traces.get(0).get(0).getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)) {
92+
clientSpan = traces.get(0).get(0);
93+
responseBodySpan = traces.get(0).get(1);
94+
}
95+
96+
Assertions.assertEquals(
97+
"test-value",
98+
TEST_WRITER
99+
.getAttributesMap(clientSpan)
100+
.get("http.response.header.test-response-header")
101+
.getStringValue());
102+
Assertions.assertEquals(
103+
"bar",
104+
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.header.foo").getStringValue());
105+
Assertions.assertNull(TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body"));
106+
107+
Assertions.assertEquals(
108+
TestHttpServer.GzipHandler.RESPONSE_BODY,
109+
TEST_WRITER.getAttributesMap(responseBodySpan).get("http.response.body").getStringValue());
110+
}
111+
112+
private String readInputStream(InputStream inputStream) throws IOException {
113+
StringBuilder textBuilder = new StringBuilder();
114+
115+
try (BufferedReader reader =
116+
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
117+
int c;
118+
while ((c = reader.read()) != -1) {
119+
textBuilder.append((char) c);
120+
}
121+
}
122+
return textBuilder.toString();
123+
}
124+
}

instrumentation/apache-httpasyncclient-4.1/src/test/java/io/opentelemetry/instrumentation/hypertrace/apachehttpasyncclient/ApacheAsyncClientInstrumentationModuleTest.java

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import java.io.InputStreamReader;
2525
import java.io.UnsupportedEncodingException;
2626
import java.net.URI;
27+
import java.nio.charset.Charset;
2728
import java.nio.charset.StandardCharsets;
2829
import java.util.List;
2930
import java.util.concurrent.ExecutionException;
3031
import java.util.concurrent.Future;
3132
import java.util.concurrent.TimeoutException;
32-
import java.util.zip.GZIPInputStream;
3333
import org.apache.http.Header;
3434
import org.apache.http.HttpEntity;
3535
import org.apache.http.HttpResponse;
@@ -112,51 +112,6 @@ public void getJson()
112112
TEST_WRITER.getAttributesMap(responseBodySpan).get("http.response.body").getStringValue());
113113
}
114114

115-
@Disabled("This is flaky!!")
116-
@Test
117-
public void getGzipResponse()
118-
throws ExecutionException, InterruptedException, TimeoutException, IOException {
119-
HttpGet getRequest =
120-
new HttpGet(String.format("http://localhost:%s/gzip", testHttpServer.port()));
121-
getRequest.addHeader("foo", "bar");
122-
Future<HttpResponse> futureResponse = client.execute(getRequest, new NoopFutureCallback());
123-
124-
HttpResponse response = futureResponse.get();
125-
Assertions.assertEquals(200, response.getStatusLine().getStatusCode());
126-
try (InputStream gzipStream = new GZIPInputStream(response.getEntity().getContent())) {
127-
String responseBody = readInputStream(gzipStream);
128-
Assertions.assertEquals(TestHttpServer.GzipHandler.RESPONSE_BODY, responseBody);
129-
}
130-
131-
TEST_WRITER.waitForTraces(1);
132-
// exclude server spans
133-
List<List<Span>> traces =
134-
TEST_WRITER.waitForSpans(2, span -> span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER));
135-
Assertions.assertEquals(1, traces.size());
136-
Assertions.assertEquals(2, traces.get(0).size());
137-
Span clientSpan = traces.get(0).get(1);
138-
Span responseBodySpan = traces.get(0).get(0);
139-
if (traces.get(0).get(0).getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)) {
140-
clientSpan = traces.get(0).get(0);
141-
responseBodySpan = traces.get(0).get(1);
142-
}
143-
144-
Assertions.assertEquals(
145-
"test-value",
146-
TEST_WRITER
147-
.getAttributesMap(clientSpan)
148-
.get("http.response.header.test-response-header")
149-
.getStringValue());
150-
Assertions.assertEquals(
151-
"bar",
152-
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.header.foo").getStringValue());
153-
Assertions.assertNull(TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body"));
154-
155-
Assertions.assertEquals(
156-
TestHttpServer.GzipHandler.RESPONSE_BODY,
157-
TEST_WRITER.getAttributesMap(responseBodySpan).get("http.response.body").getStringValue());
158-
}
159-
160115
@Test
161116
public void postJson()
162117
throws IOException, TimeoutException, InterruptedException, ExecutionException {
@@ -210,7 +165,8 @@ private static String readInputStream(InputStream inputStream) throws IOExceptio
210165
StringBuilder textBuilder = new StringBuilder();
211166

212167
try (BufferedReader reader =
213-
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
168+
new BufferedReader(
169+
new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
214170
int c;
215171
while ((c = reader.read()) != -1) {
216172
textBuilder.append((char) c);

0 commit comments

Comments
 (0)