Skip to content

Commit

Permalink
Allow setting the user agent programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Feb 15, 2024
1 parent 754f4e4 commit 60c682a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.rest.client.reactive.headers;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.QuarkusRestClientBuilder;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;

public class UserAgentProgrammaticTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(Resource.class, Client.class))
.overrideRuntimeConfigKey("quarkus.rest-client.user-agent", "from-config");

@TestHTTPResource
URI baseUri;

@Test
void test() {
Client client = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).userAgent("programmatic").build(Client.class);
assertThat(client.call()).isEqualTo("programmatic");

Client client2 = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).userAgent("programmatic2").build(Client.class);
assertThat(client2.call()).isEqualTo("programmatic2");

Client client3 = QuarkusRestClientBuilder.newBuilder().baseUri(baseUri).build(Client.class);
assertThat(client3.call()).isEqualTo("from-config");
}

@Path("/")
@ApplicationScoped
public static class Resource {
@GET
public String returnHeaders(@HeaderParam("user-agent") String header) {
return header;
}
}

public interface Client {

@Path("/")
@GET
String call();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ static QuarkusRestClientBuilder newBuilder() {
*/
QuarkusRestClientBuilder trustAll(boolean trustAll);

/**
* Set the User-Agent header to be used
*/
QuarkusRestClientBuilder userAgent(String userAgent);

/**
* Based on the configured QuarkusRestClientBuilder, creates a new instance of the given REST interface to invoke API calls
* against.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ public QuarkusRestClientBuilder trustAll(boolean trustAll) {
return this;
}

@Override
public QuarkusRestClientBuilder userAgent(String userAgent) {
proxy.userAgent(userAgent);
return this;
}

@Override
public <T> T build(Class<T> clazz) throws IllegalStateException, RestClientDefinitionException {
return proxy.build(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class RestClientBuilderImpl implements RestClientBuilder {
private Integer loggingBodyLimit;

private Boolean trustAll;
private String userAgent;

@Override
public RestClientBuilderImpl baseUrl(URL url) {
Expand Down Expand Up @@ -186,6 +187,11 @@ public RestClientBuilderImpl trustAll(boolean trustAll) {
return this;
}

public RestClientBuilderImpl userAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}

@Override
public RestClientBuilderImpl executorService(ExecutorService executor) {
throw new IllegalArgumentException("Specifying executor service is not supported. " +
Expand Down Expand Up @@ -386,10 +392,10 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
clientBuilder.trustAll(effectiveTrustAll);
restClientsConfig.verifyHost.ifPresent(clientBuilder::verifyHost);

String userAgent = (String) getConfiguration().getProperty(QuarkusRestClientProperties.USER_AGENT);
if (userAgent != null) {
clientBuilder.setUserAgent(userAgent);
} else if (restClientsConfig.userAgent.isPresent()) {
String effectiveUserAgent = userAgent;
if (effectiveUserAgent != null) {
clientBuilder.setUserAgent(effectiveUserAgent);
} else if (restClientsConfig.userAgent.isPresent()) { // if config set and client obtained programmatically
clientBuilder.setUserAgent(restClientsConfig.userAgent.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void configureCustomProperties(QuarkusRestClientBuilder builder) {
Optional<String> userAgent = oneOf(clientConfigByClassName().userAgent,
clientConfigByConfigKey().userAgent, configRoot.userAgent);
if (userAgent.isPresent()) {
builder.property(QuarkusRestClientProperties.USER_AGENT, userAgent.get());
builder.userAgent(userAgent.get());
}

Optional<Integer> maxChunkSize = oneOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testClientSpecificConfigs() {
Mockito.verify(restClientBuilderMock).nonProxyHosts("nonProxyHosts1");
Mockito.verify(restClientBuilderMock).connectTimeout(100, TimeUnit.MILLISECONDS);
Mockito.verify(restClientBuilderMock).readTimeout(101, TimeUnit.MILLISECONDS);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.USER_AGENT, "agent1");
Mockito.verify(restClientBuilderMock).userAgent("agent1");
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.STATIC_HEADERS,
Collections.singletonMap("header1", "value"));
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_TTL, 10); // value converted to seconds
Expand Down Expand Up @@ -152,7 +152,7 @@ public void testGlobalConfigs() {
Mockito.verify(restClientBuilderMock).nonProxyHosts("nonProxyHosts2");
Mockito.verify(restClientBuilderMock).connectTimeout(200, TimeUnit.MILLISECONDS);
Mockito.verify(restClientBuilderMock).readTimeout(201, TimeUnit.MILLISECONDS);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.USER_AGENT, "agent2");
Mockito.verify(restClientBuilderMock).userAgent("agent2");
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.STATIC_HEADERS,
Collections.singletonMap("header2", "value"));
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_TTL, 20);
Expand Down

0 comments on commit 60c682a

Please sign in to comment.