|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0; |
| 7 | + |
| 8 | +import io.opentelemetry.api.common.AttributeKey; |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; |
| 10 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult; |
| 11 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; |
| 12 | +import java.net.URI; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import org.apache.http.HttpHost; |
| 16 | +import org.apache.http.HttpRequest; |
| 17 | +import org.apache.http.HttpResponse; |
| 18 | +import org.apache.http.protocol.BasicHttpContext; |
| 19 | +import org.apache.http.protocol.HttpContext; |
| 20 | + |
| 21 | +abstract class AbstractApacheHttpClientTest<T extends HttpRequest> |
| 22 | + extends AbstractHttpClientTest<T> { |
| 23 | + @Override |
| 24 | + protected String userAgent() { |
| 25 | + return "apachehttpclient"; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void configure(HttpClientTestOptions.Builder optionsBuilder) { |
| 30 | + optionsBuilder.setUserAgent(userAgent()); |
| 31 | + optionsBuilder.enableTestReadTimeout(); |
| 32 | + optionsBuilder.setHttpAttributes(AbstractApacheHttpClientTest::getHttpAttributes); |
| 33 | + } |
| 34 | + |
| 35 | + private static Set<AttributeKey<?>> getHttpAttributes(URI endpoint) { |
| 36 | + return HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public T buildRequest(String method, URI uri, Map<String, String> headers) { |
| 41 | + T request = createRequest(method, uri); |
| 42 | + request.addHeader("user-agent", userAgent()); |
| 43 | + headers.forEach(request::setHeader); |
| 44 | + return request; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public int sendRequest(T request, String method, URI uri, Map<String, String> headers) |
| 49 | + throws Exception { |
| 50 | + return getResponseCode(executeRequest(request, uri)); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void sendRequestWithCallback( |
| 55 | + T request, |
| 56 | + String method, |
| 57 | + URI uri, |
| 58 | + Map<String, String> headers, |
| 59 | + HttpClientResult requestResult) { |
| 60 | + try { |
| 61 | + executeRequestWithCallback(request, uri, requestResult); |
| 62 | + } catch (Throwable throwable) { |
| 63 | + requestResult.complete(throwable); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected HttpHost getHost(URI uri) { |
| 68 | + return new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); |
| 69 | + } |
| 70 | + |
| 71 | + protected HttpContext getContext() { |
| 72 | + return new BasicHttpContext(); |
| 73 | + } |
| 74 | + |
| 75 | + protected static String fullPathFromUri(URI uri) { |
| 76 | + StringBuilder builder = new StringBuilder(); |
| 77 | + if (uri.getPath() != null) { |
| 78 | + builder.append(uri.getPath()); |
| 79 | + } |
| 80 | + |
| 81 | + if (uri.getQuery() != null) { |
| 82 | + builder.append('?'); |
| 83 | + builder.append(uri.getQuery()); |
| 84 | + } |
| 85 | + |
| 86 | + if (uri.getFragment() != null) { |
| 87 | + builder.append('#'); |
| 88 | + builder.append(uri.getFragment()); |
| 89 | + } |
| 90 | + return builder.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + abstract T createRequest(String method, URI uri); |
| 94 | + |
| 95 | + abstract HttpResponse executeRequest(T request, URI uri) throws Exception; |
| 96 | + |
| 97 | + abstract void executeRequestWithCallback(T request, URI uri, HttpClientResult requestResult) |
| 98 | + throws Exception; |
| 99 | + |
| 100 | + private static int getResponseCode(HttpResponse response) { |
| 101 | + return response.getStatusLine().getStatusCode(); |
| 102 | + } |
| 103 | +} |
0 commit comments