-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converts apache-httpclient-4.0 unit tests in java. Version 4.0 was using GuardedBy annotation which was interfering with the test cases, hence the library version was shifted to 4.1. #7195
- Loading branch information
1 parent
53353ac
commit 7bb978d
Showing
6 changed files
with
345 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
253 changes: 0 additions & 253 deletions
253
...he-httpclient/apache-httpclient-4.0/javaagent/src/test/groovy/ApacheHttpClientTest.groovy
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
...n/apache-httpclient/apache-httpclient-4.0/javaagent/src/test/groovy/HttpUriRequest.groovy
This file was deleted.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
...lemetry/javaagent/instrumentation/apachehttpclient/v4_0/AbstractApacheHttpClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest; | ||
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult; | ||
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; | ||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.protocol.BasicHttpContext; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
abstract class AbstractApacheHttpClientTest<T extends HttpRequest> | ||
extends AbstractHttpClientTest<T> { | ||
@Override | ||
protected String userAgent() { | ||
return "apachehttpclient"; | ||
} | ||
|
||
@Override | ||
protected void configure(HttpClientTestOptions.Builder optionsBuilder) { | ||
optionsBuilder.setUserAgent(userAgent()); | ||
optionsBuilder.enableTestReadTimeout(); | ||
optionsBuilder.setHttpAttributes(AbstractApacheHttpClientTest::getHttpAttributes); | ||
} | ||
|
||
private static Set<AttributeKey<?>> getHttpAttributes(URI endpoint) { | ||
return HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES; | ||
} | ||
|
||
@Override | ||
public T buildRequest(String method, URI uri, Map<String, String> headers) { | ||
T request = createRequest(method, uri); | ||
request.addHeader("user-agent", userAgent()); | ||
headers.forEach(request::setHeader); | ||
return request; | ||
} | ||
|
||
@Override | ||
public int sendRequest(T request, String method, URI uri, Map<String, String> headers) | ||
throws Exception { | ||
return getResponseCode(executeRequest(request, uri)); | ||
} | ||
|
||
@Override | ||
public void sendRequestWithCallback( | ||
T request, | ||
String method, | ||
URI uri, | ||
Map<String, String> headers, | ||
HttpClientResult requestResult) { | ||
try { | ||
executeRequestWithCallback(request, uri, requestResult); | ||
} catch (Throwable throwable) { | ||
requestResult.complete(throwable); | ||
} | ||
} | ||
|
||
protected HttpHost getHost(URI uri) { | ||
return new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); | ||
} | ||
|
||
protected HttpContext getContext() { | ||
return new BasicHttpContext(); | ||
} | ||
|
||
protected static String fullPathFromUri(URI uri) { | ||
StringBuilder builder = new StringBuilder(); | ||
if (uri.getPath() != null) { | ||
builder.append(uri.getPath()); | ||
} | ||
|
||
if (uri.getQuery() != null) { | ||
builder.append('?'); | ||
builder.append(uri.getQuery()); | ||
} | ||
|
||
if (uri.getFragment() != null) { | ||
builder.append('#'); | ||
builder.append(uri.getFragment()); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
abstract T createRequest(String method, URI uri); | ||
|
||
abstract HttpResponse executeRequest(T request, URI uri) throws Exception; | ||
|
||
abstract void executeRequestWithCallback(T request, URI uri, HttpClientResult requestResult) | ||
throws Exception; | ||
|
||
private static int getResponseCode(HttpResponse response) { | ||
return response.getStatusLine().getStatusCode(); | ||
} | ||
} |
Oops, something went wrong.