Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

169 changes: 84 additions & 85 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 @@ -17,6 +17,7 @@
package io.vertx.ext.web.tests;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.net.NetSocket;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
Expand Down Expand Up @@ -1660,67 +1661,67 @@ public void testEndHandler() throws Exception {
@Test
public void testExceptionHandler() throws Exception {
AtomicInteger cnt = new AtomicInteger();
client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").onComplete(TestUtils.onSuccess(req -> {
router.route().handler(rc -> {
rc.addEndHandler(done -> {
if (done.failed()) {
cnt.incrementAndGet();
}
});
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
if (done.failed()) {
cnt.incrementAndGet();
}
});
rc.next();
router.route().handler(rc -> {
rc.addEndHandler(done -> {
if (done.failed()) {
cnt.incrementAndGet();
}
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
if (done.failed()) {
cnt.incrementAndGet();
}
});
rc.next();
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
if (done.failed()) {
cnt.incrementAndGet();
}
});
router.route().handler(rc -> {
req.connection().close();
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
if (done.failed()) {
cnt.incrementAndGet();
}
});
req.end();
}));
rc.next();
});
HttpClientRequest request = client
.request(HttpMethod.GET, server.actualPort(), "localhost", "/path")
.await();
router.route().handler(rc -> {
request.connection().close();
});
request.end();
assertWaitUntil(() -> cnt.get() == 3);
}

// Test that adding a closeHandler doesn't overwrite other ones
@Test
public void testCloseHandler() throws Exception {
AtomicInteger cnt = new AtomicInteger();
client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").onComplete(TestUtils.onSuccess(req -> {
router.route().handler(rc -> {
rc.addEndHandler(done -> {
cnt.incrementAndGet();
});
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
cnt.incrementAndGet();
});
rc.next();
router.route().handler(rc -> {
rc.addEndHandler(done -> {
cnt.incrementAndGet();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
cnt.incrementAndGet();
});
rc.next();
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
cnt.incrementAndGet();
});
router.route().handler(rc -> {
req.connection().close();
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
cnt.incrementAndGet();
});
req.end();
}));
rc.next();
});
HttpClientRequest req = client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").await();
router.route().handler(rc -> {
req.connection().close();
});
req.end();
assertWaitUntil(() -> cnt.get() == 3);
}

Expand All @@ -1730,30 +1731,29 @@ public void testEndHandlerCalledOnce() throws Exception {
AtomicInteger endCnt = new AtomicInteger();
AtomicInteger excCnt = new AtomicInteger();
AtomicInteger closeCnt = new AtomicInteger();
client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").onComplete(TestUtils.onSuccess(req -> {
router.route().handler(rc -> {
rc.addEndHandler(done -> {
excCnt.incrementAndGet();
});
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
endCnt.incrementAndGet();
});
rc.next();
router.route().handler(rc -> {
rc.addEndHandler(done -> {
excCnt.incrementAndGet();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
closeCnt.incrementAndGet();
});
rc.next();
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
endCnt.incrementAndGet();
});
router.route().handler(rc -> {
req.connection().close();
rc.next();
});
router.route().handler(rc -> {
rc.addEndHandler(done -> {
closeCnt.incrementAndGet();
});
req.end();
}));
rc.next();
});
HttpClientRequest req = client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").await();
router.route().handler(rc -> {
req.connection().close();
});
req.end();
assertWaitUntil(() -> endCnt.get() == 1);
assertWaitUntil(() -> excCnt.get() == 1);
assertWaitUntil(() -> closeCnt.get() == 1);
Expand Down Expand Up @@ -2669,22 +2669,21 @@ private void testMissingHostHeader(VertxTestContext testContext, String httpVers
Checkpoint done = testContext.checkpoint();
router.route().handler(rc -> rc.response().end());
NetClient nc = vertx.createNetClient();
nc.connect(SocketAddress.inetSocketAddress(8080, "localhost")).onComplete(TestUtils.onSuccess(so -> {
so.write("GET / " + httpVersion + "\r\n\r\n");
Buffer response = Buffer.buffer();
so.handler(chunk -> {
response.appendBuffer(chunk);
String s = response.toString();
int idx = s.indexOf("\r\n");
if (idx >= 0) {
so.handler(null);
String[] line = s.substring(0, idx).split("\\s+");
assertTrue(line.length >= 3);
assertEquals("" + expectedStatusCode, line[1]);
done.flag();
}
});
}));
NetSocket so = nc.connect(SocketAddress.inetSocketAddress(8080, "localhost")).await();
Buffer response = Buffer.buffer();
so.handler(chunk -> {
response.appendBuffer(chunk);
String s = response.toString();
int idx = s.indexOf("\r\n");
if (idx >= 0) {
so.handler(null);
String[] line = s.substring(0, idx).split("\\s+");
assertTrue(line.length >= 3);
assertEquals("" + expectedStatusCode, line[1]);
done.flag();
}
});
so.write("GET / " + httpVersion + "\r\n\r\n");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.RequestOptions;
Expand All @@ -30,7 +31,6 @@
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.PlatformHandler;
import io.vertx.ext.web.tests.WebTestBase;
import io.vertx.junit5.Checkpoint;
import io.vertx.junit5.VertxTestContext;
import io.vertx.test.core.TestUtils;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -406,7 +406,7 @@ private void sendFileUploadRequest(Buffer fileData,
}

@Test
public void testRoutingContextFailedBeforeFileIsFullyUploaded(VertxTestContext testContext) {
public void testRoutingContextFailedBeforeFileIsFullyUploaded() {
String uploadsDirectory = new File(tempUploads, "failUpload").getPath();
new File(uploadsDirectory).mkdirs();
router.clear();
Expand Down Expand Up @@ -434,34 +434,31 @@ public void testRoutingContextFailedBeforeFileIsFullyUploaded(VertxTestContext t
.setHost("localhost")
.setPort(8080)
.setURI("/upload");
Checkpoint responseLatch = testContext.checkpoint();
client.request(requestOptions).onComplete(TestUtils.onSuccess(req -> {
req.response().onComplete(TestUtils.onSuccess(resp -> {
assertEquals(503, resp.statusCode());
responseLatch.flag();
}));
String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
Buffer buffer = TestUtils.randomBuffer(2048);
req.headers().set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
req.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length()));
req.setChunked(true);
req.write("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"somename\"; filename=\"somefile.dat\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n");
req.write(buffer.getBuffer(0, 1024));
vertx.setPeriodic(50, id -> {
if (stop.get()) {
vertx.cancelTimer(id);
req.write(buffer.getBuffer(0, 1024));
String footer = "\r\n--" + boundary + "--\r\n";
req.end(footer);
}
});
}));

responseLatch.await();
HttpClientRequest req = client.request(requestOptions).await();
String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
Buffer buffer = TestUtils.randomBuffer(2048);
req.headers().set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
req.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length()));
req.setChunked(true);
req.write("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"somename\"; filename=\"somefile.dat\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n");
req.write(buffer.getBuffer(0, 1024));
vertx.setPeriodic(50, id -> {
if (stop.get()) {
vertx.cancelTimer(id);
req.write(buffer.getBuffer(0, 1024));
String footer = "\r\n--" + boundary + "--\r\n";
req.end(footer);
}
});
int resp = req
.response()
.map(r -> r.statusCode())
.await();
assertEquals(503, resp);

