|
16 | 16 |
|
17 | 17 | package io.opentelemetry.javaagent.instrumentation.hypertrace.apachehttpclient.v4_0;
|
18 | 18 |
|
19 |
| -import io.opentelemetry.api.trace.Span; |
20 |
| -import io.opentelemetry.sdk.trace.data.SpanData; |
21 |
| -import java.io.BufferedReader; |
22 | 19 | import java.io.IOException;
|
23 | 20 | import java.io.InputStream;
|
24 |
| -import java.io.InputStreamReader; |
25 |
| -import java.io.UnsupportedEncodingException; |
26 | 21 | import java.net.URI;
|
27 |
| -import java.nio.charset.Charset; |
28 |
| -import java.nio.charset.StandardCharsets; |
29 |
| -import java.util.ArrayList; |
30 |
| -import java.util.List; |
31 |
| -import java.util.concurrent.TimeoutException; |
32 |
| -import org.apache.http.HttpEntity; |
| 22 | +import java.util.Map; |
33 | 23 | import org.apache.http.HttpResponse;
|
34 |
| -import org.apache.http.NameValuePair; |
35 | 24 | import org.apache.http.client.HttpClient;
|
36 |
| -import org.apache.http.client.entity.UrlEncodedFormEntity; |
37 | 25 | import org.apache.http.client.methods.HttpGet;
|
38 | 26 | import org.apache.http.client.methods.HttpPost;
|
39 | 27 | import org.apache.http.entity.StringEntity;
|
40 | 28 | import org.apache.http.impl.client.DefaultHttpClient;
|
41 |
| -import org.apache.http.message.BasicNameValuePair; |
42 |
| -import org.apache.http.protocol.HTTP; |
43 |
| -import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; |
44 |
| -import org.hypertrace.agent.testing.AbstractInstrumenterTest; |
45 |
| -import org.hypertrace.agent.testing.TestHttpServer; |
46 |
| -import org.hypertrace.agent.testing.TestHttpServer.GetJsonHandler; |
47 |
| -import org.junit.jupiter.api.AfterAll; |
48 |
| -import org.junit.jupiter.api.Assertions; |
49 |
| -import org.junit.jupiter.api.BeforeAll; |
50 |
| -import org.junit.jupiter.api.Test; |
| 29 | +import org.hypertrace.agent.testing.AbstractHttpClientTest; |
51 | 30 |
|
52 |
| -public class ApacheHttpClientInstrumentationTest extends AbstractInstrumenterTest { |
53 |
| - |
54 |
| - private static final String JSON = "{\"id\":1,\"name\":\"John\"}"; |
55 |
| - private static final TestHttpServer testHttpServer = new TestHttpServer(); |
| 31 | +public class ApacheHttpClientInstrumentationTest extends AbstractHttpClientTest { |
56 | 32 |
|
57 | 33 | private final HttpClient client = new DefaultHttpClient();
|
58 | 34 |
|
59 |
| - @BeforeAll |
60 |
| - public static void startServer() throws Exception { |
61 |
| - testHttpServer.start(); |
62 |
| - } |
63 |
| - |
64 |
| - @AfterAll |
65 |
| - public static void closeServer() throws Exception { |
66 |
| - testHttpServer.close(); |
| 35 | + public ApacheHttpClientInstrumentationTest() { |
| 36 | + super(true); |
67 | 37 | }
|
68 | 38 |
|
69 |
| - @Test |
70 |
| - public void getJson() throws IOException, TimeoutException, InterruptedException { |
71 |
| - HttpGet getRequest = new HttpGet(); |
72 |
| - getRequest.addHeader("foo", "bar"); |
73 |
| - getRequest.setURI( |
74 |
| - URI.create(String.format("http://localhost:%d/get_json", testHttpServer.port()))); |
75 |
| - HttpResponse response = client.execute(getRequest); |
76 |
| - Assertions.assertEquals(200, response.getStatusLine().getStatusCode()); |
77 |
| - InputStream inputStream = response.getEntity().getContent(); |
78 |
| - Assertions.assertEquals(GetJsonHandler.RESPONSE_BODY, readInputStream(inputStream)); |
79 |
| - |
80 |
| - Assertions.assertEquals(false, Span.current().isRecording()); |
81 |
| - |
82 |
| - TEST_WRITER.waitForTraces(1); |
83 |
| - List<List<SpanData>> traces = TEST_WRITER.getTraces(); |
84 |
| - Assertions.assertEquals(1, traces.size()); |
85 |
| - Assertions.assertEquals(2, traces.get(0).size()); |
86 |
| - SpanData clientSpan = traces.get(0).get(0); |
87 |
| - |
88 |
| - Assertions.assertEquals( |
89 |
| - "test-value", |
90 |
| - clientSpan |
91 |
| - .getAttributes() |
92 |
| - .get(HypertraceSemanticAttributes.httpResponseHeader("test-response-header"))); |
93 |
| - Assertions.assertEquals( |
94 |
| - "bar", |
95 |
| - clientSpan.getAttributes().get(HypertraceSemanticAttributes.httpRequestHeader("foo"))); |
96 |
| - Assertions.assertNull( |
97 |
| - clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); |
98 |
| - SpanData responseBodySpan = traces.get(0).get(1); |
99 |
| - Assertions.assertEquals( |
100 |
| - GetJsonHandler.RESPONSE_BODY, |
101 |
| - responseBodySpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); |
102 |
| - } |
103 |
| - |
104 |
| - @Test |
105 |
| - public void postUrlEncoded() throws IOException, TimeoutException, InterruptedException { |
106 |
| - List<NameValuePair> nvps = new ArrayList<>(); |
107 |
| - nvps.add(new BasicNameValuePair("key1", "value1")); |
108 |
| - nvps.add(new BasicNameValuePair("key2", "value2")); |
109 |
| - |
110 |
| - HttpPost postRequest = new HttpPost(); |
111 |
| - postRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); |
112 |
| - postRequest.setURI( |
113 |
| - URI.create(String.format("http://localhost:%d/post", testHttpServer.port()))); |
114 |
| - HttpResponse response = client.execute(postRequest); |
115 |
| - Assertions.assertEquals(204, response.getStatusLine().getStatusCode()); |
116 |
| - |
117 |
| - TEST_WRITER.waitForTraces(1); |
118 |
| - List<List<SpanData>> traces = TEST_WRITER.getTraces(); |
119 |
| - Assertions.assertEquals(1, traces.size()); |
120 |
| - Assertions.assertEquals(1, traces.get(0).size()); |
121 |
| - SpanData clientSpan = traces.get(0).get(0); |
122 |
| - |
123 |
| - Assertions.assertEquals( |
124 |
| - "test-value", |
125 |
| - clientSpan |
126 |
| - .getAttributes() |
127 |
| - .get(HypertraceSemanticAttributes.httpResponseHeader("test-response-header"))); |
128 |
| - Assertions.assertEquals( |
129 |
| - "key1=value1&key2=value2", |
130 |
| - clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); |
131 |
| - Assertions.assertNull( |
132 |
| - clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); |
133 |
| - } |
134 |
| - |
135 |
| - @Test |
136 |
| - public void postJson() throws IOException, TimeoutException, InterruptedException { |
137 |
| - StringEntity entity = new StringEntity(JSON); |
138 |
| - entity.setContentType("application/json"); |
139 |
| - postJsonEntity(entity); |
140 |
| - } |
| 39 | + @Override |
| 40 | + public Response doPostRequest( |
| 41 | + String uri, Map<String, String> headers, String body, String contentType) throws IOException { |
141 | 42 |
|
142 |
| - @Test |
143 |
| - public void postJsonNonRepeatableEntity() |
144 |
| - throws IOException, TimeoutException, InterruptedException { |
145 |
| - StringEntity entity = new NonRepeatableStringEntity(JSON); |
146 |
| - entity.setContentType("application/json"); |
147 |
| - postJsonEntity(entity); |
148 |
| - } |
149 |
| - |
150 |
| - public void postJsonEntity(HttpEntity entity) |
151 |
| - throws TimeoutException, InterruptedException, IOException { |
152 |
| - HttpPost postRequest = new HttpPost(); |
153 |
| - postRequest.setEntity(entity); |
154 |
| - postRequest.setHeader("Content-type", "application/json"); |
155 |
| - postRequest.setURI( |
156 |
| - URI.create(String.format("http://localhost:%d/post", testHttpServer.port()))); |
157 |
| - HttpResponse response = client.execute(postRequest); |
158 |
| - Assertions.assertEquals(204, response.getStatusLine().getStatusCode()); |
159 |
| - |
160 |
| - TEST_WRITER.waitForTraces(1); |
161 |
| - List<List<SpanData>> traces = TEST_WRITER.getTraces(); |
162 |
| - Assertions.assertEquals(1, traces.size()); |
163 |
| - Assertions.assertEquals(1, traces.get(0).size()); |
164 |
| - SpanData clientSpan = traces.get(0).get(0); |
165 |
| - |
166 |
| - Assertions.assertEquals( |
167 |
| - "test-value", |
168 |
| - clientSpan |
169 |
| - .getAttributes() |
170 |
| - .get(HypertraceSemanticAttributes.httpResponseHeader("test-response-header"))); |
171 |
| - Assertions.assertEquals( |
172 |
| - readInputStream(entity.getContent()), |
173 |
| - clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); |
174 |
| - Assertions.assertNull( |
175 |
| - clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); |
176 |
| - } |
177 |
| - |
178 |
| - @Test |
179 |
| - public void getContent_throws_exception() throws IOException { |
180 |
| - HttpClient client = new DefaultHttpClient(); |
181 |
| - HttpGet getRequest = new HttpGet(); |
182 |
| - getRequest.setURI( |
183 |
| - URI.create(String.format("http://localhost:%d/get_json", testHttpServer.port()))); |
184 |
| - HttpResponse response = client.execute(getRequest); |
185 |
| - HttpEntity entity = response.getEntity(); |
186 |
| - Assertions.assertNotNull(entity.getContent()); |
187 |
| - try { |
188 |
| - entity.getContent(); |
189 |
| - } catch (Exception ex) { |
190 |
| - Assertions.assertEquals(IllegalStateException.class, ex.getClass()); |
191 |
| - } |
192 |
| - } |
193 |
| - |
194 |
| - private static String readInputStream(InputStream inputStream) throws IOException { |
195 |
| - StringBuilder textBuilder = new StringBuilder(); |
196 |
| - |
197 |
| - try (BufferedReader reader = |
198 |
| - new BufferedReader( |
199 |
| - new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) { |
200 |
| - int c; |
201 |
| - while ((c = reader.read()) != -1) { |
202 |
| - textBuilder.append((char) c); |
203 |
| - } |
| 43 | + HttpPost request = new HttpPost(); |
| 44 | + for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 45 | + request.addHeader(entry.getKey(), entry.getValue()); |
204 | 46 | }
|
205 |
| - return textBuilder.toString(); |
| 47 | + request.setURI(URI.create(uri)); |
| 48 | + StringEntity entity = new StringEntity(body); |
| 49 | + entity.setContentType(contentType); |
| 50 | + request.setEntity(entity); |
| 51 | + request.addHeader("Content-type", contentType); |
| 52 | + HttpResponse response = client.execute(request); |
| 53 | + InputStream inputStream = response.getEntity().getContent(); |
| 54 | + return new Response(readInputStream(inputStream), response.getStatusLine().getStatusCode()); |
206 | 55 | }
|
207 | 56 |
|
208 |
| - class NonRepeatableStringEntity extends StringEntity { |
209 |
| - |
210 |
| - public NonRepeatableStringEntity(String s) throws UnsupportedEncodingException { |
211 |
| - super(s); |
| 57 | + @Override |
| 58 | + public Response doGetRequest(String uri, Map<String, String> headers) throws IOException { |
| 59 | + HttpGet request = new HttpGet(); |
| 60 | + for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 61 | + request.addHeader(entry.getKey(), entry.getValue()); |
212 | 62 | }
|
213 |
| - |
214 |
| - @Override |
215 |
| - public boolean isRepeatable() { |
216 |
| - return false; |
| 63 | + request.setURI(URI.create(uri)); |
| 64 | + HttpResponse response = client.execute(request); |
| 65 | + if (response.getEntity() == null || response.getEntity().getContentLength() <= 0) { |
| 66 | + return new Response(null, response.getStatusLine().getStatusCode()); |
217 | 67 | }
|
| 68 | + InputStream inputStream = response.getEntity().getContent(); |
| 69 | + return new Response(readInputStream(inputStream), response.getStatusLine().getStatusCode()); |
218 | 70 | }
|
219 | 71 | }
|
0 commit comments