From de30e3120a77e89368aa983b837c06706001ed35 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Sat, 1 Jun 2024 11:20:45 +0300 Subject: [PATCH] Add http/2 tests for jdk http client (#11506) --- .../httpclient/JavaHttpClientTest.java | 37 ++++++++++++++++++- .../httpclient/JavaHttpClientTest.java | 37 ++++++++++++++++++- .../AbstractJavaHttpClientTest.java | 10 +++-- .../junit/http/AbstractHttpClientTest.java | 3 +- 4 files changed, 80 insertions(+), 7 deletions(-) diff --git a/instrumentation/java-http-client/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/httpclient/JavaHttpClientTest.java b/instrumentation/java-http-client/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/httpclient/JavaHttpClientTest.java index 2d9906b1d46d..b3e0e74c6397 100644 --- a/instrumentation/java-http-client/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/httpclient/JavaHttpClientTest.java +++ b/instrumentation/java-http-client/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/httpclient/JavaHttpClientTest.java @@ -8,10 +8,12 @@ import io.opentelemetry.instrumentation.httpclient.AbstractJavaHttpClientTest; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions; import java.net.http.HttpClient; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.extension.RegisterExtension; -public class JavaHttpClientTest extends AbstractJavaHttpClientTest { +public abstract class JavaHttpClientTest extends AbstractJavaHttpClientTest { @RegisterExtension static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent(); @@ -20,4 +22,37 @@ public class JavaHttpClientTest extends AbstractJavaHttpClientTest { protected HttpClient configureHttpClient(HttpClient httpClient) { return httpClient; } + + @Nested + static class Http1ClientTest extends JavaHttpClientTest { + + @Override + protected void configureHttpClientBuilder(HttpClient.Builder httpClientBuilder) { + httpClientBuilder.version(HttpClient.Version.HTTP_1_1); + } + } + + @Nested + static class Http2ClientTest extends JavaHttpClientTest { + + @Override + protected void configureHttpClientBuilder(HttpClient.Builder httpClientBuilder) { + httpClientBuilder.version(HttpClient.Version.HTTP_2); + } + + @Override + protected void configure(HttpClientTestOptions.Builder optionsBuilder) { + super.configure(optionsBuilder); + + optionsBuilder.setHttpProtocolVersion( + uri -> { + String uriString = uri.toString(); + if (uriString.equals("http://localhost:61/") + || uriString.equals("https://192.0.2.1/")) { + return "1.1"; + } + return "2"; + }); + } + } } diff --git a/instrumentation/java-http-client/library/src/test/java/io/opentelemetry/instrumentation/httpclient/JavaHttpClientTest.java b/instrumentation/java-http-client/library/src/test/java/io/opentelemetry/instrumentation/httpclient/JavaHttpClientTest.java index 5bd0396c96a9..900d916aeb24 100644 --- a/instrumentation/java-http-client/library/src/test/java/io/opentelemetry/instrumentation/httpclient/JavaHttpClientTest.java +++ b/instrumentation/java-http-client/library/src/test/java/io/opentelemetry/instrumentation/httpclient/JavaHttpClientTest.java @@ -8,11 +8,13 @@ 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.http.HttpClient; import java.util.Collections; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.extension.RegisterExtension; -public class JavaHttpClientTest extends AbstractJavaHttpClientTest { +public abstract class JavaHttpClientTest extends AbstractJavaHttpClientTest { @RegisterExtension static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forLibrary(); @@ -27,4 +29,37 @@ protected HttpClient configureHttpClient(HttpClient httpClient) { .build() .newHttpClient(httpClient); } + + @Nested + static class Http1ClientTest extends JavaHttpClientTest { + + @Override + protected void configureHttpClientBuilder(HttpClient.Builder httpClientBuilder) { + httpClientBuilder.version(HttpClient.Version.HTTP_1_1); + } + } + + @Nested + static class Http2ClientTest extends JavaHttpClientTest { + + @Override + protected void configureHttpClientBuilder(HttpClient.Builder httpClientBuilder) { + httpClientBuilder.version(HttpClient.Version.HTTP_2); + } + + @Override + protected void configure(HttpClientTestOptions.Builder optionsBuilder) { + super.configure(optionsBuilder); + + optionsBuilder.setHttpProtocolVersion( + uri -> { + String uriString = uri.toString(); + if (uriString.equals("http://localhost:61/") + || uriString.equals("https://192.0.2.1/")) { + return "1.1"; + } + return "2"; + }); + } + } } diff --git a/instrumentation/java-http-client/testing/src/main/java/io/opentelemetry/instrumentation/httpclient/AbstractJavaHttpClientTest.java b/instrumentation/java-http-client/testing/src/main/java/io/opentelemetry/instrumentation/httpclient/AbstractJavaHttpClientTest.java index 9541f8a97346..6e7caadbda70 100644 --- a/instrumentation/java-http-client/testing/src/main/java/io/opentelemetry/instrumentation/httpclient/AbstractJavaHttpClientTest.java +++ b/instrumentation/java-http-client/testing/src/main/java/io/opentelemetry/instrumentation/httpclient/AbstractJavaHttpClientTest.java @@ -26,15 +26,17 @@ public abstract class AbstractJavaHttpClientTest extends AbstractHttpClientTest< @BeforeAll void setUp() { - HttpClient httpClient = + HttpClient.Builder httpClientBuilder = HttpClient.newBuilder() - .version(HttpClient.Version.HTTP_1_1) .connectTimeout(CONNECTION_TIMEOUT) - .followRedirects(HttpClient.Redirect.NORMAL) - .build(); + .followRedirects(HttpClient.Redirect.NORMAL); + configureHttpClientBuilder(httpClientBuilder); + HttpClient httpClient = httpClientBuilder.build(); client = configureHttpClient(httpClient); } + protected abstract void configureHttpClientBuilder(HttpClient.Builder httpClientBuilder); + protected abstract HttpClient configureHttpClient(HttpClient httpClient); @Override diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java index 02250b349771..4fc5ac672621 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java @@ -130,7 +130,8 @@ void requestWithNonStandardHttpMethod() throws Exception { String method = "TEST"; int responseCode = doRequest(method, uri); - assertThat(responseCode).isEqualTo(405); + assertThat(responseCode) + .isEqualTo("2".equals(options.getHttpProtocolVersion().apply(uri)) ? 400 : 405); testing.waitAndAssertTraces( trace ->