Refactor JDK HttpClient tests to Java (#6486)
This commit is contained in:
parent
abf601c678
commit
4cbfd7da8b
|
@ -1,69 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright The OpenTelemetry Authors
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
import io.opentelemetry.instrumentation.test.AgentTestTrait
|
|
||||||
import io.opentelemetry.instrumentation.test.base.HttpClientTest
|
|
||||||
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest
|
|
||||||
import spock.lang.Shared
|
|
||||||
|
|
||||||
import java.net.http.HttpClient
|
|
||||||
import java.net.http.HttpRequest
|
|
||||||
import java.net.http.HttpResponse
|
|
||||||
import java.time.Duration
|
|
||||||
import java.time.temporal.ChronoUnit
|
|
||||||
|
|
||||||
class JdkHttpClientTest extends HttpClientTest<HttpRequest> implements AgentTestTrait {
|
|
||||||
|
|
||||||
@Shared
|
|
||||||
def client = HttpClient.newBuilder()
|
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
|
||||||
.connectTimeout(Duration.of(CONNECT_TIMEOUT_MS, ChronoUnit.MILLIS))
|
|
||||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
@Override
|
|
||||||
HttpRequest buildRequest(String method, URI uri, Map<String, String> headers) {
|
|
||||||
def requestBuilder = HttpRequest.newBuilder()
|
|
||||||
.uri(uri)
|
|
||||||
.method(method, HttpRequest.BodyPublishers.noBody())
|
|
||||||
headers.entrySet().each {
|
|
||||||
requestBuilder.header(it.key, it.value)
|
|
||||||
}
|
|
||||||
if (uri.toString().contains("/read-timeout")) {
|
|
||||||
requestBuilder.timeout(Duration.of(READ_TIMEOUT_MS, ChronoUnit.MILLIS))
|
|
||||||
}
|
|
||||||
return requestBuilder.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
int sendRequest(HttpRequest request, String method, URI uri, Map<String, String> headers) {
|
|
||||||
return client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void sendRequestWithCallback(HttpRequest request, String method, URI uri, Map<String, String> headers, AbstractHttpClientTest.RequestResult requestResult) {
|
|
||||||
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
|
||||||
.whenComplete { response, throwable ->
|
|
||||||
requestResult.complete({ response.statusCode() }, throwable?.getCause())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean testCircularRedirects() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO nested client span is not created, but context is still injected
|
|
||||||
// which is not what the test expects
|
|
||||||
@Override
|
|
||||||
boolean testWithClientParent() {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean testReadTimeout() {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
/*
|
||||||
|
* Copyright The OpenTelemetry Authors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.opentelemetry.javaagent.instrumentation.httpclient;
|
||||||
|
|
||||||
|
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
|
||||||
|
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
|
||||||
|
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
|
||||||
|
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
|
||||||
|
public class JdkHttpClientTest extends AbstractHttpClientTest<HttpRequest> {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
|
||||||
|
|
||||||
|
private static final HttpClient client =
|
||||||
|
HttpClient.newBuilder()
|
||||||
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
|
.connectTimeout(CONNECTION_TIMEOUT)
|
||||||
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpRequest buildRequest(String method, URI uri, Map<String, String> headers) {
|
||||||
|
HttpRequest.Builder requestBuilder =
|
||||||
|
HttpRequest.newBuilder().uri(uri).method(method, HttpRequest.BodyPublishers.noBody());
|
||||||
|
headers.forEach(requestBuilder::header);
|
||||||
|
if (uri.toString().contains("/read-timeout")) {
|
||||||
|
requestBuilder.timeout(READ_TIMEOUT);
|
||||||
|
}
|
||||||
|
return requestBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int sendRequest(HttpRequest request, String method, URI uri, Map<String, String> headers)
|
||||||
|
throws Exception {
|
||||||
|
return client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendRequestWithCallback(
|
||||||
|
HttpRequest request,
|
||||||
|
String method,
|
||||||
|
URI uri,
|
||||||
|
Map<String, String> headers,
|
||||||
|
AbstractHttpClientTest.RequestResult requestResult) {
|
||||||
|
client
|
||||||
|
.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||||
|
.whenComplete(
|
||||||
|
(response, throwable) -> {
|
||||||
|
if (throwable == null) {
|
||||||
|
requestResult.complete(response.statusCode());
|
||||||
|
} else {
|
||||||
|
requestResult.complete(throwable.getCause());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpClientTestOptions options) {
|
||||||
|
options.disableTestCircularRedirects();
|
||||||
|
options.enableTestReadTimeout();
|
||||||
|
// TODO nested client span is not created, but context is still injected
|
||||||
|
// which is not what the test expects
|
||||||
|
options.disableTestWithClientParent();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue