routingContext.response().setStatusMessage("OK").end(); }); if (failure instanceof ValidationException) routingContext.response() .setStatusCode(400) .setStatusMessage("ValidationException thrown! " + ((ValidationException) failure).type().name()) .end(); });
private void testStatusCode(int code, String statusMessage) { server.requestHandler(req -> { if (code != -1) { req.response().setStatusCode(code); } if (statusMessage != null) { req.response().setStatusMessage(statusMessage); } req.response().end(); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { int theCode; if (code == -1) { // Default code - 200 assertEquals(200, resp.statusCode()); theCode = 200; } else { theCode = code; } if (statusMessage != null && resp.version() != HttpVersion.HTTP_2) { assertEquals(statusMessage, resp.statusMessage()); } else { assertEquals(HttpResponseStatus.valueOf(theCode).reasonPhrase(), resp.statusMessage()); } testComplete(); })).end(); })); await(); }
@Test public void testSubRouteRegex() throws Exception { Router subRouter = Router.router(vertx); router.routeWithRegex("/foo/.*").handler(subRouter::handleContext).failureHandler(subRouter::handleFailure); subRouter.route("/blah").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/foo/blah", 500, "Internal Server Error"); }
@Test public void testPathParamsAreFulfilled() throws Exception { router.route("/blah/:abc/quux/:def/eep/:ghi").handler(rc -> { Map<String, String> params = rc.pathParams(); rc.response().setStatusMessage(params.get("abc") + params.get("def") + params.get("ghi")).end(); }); testPattern("/blah/tim/quux/julien/eep/nick", "timjuliennick"); }
@Test public void testFailureinHandlingFailureWithInvalidStatusMessage() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> frc.response().setStatusMessage("Hello\nWorld").end()); testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Test public void testRegex3() throws Exception { router.routeWithRegex(".*foo.txt").handler(rc -> rc.response().setStatusMessage("ok").end()); testPattern("/dog/cat/foo.txt", "ok"); testRequest(HttpMethod.POST, "/dog/cat/foo.bar", 404, "Not Found"); }
@Test public void testFailureHandler2() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }); router.route("/bl*").failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); testRequest(HttpMethod.GET, path, 555, "oh dear"); }
@Test public void testFailureHandlerNoMatch() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }); router.route("/other").failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); // Default failure response testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Test public void testFailureHandler1() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); testRequest(HttpMethod.GET, path, 555, "oh dear"); }
@Test public void testDecodingErrorCustomHandler() throws Exception { String BAD_PARAM = "~!@\\||$%^&*()_=-%22;;%27%22:%3C%3E/?]}{"; router.errorHandler(400, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end()); router.route().handler(rc -> rc.next()); router.route("/path").handler(rc -> rc.response().setStatusCode(500).end()); testRequest(HttpMethod.GET, "/path?q=" + BAD_PARAM, 500,"Dumb"); }
@Test public void testProducesTopLevelTypeWildcard() throws Exception { router.route().produces("application/json").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "*/json", 200, "application/json"); testRequestWithAccepts(HttpMethod.GET, "/foo", "*/html", 404, "Not Found"); }
@Test public void testAcceptsMultiple2() throws Exception { router.route().produces("application/json").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "text/html,application/*,text/plain", 200, "application/json"); testRequestWithAccepts(HttpMethod.GET, "/foo", "text/html;a,application/*,text/plain", 200, "application/json"); }
@Test public void testAcceptsMultipleWithParams() throws Exception { router.route().produces("application/json").produces("text/plain").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "text/html,text/plain;q=0.9,application/json;q=0.8", 200, "text/plain"); }
@Test public void testProducesMissingSlash() throws Exception { // will assume "*/json" router.route().produces("application/json").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "json", 200, "application/json"); testRequestWithAccepts(HttpMethod.GET, "/foo", "text", 404, "Not Found"); }
@Test public void testProducesSubtypeWildcard() throws Exception { router.route().produces("text/html").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "text/*", 200, "text/html"); testRequestWithAccepts(HttpMethod.GET, "/foo", "application/*", 404, "Not Found"); }
@Test public void testAcceptsMultiple1() throws Exception { router.route().produces("application/json").handler(rc -> { rc.response().setStatusMessage(rc.getAcceptableContentType()); rc.response().end(); }); testRequestWithAccepts(HttpMethod.GET, "/foo", "text/html,application/json,text/plain", 200, "application/json"); testRequestWithAccepts(HttpMethod.GET, "/foo", "text/html,application/json;b,text/plain", 200, "application/json"); }