Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support for ending RoutingContext with statuscode #2147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions vertx-web/src/main/java/io/vertx/ext/web/RoutingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,4 +907,22 @@ default RoutingContext end(Handler<AsyncResult<Void>> handler) {
end().onComplete(handler);
return this;
}

/**
* Shortcut to the response end with a given status code.
* @return future
*/
default Future<Void> end(int statusCode) {
return response().setStatusCode(statusCode).end();
}

/**
* Shortcut to the response end with a given status code.
* See {@link #end(int)}
*/
@Fluent
default RoutingContext end(int statusCode, Handler<AsyncResult<Void>> handler) {
end(statusCode).onComplete(handler);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.WebTestBase;
import io.vertx.ext.web.handler.BodyHandler;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.AfterClass;
import org.junit.Test;

Expand Down Expand Up @@ -307,4 +310,18 @@ public void test_get_or_default() throws Exception {
});
testRequest(HttpMethod.GET, "/", HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase());
}

@Test
public void test_end_with_statuscode() throws Exception {
router.route().handler(ctx -> ctx.end(202));
testRequest(HttpMethod.POST, "/", HttpResponseStatus.ACCEPTED.code(), HttpResponseStatus.ACCEPTED.reasonPhrase());
}

@Test
public void test_end_with_statuscode_handler() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
router.route().handler(ctx -> ctx.end(202, v -> latch.countDown()));
testRequest(HttpMethod.POST, "/", HttpResponseStatus.ACCEPTED.code(), HttpResponseStatus.ACCEPTED.reasonPhrase());
awaitLatch(latch);
}
}