Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.BeforeAll;
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 = new HttpClient(connectionManager);

private static final HttpClient clientWithReadTimeout = new HttpClient(connectionManager);
xiepuhuan marked this conversation as resolved.
Show resolved Hide resolved

@BeforeAll
static void setUp() {
client.setConnectionTimeout((int) CONNECTION_TIMEOUT.toMillis());
clientWithReadTimeout.setTimeout((int) READ_TIMEOUT.toMillis());
}
xiepuhuan marked this conversation as resolved.
Show resolved Hide resolved

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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Loading