Skip to content

Commit

Permalink
Allow @ClientHeaderParam to override User-Agent
Browse files Browse the repository at this point in the history
Fixes: #36329
(cherry picked from commit 134f462)
  • Loading branch information
geoand authored and gsmet committed Oct 11, 2023
1 parent 716c175 commit 24f25ab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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 @@ -74,7 +74,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

0 comments on commit 24f25ab

Please sign in to comment.