assertWaitUntil(() -> vertx.fileSystem().readDirBlocking(uploadsDirectory).isEmpty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void testRegistration(VertxTestContext testContext) throws Exception {
}

@Test
public void testNoOrphanClusteredSubscription(VertxTestContext testContext) throws Exception {
public void testNoOrphanClusteredSubscription() throws Exception {
String addr = "someaddress";
String websocketURI = "/eventbus/websocket";

Expand All @@ -161,16 +161,14 @@ public void testNoOrphanClusteredSubscription(VertxTestContext testContext) thro
.connect(websocketURI)
.compose(v -> bridgeClient.register(addr))
.compose(v -> bridgeClient.unregister(addr))
.onComplete(TestUtils.onSuccess(v -> {
Promise<List<RegistrationInfo>> promise = Promise.promise();
node1.setTimer(1500, l -> {
node1.clusterManager().getRegistrations(addr, promise);
promise.future().onComplete(TestUtils.onSuccess(registrationInfos -> {
assertTrue(registrationInfos == null || registrationInfos.isEmpty());
testContext.completeNow();
}));
});
}));
.await();

Thread.sleep(1500);

Promise<List<RegistrationInfo>> promise = Promise.promise();
node1.clusterManager().getRegistrations(addr, promise);
List<RegistrationInfo> registrationInfos = promise.future().await();
assertTrue(registrationInfos == null || registrationInfos.isEmpty());
}

private static class SlowClusterManager extends WrappedClusterManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ public void testHttp2Push(VertxTestContext testContext) throws Exception {
.compose(req ->
req.pushHandler(push -> {
assertNotNull(push);
push.response().onComplete(TestUtils.onSuccess(resp -> {
resp.body().onComplete(TestUtils.onSuccess(body -> {
push.response()
.compose(HttpClientResponse::body)
.onComplete(TestUtils.onSuccess(body -> {
assertTrue(body.length() > 0);
pushReceived.flag();
}));
}));
}).send()
.expecting(HttpResponseExpectation.SC_OK)
.expecting(resp -> resp.version() == HttpVersion.HTTP_2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,25 @@ protected void testRequestBuffer(HttpMethod method, String path, Consumer<HttpCl
Consumer<Buffer> responseBodyBufferAction) throws Exception {
RequestOptions requestOptions = new RequestOptions().setMethod(method).setPort(8080).setURI(path).setHost("localhost");
Promise<Void> promise = Promise.promise();
client.request(requestOptions).onComplete(TestUtils.onSuccess(req -> {
req.response().onComplete(TestUtils.onSuccess(resp -> {
assertEquals(statusCode, resp.statusCode());
assertEquals(statusMessage, resp.statusMessage());
if (responseAction != null) {
responseAction.accept(resp);
}
if (responseBodyBufferAction == null) {
promise.complete();
} else {
resp.bodyHandler(buff -> {
responseBodyBufferAction.accept(buff);
promise.complete();
});
}
}));
client.request(requestOptions).compose(req -> {
if (requestAction != null) {
requestAction.accept(req);
}
req.end();
return req.send();
}).onComplete(TestUtils.onSuccess(resp -> {
assertEquals(statusCode, resp.statusCode());
assertEquals(statusMessage, resp.statusMessage());
if (responseAction != null) {
responseAction.accept(resp);
}
if (responseBodyBufferAction == null) {
promise.complete();
} else {
resp.bodyHandler(buff -> {
responseBodyBufferAction.accept(buff);
promise.complete();
});
}
}));
promise.future().await();
}
Expand Down
Loading
Loading