-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Julien Ponge <[email protected]>
- Loading branch information
1 parent
5d5bb73
commit 6d6165c
Showing
5 changed files
with
197 additions
and
3 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
177 changes: 177 additions & 0 deletions
177
extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/form/FormTest.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,177 @@ | ||
package io.quarkus.vertx.http.form; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.http.HttpClientResponse; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.core.http.RequestOptions; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.handler.BodyHandler; | ||
|
||
public class FormTest { | ||
|
||
private static final String APP_PROPS = """ | ||
quarkus.http.limits.max-form-fields=10 | ||
quarkus.http.limits.max-form-buffered-bytes=100 | ||
"""; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties") | ||
.addClasses(BeanRegisteringRouteUsingObserves.class)); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@Test | ||
public void testTooManyFields() throws Exception { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
AtomicReference<HttpClientResponse> reference = new AtomicReference<>(); | ||
HttpClient client = vertx.createHttpClient(); | ||
client.request(new RequestOptions().setMethod(HttpMethod.POST).setAbsoluteURI("http://localhost:8081/form")) | ||
.onComplete(ar -> { | ||
var req = ar.result(); | ||
req.setChunked(true); | ||
req.putHeader("content-type", "application/x-www-form-urlencoded"); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < 20; i++) { | ||
if (i > 0) { | ||
sb.append('&'); | ||
} | ||
sb.append("a").append(i).append("=").append("b"); | ||
} | ||
req.write(sb.toString()); | ||
vertx.setTimer(10, id -> { | ||
req.end(); | ||
}); | ||
|
||
req.response().onComplete(rc -> { | ||
reference.set(rc.result()); | ||
latch.countDown(); | ||
}); | ||
}); | ||
latch.await(10, TimeUnit.SECONDS); | ||
Assertions.assertEquals(400, reference.get().statusCode()); | ||
} | ||
|
||
@Test | ||
public void testOkForm() throws Exception { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
AtomicReference<HttpClientResponse> reference = new AtomicReference<>(); | ||
HttpClient client = vertx.createHttpClient(); | ||
client.request(new RequestOptions().setMethod(HttpMethod.POST).setAbsoluteURI("http://localhost:8081/form")) | ||
.onComplete(ar -> { | ||
var req = ar.result(); | ||
req.setChunked(true); | ||
req.putHeader("content-type", "application/x-www-form-urlencoded"); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < 10; i++) { | ||
if (i > 0) { | ||
sb.append('&'); | ||
} | ||
sb.append("a").append(i).append("=").append("b"); | ||
} | ||
req.write(sb.toString()); | ||
vertx.setTimer(10, id -> { | ||
req.end(); | ||
}); | ||
|
||
req.response().onComplete(rc -> { | ||
reference.set(rc.result()); | ||
latch.countDown(); | ||
}); | ||
}); | ||
latch.await(10, TimeUnit.SECONDS); | ||
Assertions.assertEquals(200, reference.get().statusCode()); | ||
} | ||
|
||
@Test | ||
public void testTooManyBytes() throws Exception { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
AtomicReference<HttpClientResponse> reference = new AtomicReference<>(); | ||
HttpClient client = vertx.createHttpClient(); | ||
client.request(new RequestOptions().setMethod(HttpMethod.POST).setAbsoluteURI("http://localhost:8081/form")) | ||
.onComplete(ar -> { | ||
var req = ar.result(); | ||
req.setChunked(true); | ||
req.putHeader("content-type", "application/x-www-form-urlencoded"); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < 200; i++) { | ||
sb.append("a"); | ||
} | ||
req.write(sb.toString()); | ||
vertx.setTimer(10, id -> { | ||
req.end("=b"); | ||
}); | ||
|
||
req.response().onComplete(rc -> { | ||
reference.set(rc.result()); | ||
latch.countDown(); | ||
}); | ||
}); | ||
latch.await(10, TimeUnit.SECONDS); | ||
Assertions.assertEquals(400, reference.get().statusCode()); | ||
} | ||
|
||
@Test | ||
public void testBytesOk() throws Exception { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
AtomicReference<HttpClientResponse> reference = new AtomicReference<>(); | ||
HttpClient client = vertx.createHttpClient(); | ||
client.request(new RequestOptions().setMethod(HttpMethod.POST).setAbsoluteURI("http://localhost:8081/form")) | ||
.onComplete(ar -> { | ||
var req = ar.result(); | ||
req.setChunked(true); | ||
req.putHeader("content-type", "application/x-www-form-urlencoded"); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < 100; i++) { | ||
sb.append("a"); | ||
} | ||
req.write(sb.toString()); | ||
vertx.setTimer(10, id -> { | ||
req.end("=b"); | ||
}); | ||
|
||
req.response().onComplete(rc -> { | ||
reference.set(rc.result()); | ||
latch.countDown(); | ||
}); | ||
}); | ||
latch.await(10, TimeUnit.SECONDS); | ||
Assertions.assertEquals(200, reference.get().statusCode()); | ||
} | ||
|
||
@ApplicationScoped | ||
static class BeanRegisteringRouteUsingObserves { | ||
|
||
public void register(@Observes Router router) { | ||
|
||
router | ||
.post().order(Integer.MIN_VALUE).handler(rc -> { | ||
rc.request().setExpectMultipart(true); | ||
rc.next(); | ||
}); | ||
router.post().handler(BodyHandler.create()); | ||
router.post("/form") | ||
.handler(rc -> { | ||
rc.response().end("OK"); | ||
}); | ||
} | ||
|
||
} | ||
} |
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