Convert jaxrs-client-1.1 tests to java (#12497)
This commit is contained in:
parent
5ade05f88b
commit
3100f0b65f
|
@ -1,90 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright The OpenTelemetry Authors
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
import com.sun.jersey.api.client.Client
|
|
||||||
import com.sun.jersey.api.client.ClientHandlerException
|
|
||||||
import com.sun.jersey.api.client.ClientResponse
|
|
||||||
import com.sun.jersey.api.client.WebResource
|
|
||||||
import com.sun.jersey.api.client.filter.GZIPContentEncodingFilter
|
|
||||||
import com.sun.jersey.api.client.filter.LoggingFilter
|
|
||||||
import io.opentelemetry.api.common.AttributeKey
|
|
||||||
import io.opentelemetry.instrumentation.test.AgentTestTrait
|
|
||||||
import io.opentelemetry.instrumentation.test.base.HttpClientTest
|
|
||||||
import io.opentelemetry.semconv.NetworkAttributes
|
|
||||||
import spock.lang.Shared
|
|
||||||
|
|
||||||
class JaxRsClientV1Test extends HttpClientTest<WebResource.Builder> implements AgentTestTrait {
|
|
||||||
|
|
||||||
@Shared
|
|
||||||
Client client = buildClient(false)
|
|
||||||
|
|
||||||
@Shared
|
|
||||||
Client clientWithReadTimeout = buildClient(true)
|
|
||||||
|
|
||||||
def buildClient(boolean readTimeout) {
|
|
||||||
def client = Client.create()
|
|
||||||
client.setConnectTimeout(CONNECT_TIMEOUT_MS)
|
|
||||||
if (readTimeout) {
|
|
||||||
client.setReadTimeout(READ_TIMEOUT_MS)
|
|
||||||
}
|
|
||||||
// Add filters to ensure spans aren't duplicated.
|
|
||||||
client.addFilter(new LoggingFilter())
|
|
||||||
client.addFilter(new GZIPContentEncodingFilter())
|
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
Client getClient(URI uri) {
|
|
||||||
if (uri.toString().contains("/read-timeout")) {
|
|
||||||
return clientWithReadTimeout
|
|
||||||
}
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
def cleanupSpec() {
|
|
||||||
client?.destroy()
|
|
||||||
clientWithReadTimeout?.destroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
WebResource.Builder buildRequest(String method, URI uri, Map<String, String> headers) {
|
|
||||||
def resource = getClient(uri).resource(uri).requestBuilder
|
|
||||||
headers.each { resource.header(it.key, it.value) }
|
|
||||||
return resource
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
int sendRequest(WebResource.Builder resource, String method, URI uri, Map<String, String> headers) {
|
|
||||||
def body = BODY_METHODS.contains(method) ? "" : null
|
|
||||||
try {
|
|
||||||
return resource.method(method, ClientResponse, body).status
|
|
||||||
} catch (ClientHandlerException exception) {
|
|
||||||
throw exception.getCause()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean testCircularRedirects() {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean testCallback() {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
Set<AttributeKey<?>> httpAttributes(URI uri) {
|
|
||||||
def attributes = super.httpAttributes(uri)
|
|
||||||
attributes.remove(NetworkAttributes.NETWORK_PROTOCOL_VERSION)
|
|
||||||
return attributes
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
boolean testNonStandardHttpMethod() {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
/*
|
||||||
|
* Copyright The OpenTelemetry Authors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.opentelemetry.javaagent.instrumentation.jaxrsclient;
|
||||||
|
|
||||||
|
import com.sun.jersey.api.client.Client;
|
||||||
|
import com.sun.jersey.api.client.ClientHandlerException;
|
||||||
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
|
import com.sun.jersey.api.client.WebResource;
|
||||||
|
import com.sun.jersey.api.client.filter.GZIPContentEncodingFilter;
|
||||||
|
import com.sun.jersey.api.client.filter.LoggingFilter;
|
||||||
|
import io.opentelemetry.api.common.AttributeKey;
|
||||||
|
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 io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
|
||||||
|
import io.opentelemetry.semconv.NetworkAttributes;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
|
||||||
|
class JaxRsClientV1Test extends AbstractHttpClientTest<WebResource.Builder> {
|
||||||
|
@RegisterExtension
|
||||||
|
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
|
||||||
|
|
||||||
|
private final Client client = buildClient(false);
|
||||||
|
private final Client clientWithReadTimeout = buildClient(true);
|
||||||
|
|
||||||
|
private static Client buildClient(boolean readTimeout) {
|
||||||
|
Client client = Client.create();
|
||||||
|
client.setConnectTimeout((int) CONNECTION_TIMEOUT.toMillis());
|
||||||
|
if (readTimeout) {
|
||||||
|
client.setReadTimeout((int) READ_TIMEOUT.toMillis());
|
||||||
|
}
|
||||||
|
// Add filters to ensure spans aren't duplicated.
|
||||||
|
client.addFilter(new LoggingFilter());
|
||||||
|
client.addFilter(new GZIPContentEncodingFilter());
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpClientTestOptions.Builder options) {
|
||||||
|
options.setTestCircularRedirects(false);
|
||||||
|
options.setTestNonStandardHttpMethod(false);
|
||||||
|
options.setTestCallback(false);
|
||||||
|
options.setHttpAttributes(
|
||||||
|
serverEndpoint -> {
|
||||||
|
Set<AttributeKey<?>> attributes =
|
||||||
|
new HashSet<>(HttpServerTestOptions.DEFAULT_HTTP_ATTRIBUTES);
|
||||||
|
attributes.remove(NetworkAttributes.NETWORK_PROTOCOL_VERSION);
|
||||||
|
return attributes;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
void tearDown() {
|
||||||
|
client.destroy();
|
||||||
|
clientWithReadTimeout.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Client getClient(URI uri) {
|
||||||
|
if (uri.toString().contains("/read-timeout")) {
|
||||||
|
return clientWithReadTimeout;
|
||||||
|
}
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WebResource.Builder buildRequest(String method, URI uri, Map<String, String> headers) {
|
||||||
|
WebResource.Builder builder = getClient(uri).resource(uri).getRequestBuilder();
|
||||||
|
headers.forEach(builder::header);
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int sendRequest(
|
||||||
|
WebResource.Builder builder, String method, URI uri, Map<String, String> headers)
|
||||||
|
throws Exception {
|
||||||
|
String body = "POST".equals(method) || "PUT".equals(method) ? "" : null;
|
||||||
|
try {
|
||||||
|
return builder.method(method, ClientResponse.class, body).getStatus();
|
||||||
|
} catch (ClientHandlerException exception) {
|
||||||
|
Throwable cause = exception.getCause();
|
||||||
|
if (cause instanceof Exception) {
|
||||||
|
throw (Exception) cause;
|
||||||
|
}
|
||||||
|
throw new IllegalStateException(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue