Skip to content

Commit

Permalink
vert-x3#2663 Add test for accepted content type headers in case of a 415
Browse files Browse the repository at this point in the history
  • Loading branch information
komape committed Oct 30, 2024
1 parent 0bba15e commit 66f713e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
27 changes: 13 additions & 14 deletions vertx-web/src/test/java/io/vertx/ext/web/tests/RouterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import java.util.stream.Collectors;

import static io.vertx.core.Future.succeededFuture;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -1044,8 +1043,8 @@ public void testRegexWithNamedParamsKeepsIndexedParams() throws Exception {
public void testConsumes() throws Exception {
router.route().consumes("text/html").handler(rc -> rc.response().end());
testRequestWithContentType(HttpMethod.GET, "/foo", "text/html", 200, "OK");
testRequestWithContentType(HttpMethod.GET, "/foo", "text/json", 415, "Unsupported Media Type");
testRequestWithContentType(HttpMethod.GET, "/foo", "something/html", 415, "Unsupported Media Type");
testRequestWithContentTypeAndResponseHeader(HttpMethod.GET, "/foo", "text/json", 415, "Unsupported Media Type", Map.of("Accept", "text/html"));
testRequestWithContentTypeAndResponseHeader(HttpMethod.GET, "/foo", "something/html", 415, "Unsupported Media Type", Map.of("Accept", "text/html"));
}

@Test
Expand Down Expand Up @@ -1469,7 +1468,7 @@ public void testGetRoutesRecursive() throws Exception {
router.route("/sub/*").subRouter(subRouter);
List<Route> routes = router.getRoutes();
assertEquals(2, routes.size());
for(Route route: routes) {
for (Route route : routes) {
Router theSubRouter = route.getSubRouter();
if (route.getPath().equals("/sub/")) {
assertNotNull(theSubRouter);
Expand Down Expand Up @@ -2446,8 +2445,8 @@ public void testMultipleHandlersMultipleConnectionsDelayedMixed() throws Excepti
// using executeBlocking should create multiple connections
vertx.executeBlocking((new Random().nextBoolean() ? execute200Request : execute400Request), false)
.onComplete(onSuccess(v -> {
latch.countDown();
}));
latch.countDown();
}));
}
awaitLatch(latch);
}
Expand Down Expand Up @@ -3538,12 +3537,12 @@ public void testPausedConnection() {
ctx.response().end(ctx.normalizedPath() + "\n");
});

int numRequests= 20;
int numRequests = 20;

waitFor(numRequests);

HttpClient client = vertx.createHttpClient(new PoolOptions().setHttp1MaxSize(1));
for (int i = 0;i < numRequests;i++) {
for (int i = 0; i < numRequests; i++) {
client.request(new RequestOptions().setMethod(HttpMethod.PUT).setPort(8080)).onComplete(onSuccess(req -> {
// 8192 * 8 fills the HTTP server request pending queue
// => pauses the HttpConnection (see Http1xServerRequest#handleContent(Buffer) that calls Http1xServerConnection#doPause())
Expand Down Expand Up @@ -3573,12 +3572,12 @@ public void testPausedConnection2() {
router.route("/")
.handler(BodyHandler.create());

int numRequests= 20;
int numRequests = 20;

waitFor(numRequests);

HttpClient client = vertx.createHttpClient(new PoolOptions().setHttp1MaxSize(1));
for (int i = 0;i < numRequests;i++) {
for (int i = 0; i < numRequests; i++) {
client.request(new RequestOptions().setMethod(HttpMethod.PUT).setPort(8080)).onComplete(onSuccess(req -> {
// 8192 * 8 fills the HTTP server request pending queue
// => pauses the HttpConnection (see Http1xServerRequest#handleContent(Buffer) that calls Http1xServerConnection#doPause())
Expand Down Expand Up @@ -3611,12 +3610,12 @@ public void testPausedConnection3() {
ctx.request().endHandler(done -> ctx.response().end(ctx.normalizedPath() + "\n"));
});

int numRequests= 20;
int numRequests = 20;

waitFor(numRequests);

HttpClient client = vertx.createHttpClient(new PoolOptions().setHttp1MaxSize(1));
for (int i = 0;i < numRequests;i++) {
for (int i = 0; i < numRequests; i++) {
client.request(new RequestOptions().setMethod(HttpMethod.PUT).setPort(8080)).onComplete(onSuccess(req -> {
// 8192 * 8 fills the HTTP server request pending queue
// => pauses the HttpConnection (see Http1xServerRequest#handleContent(Buffer) that calls Http1xServerConnection#doPause())
Expand All @@ -3639,12 +3638,12 @@ public void testPausedConnection4() {
.setTimer(1L, t -> ctx.next());
});

int numRequests= 20;
int numRequests = 20;

waitFor(numRequests);

HttpClient client = vertx.createHttpClient(new PoolOptions().setHttp1MaxSize(1));
for (int i = 0;i < numRequests;i++) {
for (int i = 0; i < numRequests; i++) {
client.request(new RequestOptions().setMethod(HttpMethod.PUT).setPort(8080)).onComplete(onSuccess(req -> {
// 8192 * 8 fills the HTTP server request pending queue
// => pauses the HttpConnection (see Http1xServerRequest#handleContent(Buffer) that calls Http1xServerConnection#doPause())
Expand Down
12 changes: 8 additions & 4 deletions vertx-web/src/test/java/io/vertx/ext/web/tests/WebTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;

Expand Down Expand Up @@ -140,6 +137,10 @@ protected void testRequestWithContentType(HttpMethod method, String path, String
testRequest(method, path, req -> req.putHeader("content-type", contentType), statusCode, statusMessage, null);
}

protected void testRequestWithContentTypeAndResponseHeader(HttpMethod method, String path, String contentType, int statusCode, String statusMessage, Map<String, String> headers) throws Exception {
testRequest(method, path, req -> req.putHeader("content-type", contentType), res -> headers.forEach((key, val) -> assertEquals(val, res.getHeader(key))), statusCode, statusMessage, null);
}

protected void testRequestWithAccepts(RequestOptions requestOptions, String accepts, int statusCode, String statusMessage) throws Exception {
testRequest(requestOptions, req -> req.putHeader("accept", accepts), statusCode, statusMessage, null);
}
Expand All @@ -161,6 +162,7 @@ protected void testRequest(RequestOptions requestOptions, Consumer<HttpClientReq
String responseBody) throws Exception {
testRequest(requestOptions, requestAction, null, statusCode, statusMessage, responseBody);
}

protected void testRequest(HttpMethod method, String path, Consumer<HttpClientRequest> requestAction,
int statusCode, String statusMessage,
String responseBody) throws Exception {
Expand All @@ -172,6 +174,7 @@ protected void testRequest(RequestOptions requestOptions, Consumer<HttpClientReq
String responseBody) throws Exception {
testRequestBuffer(requestOptions, requestAction, responseAction, statusCode, statusMessage, responseBody != null ? Buffer.buffer(responseBody) : null, true);
}

protected void testRequest(HttpMethod method, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
String responseBody) throws Exception {
Expand All @@ -183,6 +186,7 @@ protected void testRequestBuffer(RequestOptions requestOptions, Consumer<HttpCli
Buffer responseBodyBuffer) throws Exception {
testRequestBuffer(requestOptions, requestAction, responseAction, statusCode, statusMessage, responseBodyBuffer, false);
}

protected void testRequestBuffer(HttpMethod method, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
Buffer responseBodyBuffer) throws Exception {
Expand Down

0 comments on commit 66f713e

Please sign in to comment.