Skip to content

Commit

Permalink
Add http/2 tests for jdk http client (#11506)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Jun 1, 2024
1 parent 5c6aa52 commit de30e31
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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";
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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";
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down

0 comments on commit de30e31

Please sign in to comment.