-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix providers not auto registered for JAX-RS Client #11448
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
.../src/test/java/io/quarkus/restclient/registerprovider/ProviderClientRegistrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package io.quarkus.restclient.registerprovider; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.Priority; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.Priorities; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.client.ClientRequestFilter; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.UriBuilder; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.resteasy.specimpl.ResteasyUriInfo; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ProviderClientRegistrationTest { | ||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(ProviderClientRegistrationTest.class, ClientBean.class, HelloFooProvider.class)); | ||
|
||
static HttpServer server; | ||
|
||
@Inject | ||
ClientBean client; | ||
|
||
@BeforeAll | ||
static void beforeAll() throws Exception { | ||
// This Server is required, so we don't start a REST Application and test that we are able to add Providers | ||
// without a class annotated with @Path, to a Client: https://github.com/quarkusio/quarkus/issues/11424 | ||
server = HttpServer.create(new InetSocketAddress(0), 0); | ||
server.createContext("/hello", exchange -> { | ||
ResteasyUriInfo uriInfo = new ResteasyUriInfo(exchange.getRequestURI()); | ||
String who = uriInfo.getQueryParameters().getFirst("name"); | ||
String response = "Hello "; | ||
if (who == null) { | ||
response = response + "Undefined"; | ||
} else { | ||
response = response + who; | ||
} | ||
|
||
exchange.sendResponseHeaders(200, response.length()); | ||
exchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN); | ||
exchange.getResponseBody().write(response.getBytes()); | ||
exchange.getResponseBody().flush(); | ||
exchange.getResponseBody().close(); | ||
}); | ||
|
||
server.start(); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
server.stop(0); | ||
} | ||
|
||
@Test | ||
void hello() { | ||
assertEquals("Hello Naruto", client.hello("Naruto")); | ||
} | ||
|
||
@Test | ||
void helloFoo() { | ||
assertEquals("Hello Foo", client.hello()); | ||
} | ||
|
||
// The Client needs to be deployed with the Quarkus app, so it inherits all of the RESTEasy configuration. | ||
@ApplicationScoped | ||
public static class ClientBean { | ||
private WebTarget webTarget; | ||
|
||
@PostConstruct | ||
void init() { | ||
webTarget = ClientBuilder.newClient().target("http://localhost:" + server.getAddress().getPort()).path("/hello"); | ||
; | ||
} | ||
|
||
String hello() { | ||
return hello(null); | ||
} | ||
|
||
String hello(final String name) { | ||
WebTarget webTarget = this.webTarget; | ||
if (name != null) { | ||
webTarget = webTarget.queryParam("name", name); | ||
} | ||
|
||
Response response = webTarget.request(MediaType.TEXT_PLAIN_TYPE).get(Response.class); | ||
|
||
assertEquals(200, response.getStatus()); | ||
return response.readEntity(String.class); | ||
} | ||
} | ||
|
||
@Provider | ||
@Priority(Priorities.USER - 1) | ||
public static class HelloFooProvider implements ClientRequestFilter { | ||
@Override | ||
public void filter(final ClientRequestContext requestContext) { | ||
if (requestContext.getUri().getQuery() == null) { | ||
requestContext.setUri(UriBuilder.fromUri(requestContext.getUri()).queryParam("name", "Foo").build()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't conflict with the server behavior when both are used? /cc @kenfinnigan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. When the server starts (if any) it will replace the instance:
https://github.com/resteasy/Resteasy/blob/1570f6825de1fde1ea3c3a950dcb6297e3c9bd4f/resteasy-core/src/main/java/org/jboss/resteasy/core/ResteasyDeploymentImpl.java#L377
And it will work for both server and client with the right factory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
ResteasyDeploymentImpl
used in Quarkus, for some reason I'm thinking it isn't, but could easily be wrong?If we do use it, then I think it should work fine. But certainly worth verifying
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is used here:
https://github.com/quarkusio/quarkus/blob/5941a39ba4c1eecedd854f74b4d9e688082a73fc/extensions/resteasy-server-common/runtime/src/main/java/io/quarkus/resteasy/server/common/runtime/QuarkusResteasyDeployment.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Famous Last Words™