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

Propagate headers from @ClientHeaderParam when using ClientHeadersFactory #28845

Merged
merged 1 commit into from
Oct 26, 2022
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
@@ -0,0 +1,9 @@
package io.quarkus.rest.client.reactive.registerclientheaders;

public final class ComputedHeader {

public static String get() {
return "From " + ComputedHeader.class.getName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.rest.client.reactive.registerclientheaders;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;

import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient
@RegisterClientHeaders(MyHeadersFactory.class)
@ClientHeaderParam(name = "my-header", value = "constant-header-value")
@ClientHeaderParam(name = "computed-header", value = "{io.quarkus.rest.client.reactive.registerclientheaders.ComputedHeader.get}")
public interface MultipleHeadersBindingClient {
@GET
@Path("/describe-request")
@ClientHeaderParam(name = "header-from-properties", value = "${header.value}")
RequestData call(@HeaderParam("jaxrs-style-header") String headerValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class MyHeadersFactory implements ClientHeadersFactory {
public MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders,
MultivaluedMap<String, String> clientOutgoingHeaders) {
assertNotNull(beanManager);
incomingHeaders.add("foo", "bar");
return incomingHeaders;
clientOutgoingHeaders.add("foo", "bar");
return clientOutgoingHeaders;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.quarkus.rest.client.reactive.registerclientheaders;

import static io.quarkus.rest.client.reactive.RestClientTestUtil.setUrlForClass;
import static io.quarkus.rest.client.reactive.registerclientheaders.HeaderSettingClient.HEADER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
Expand All @@ -26,8 +30,9 @@ public class RegisterClientHeadersTest {
setUrlForClass(HeaderSettingClient.class) +
setUrlForClass(HeaderPassingClient.class) +
setUrlForClass(HeaderNoPassingClient.class) +
"org.eclipse.microprofile.rest.client.propagateHeaders="
+ HeaderSettingClient.HEADER),
setUrlForClass(MultipleHeadersBindingClient.class) +
"org.eclipse.microprofile.rest.client.propagateHeaders=" + HEADER + "\n" +
"header.value=from property file"),
"application.properties");
});

Expand All @@ -37,6 +42,9 @@ public class RegisterClientHeadersTest {
@RestClient
HeaderSettingClient headerSettingClient;

@RestClient
MultipleHeadersBindingClient multipleHeadersBindingClient;

@Test
public void shouldUseHeadersFactory() {
assertEquals("ping:bar", client.echo("ping:"));
Expand All @@ -47,14 +55,30 @@ public void shouldUseHeadersFactory() {
public void shouldPassIncomingHeaders() {
String headerValue = "my-header-value";
RequestData requestData = headerSettingClient.setHeaderValue(headerValue);
assertThat(requestData.getHeaders().get(HeaderSettingClient.HEADER).get(0)).isEqualTo(headerValue);
assertThat(requestData.getHeaders().get(HEADER).get(0)).isEqualTo(headerValue);
}

@Test
public void shouldNotPassIncomingHeaders() {
String headerValue = "my-header-value";
RequestData requestData = headerSettingClient.setHeaderValueNoPassing(headerValue);
assertThat(requestData.getHeaders().get(HeaderSettingClient.HEADER)).isNull();
assertThat(requestData.getHeaders().get(HEADER)).isNull();
}

@Test
public void shouldSetHeadersFromMultipleBindings() {
String headerValue = "my-header-value";
Map<String, List<String>> headers = multipleHeadersBindingClient.call(headerValue).getHeaders();
// Verify: @RegisterClientHeaders(MyHeadersFactory.class)
assertThat(headers.get("foo")).containsExactly("bar");
// Verify: @ClientHeaderParam(name = "my-header", value = "constant-header-value")
assertThat(headers.get("my-header")).containsExactly("constant-header-value");
// Verify: @ClientHeaderParam(name = "computed-header", value = "{...ComputedHeader.get}")
assertThat(headers.get("computed-header")).containsExactly("From " + ComputedHeader.class.getName());
// Verify: @ClientHeaderParam(name = "header-from-properties", value = "${header.value}")
assertThat(headers.get("header-from-properties")).containsExactly("from property file");
// Verify: @HeaderParam("jaxrs-style-header")
assertThat(headers.get("jaxrs-style-header")).containsExactly(headerValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.ws.rs.core.MultivaluedMap;

import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory;
import org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl;
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestContext;
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter;

Expand Down Expand Up @@ -55,12 +54,9 @@ public void filter(ResteasyReactiveClientRequestContext requestContext) {
}
}

if (clientHeadersFactory instanceof DefaultClientHeadersFactoryImpl) {
// When using the default factory, pass the proposed outgoing headers onto the request context.
// Propagation with the default factory will then overwrite any values if required.
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
requestContext.getHeaders().put(headerEntry.getKey(), castToListOfObjects(headerEntry.getValue()));
}
// Propagation with the default factory will then overwrite any values if required.
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
requestContext.getHeaders().put(headerEntry.getKey(), castToListOfObjects(headerEntry.getValue()));
}

if (clientHeadersFactory != null) {
Expand All @@ -82,10 +78,6 @@ public void filter(ResteasyReactiveClientRequestContext requestContext) {
requestContext.getHeaders().put(headerEntry.getKey(), castToListOfObjects(headerEntry.getValue()));
}
}
} else {
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
requestContext.getHeaders().put(headerEntry.getKey(), castToListOfObjects(headerEntry.getValue()));
}
}
}

Expand Down