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.
Deprecate
@ServerRequestFilter(withBody)
in favour of @WithFormRead
You can also place it on endpoint methods. Fixes quarkusio#22444
- Loading branch information
Showing
18 changed files
with
311 additions
and
61 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
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
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
119 changes: 119 additions & 0 deletions
119
...arkus/resteasy/reactive/server/test/customproviders/ImpliedReadBodyRequestFilterTest.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,119 @@ | ||
package io.quarkus.resteasy.reactive.server.test.customproviders; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.HttpHeaders; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.resteasy.reactive.RestForm; | ||
import org.jboss.resteasy.reactive.RestQuery; | ||
import org.jboss.resteasy.reactive.server.ServerRequestFilter; | ||
import org.jboss.resteasy.reactive.server.WithFormRead; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestContext; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class ImpliedReadBodyRequestFilterTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloResource.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testMethodWithBody() { | ||
RestAssured.with() | ||
.formParam("name", "Quarkus") | ||
.post("/hello") | ||
.then().body(Matchers.equalTo("hello Quarkus!!!!!!!")); | ||
} | ||
|
||
@Test | ||
public void testMethodWithUndeclaredBody() { | ||
RestAssured.with() | ||
.formParam("name", "Quarkus") | ||
.post("/hello/empty") | ||
.then().body(Matchers.equalTo("hello !!!!!!!")); | ||
} | ||
|
||
@Test | ||
public void testMethodWithStringBody() { | ||
// make sure that a form-reading filter doesn't prevent non-form request bodies from being deserialised | ||
RestAssured.with() | ||
.formParam("name", "Quarkus") | ||
.post("/hello/string") | ||
.then().body(Matchers.equalTo("hello name=Quarkus!!!!!!!")); | ||
RestAssured.with() | ||
.body("Quarkus") | ||
.post("/hello/string") | ||
.then().body(Matchers.equalTo("hello Quarkus?")); | ||
} | ||
|
||
@Test | ||
public void testMethodWithoutBody() { | ||
RestAssured.with() | ||
.queryParam("name", "Quarkus") | ||
.get("/hello") | ||
.then().body(Matchers.equalTo("hello Quarkus!")); | ||
} | ||
|
||
@Path("hello") | ||
public static class HelloResource { | ||
|
||
@POST | ||
public String helloPost(@RestForm String name, HttpHeaders headers) { | ||
return "hello " + name + headers.getHeaderString("suffix"); | ||
} | ||
|
||
@Path("empty") | ||
@POST | ||
public String helloEmptyPost(HttpHeaders headers) { | ||
return "hello " + headers.getHeaderString("suffix"); | ||
} | ||
|
||
@Path("string") | ||
@POST | ||
public String helloStringPost(String body, HttpHeaders headers) { | ||
return "hello " + body + headers.getHeaderString("suffix"); | ||
} | ||
|
||
@GET | ||
public String helloGet(@RestQuery String name, HttpHeaders headers) { | ||
return "hello " + name + headers.getHeaderString("suffix"); | ||
} | ||
} | ||
|
||
public static class Filters { | ||
|
||
@WithFormRead | ||
@ServerRequestFilter | ||
public void addSuffix(ResteasyReactiveContainerRequestContext containerRequestContext) { | ||
ResteasyReactiveRequestContext rrContext = (ResteasyReactiveRequestContext) containerRequestContext | ||
.getServerRequestContext(); | ||
if (containerRequestContext.getMethod().equals("POST")) { | ||
String nameFormParam = (String) rrContext.getFormParameter("name", true, false); | ||
if (nameFormParam != null) { | ||
containerRequestContext.getHeaders().putSingle("suffix", "!".repeat(nameFormParam.length())); | ||
} else { | ||
containerRequestContext.getHeaders().putSingle("suffix", "?"); | ||
} | ||
} else { | ||
containerRequestContext.getHeaders().putSingle("suffix", "!"); | ||
} | ||
} | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
.../test/java/io/quarkus/resteasy/reactive/server/test/customproviders/WithFormBodyTest.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,64 @@ | ||
package io.quarkus.resteasy.reactive.server.test.customproviders; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.resteasy.reactive.RestForm; | ||
import org.jboss.resteasy.reactive.server.WithFormRead; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class WithFormBodyTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(HelloResource.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testMethodWithBody() { | ||
RestAssured.with() | ||
.formParam("name", "Quarkus") | ||
.post("/hello") | ||
.then().body(Matchers.equalTo("hello Quarkus")); | ||
} | ||
|
||
@Test | ||
public void testMethodWithUndeclaredBody() { | ||
RestAssured.with() | ||
.formParam("name", "Quarkus") | ||
.post("/hello/empty") | ||
.then().body(Matchers.equalTo("hello Quarkus")); | ||
} | ||
|
||
@Path("hello") | ||
public static class HelloResource { | ||
|
||
@POST | ||
public String helloPost(@RestForm String name) { | ||
return "hello " + name; | ||
} | ||
|
||
@WithFormRead | ||
@Path("empty") | ||
@POST | ||
public String helloEmptyPost(ServerRequestContext requestContext) { | ||
return "hello " + ((ResteasyReactiveRequestContext) requestContext).getFormParameter("name", true, false); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.