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

Allow @ClientHeaderParam to override User-Agent #36377

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -36,6 +37,18 @@ void testHeaderOverride() {
assertThat(client.callWithUserAgent("custom-agent")).isEqualTo("custom-agent");
}

@Test
void testHeadersWithImplicitValue() {
ClientWithAnnotation client = RestClientBuilder.newBuilder().baseUri(baseUri).build(ClientWithAnnotation.class);
assertThat(client.callWithImplicitValue()).isEqualTo("Annotated");
}

@Test
void testHeadersWithExplicitValue() {
ClientWithAnnotation client = RestClientBuilder.newBuilder().baseUri(baseUri).build(ClientWithAnnotation.class);
assertThat(client.callWithExplicitValue("custom-agent")).isEqualTo("custom-agent");
}

@Path("/")
@ApplicationScoped
public static class Resource {
Expand All @@ -56,4 +69,16 @@ public interface Client {
String callWithUserAgent(@HeaderParam("User-AgenT") String userAgent);
}

@ClientHeaderParam(name = "User-Agent", value = "Annotated")
public interface ClientWithAnnotation {

@Path("/")
@GET
String callWithImplicitValue();

@Path("/")
@GET
String callWithExplicitValue(@HeaderParam("User-Agent") String userAgent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedMap;

import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;

@SuppressWarnings("unused")
public final class HeaderFillerUtil {

Expand All @@ -27,6 +29,8 @@ public static boolean shouldAddHeader(String name, MultivaluedMap<String, String
// if the header is the Content-Type, then we should update if its current value equals what determined at build time (and therefore no other code has changed it)
return existingValue.equals(defaultContentType);
}
} else if (HttpHeaders.USER_AGENT.equals(name)) {
return RestClientRequestContext.DEFAULT_USER_AGENT_VALUE.equals(existingValue);
} else {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class ClientBuilderImpl extends ClientBuilder {
private MultiQueryParamMode multiQueryParamMode;

private ClientLogger clientLogger = new DefaultClientLogger();
private String userAgent = "Resteasy Reactive Client";
private String userAgent = RestClientRequestContext.DEFAULT_USER_AGENT_VALUE;

private boolean enableCompression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class RestClientRequestContext extends AbstractResteasyReactiveContext<Re
public static final String INVOKED_METHOD_PROP = "org.eclipse.microprofile.rest.client.invokedMethod";
public static final String INVOKED_METHOD_PARAMETERS_PROP = "io.quarkus.rest.client.invokedMethodParameters";
public static final String DEFAULT_CONTENT_TYPE_PROP = "io.quarkus.rest.client.defaultContentType";
public static final String DEFAULT_USER_AGENT_VALUE = "Resteasy Reactive Client";
private static final String TMP_FILE_PATH_KEY = "tmp_file_path";

static final MediaType IGNORED_MEDIA_TYPE = new MediaType("ignored", "ignored");
Expand Down
Loading