Convert apache-httpclient-2.0 tests from groovy to java (#12102)

Co-authored-by: Steve Rao <raozihao.rzh@alibaba-inc.com>
This commit is contained in:
xiepuhuan 2024-08-27 06:29:52 +08:00 committed by GitHub
parent d6d3334a6a
commit 53f019b8f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 149 additions and 142 deletions

View File

@ -1,98 +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 org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.HttpConnectionManager
import org.apache.commons.httpclient.HttpMethod
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager
import org.apache.commons.httpclient.methods.DeleteMethod
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.HeadMethod
import org.apache.commons.httpclient.methods.OptionsMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.PutMethod
import org.apache.commons.httpclient.methods.TraceMethod
import spock.lang.Shared
abstract class AbstractCommonsHttpClientTest extends HttpClientTest<HttpMethod> implements AgentTestTrait {
@Shared
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager()
@Shared
HttpClient client = buildClient(false)
@Shared
HttpClient clientWithReadTimeout = buildClient(true)
def buildClient(boolean readTimeout) {
HttpClient client = new HttpClient(connectionManager)
client.setConnectionTimeout(CONNECT_TIMEOUT_MS)
if (readTimeout) {
client.setTimeout(READ_TIMEOUT_MS)
}
return client
}
HttpClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return clientWithReadTimeout
}
return client
}
@Override
HttpMethod buildRequest(String method, URI uri, Map<String, String> headers) {
def request
switch (method) {
case "GET":
request = new GetMethod(uri.toString())
break
case "PUT":
request = new PutMethod(uri.toString())
break
case "POST":
request = new PostMethod(uri.toString())
break
case "HEAD":
request = new HeadMethod(uri.toString())
break
case "DELETE":
request = new DeleteMethod(uri.toString())
break
case "OPTIONS":
request = new OptionsMethod(uri.toString())
break
case "TRACE":
request = new TraceMethod(uri.toString())
break
default:
throw new IllegalStateException("Unsupported method: " + method)
}
headers.each { request.setRequestHeader(it.key, it.value) }
return request
}
@Override
boolean testCircularRedirects() {
false
}
@Override
boolean testReusedRequest() {
// apache commons throws an exception if the request is reused without being recycled first
// at which point this test is not useful (and requires re-populating uri)
false
}
@Override
boolean testCallback() {
false
}
@Override
boolean testNonStandardHttpMethod() {
false
}
}

View File

@ -1,25 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import org.apache.commons.httpclient.HttpMethod
import spock.lang.IgnoreIf
//this test will be ignored if not executed with -PtestLatestDeps=true
//because the latest dependency commons-httpclient v3.1 allows a call to the executeMethod
//with some null parameters like HttpClient.executeMethod(null, request, null)
//but this construct is not allowed in commons-httpclient v2 that is used for regular otel testing
@IgnoreIf({ !Boolean.getBoolean("testLatestDeps") })
class CommonsHttpClientLatestDepsTest extends AbstractCommonsHttpClientTest {
@Override
int sendRequest(HttpMethod request, String method, URI uri, Map<String, String> headers) {
try {
getClient(uri).executeMethod(null, request, null)
return request.getStatusCode()
} finally {
request.releaseConnection()
}
}
}

View File

@ -1,19 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import org.apache.commons.httpclient.HttpMethod
class CommonsHttpClientTest extends AbstractCommonsHttpClientTest {
@Override
int sendRequest(HttpMethod request, String method, URI uri, Map<String, String> headers) {
try {
getClient(uri).executeMethod(request)
return request.getStatusCode()
} finally {
request.releaseConnection()
}
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0;
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.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.OptionsMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.TraceMethod;
import org.junit.jupiter.api.extension.RegisterExtension;
abstract class AbstractCommonsHttpClientTest extends AbstractHttpClientTest<HttpMethod> {
@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
private static final HttpConnectionManager connectionManager =
new MultiThreadedHttpConnectionManager();
private static final HttpClient client = buildClient(false);
private static final HttpClient clientWithReadTimeout = buildClient(true);
static HttpClient buildClient(boolean readTimeout) {
HttpClient client = new HttpClient(connectionManager);
client.setConnectionTimeout((int) CONNECTION_TIMEOUT.toMillis());
if (readTimeout) {
client.setTimeout((int) READ_TIMEOUT.toMillis());
}
return client;
}
HttpClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return clientWithReadTimeout;
}
return client;
}
@Override
public HttpMethod buildRequest(String method, URI uri, Map<String, String> headers) {
HttpMethod request;
switch (method) {
case "GET":
request = new GetMethod(uri.toString());
break;
case "PUT":
request = new PutMethod(uri.toString());
break;
case "POST":
request = new PostMethod(uri.toString());
break;
case "HEAD":
request = new HeadMethod(uri.toString());
break;
case "DELETE":
request = new DeleteMethod(uri.toString());
break;
case "OPTIONS":
request = new OptionsMethod(uri.toString());
break;
case "TRACE":
request = new TraceMethod(uri.toString());
break;
default:
throw new IllegalStateException("Unsupported method: " + method);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
request.setRequestHeader(entry.getKey(), entry.getValue());
}
return request;
}
@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
optionsBuilder
.disableTestCallback()
.disableTestReusedRequest()
.disableTestNonStandardHttpMethod()
.disableTestCircularRedirects();
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0;
import java.net.URI;
import java.util.Map;
import org.apache.commons.httpclient.HttpMethod;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
// this test will be ignored if not executed with -PtestLatestDeps=true
// because the latest dependency commons-httpclient v3.1 allows a call to the executeMethod
// with some null parameters like HttpClient.executeMethod(null, request, null)
// but this construct is not allowed in commons-httpclient v2 that is used for regular otel testing
@EnabledIfSystemProperty(named = "testLatestDeps", matches = "true")
public class CommonsHttpClientLatestDepsTest extends AbstractCommonsHttpClientTest {
@Override
public int sendRequest(HttpMethod request, String method, URI uri, Map<String, String> headers)
throws Exception {
try {
getClient(uri).executeMethod(null, request, null);
return request.getStatusCode();
} finally {
request.releaseConnection();
}
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0;
import java.net.URI;
import java.util.Map;
import org.apache.commons.httpclient.HttpMethod;
class CommonsHttpClientTest extends AbstractCommonsHttpClientTest {
@Override
public int sendRequest(HttpMethod request, String method, URI uri, Map<String, String> headers)
throws Exception {
try {
getClient(uri).executeMethod(request);
return request.getStatusCode();
} finally {
request.releaseConnection();
}
}
}