forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take ParamConverterProvider into account for form params in REST Clie…
…nt Reactive Fixes: quarkusio#23034
- Loading branch information
Showing
2 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
.../deployment/src/test/java/io/quarkus/rest/client/reactive/form/FormWithConverterTest.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,91 @@ | ||
package io.quarkus.rest.client.reactive.form; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.FormParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.ext.ParamConverter; | ||
import javax.ws.rs.ext.ParamConverterProvider; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
|
||
public class FormWithConverterTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(ResourceClient.class, Resource.class, Input.class, InputParamConverterProvider.class)); | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@Test | ||
void test() { | ||
ResourceClient client = RestClientBuilder.newBuilder().baseUri(baseUri).register(InputParamConverterProvider.class) | ||
.build(ResourceClient.class); | ||
String result = client.hello(new Input("hey")); | ||
assertThat(result).isEqualTo("hey!"); | ||
} | ||
|
||
public interface ResourceClient { | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String hello(@FormParam("message") Input input); | ||
} | ||
|
||
@Path("") | ||
public static class Resource { | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String hello(@FormParam("message") String message) { | ||
return message + "!"; | ||
} | ||
} | ||
|
||
public static class Input { | ||
|
||
public String value; | ||
|
||
public Input(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static class InputParamConverterProvider implements ParamConverterProvider { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { | ||
if (rawType == Input.class) { | ||
return (ParamConverter<T>) new ParamConverter<Input>() { | ||
@Override | ||
public Input fromString(String value) { | ||
return new Input(value); | ||
} | ||
|
||
@Override | ||
public String toString(Input value) { | ||
return value.value; | ||
} | ||
}; | ||
} | ||
return null; | ||
} | ||
} | ||
} |