private void testRouteWithHandler(Route innerRoute) { TestRoute route = testRoute(innerRoute); route.run(HttpRequest.GET("/test")).assertStatusCode(200); route .run(HttpRequest.GET("/other")) .assertStatusCode(429) .assertEntity("Too many requests for busy path!"); }
@Test public void testCatchExceptionThrownFromHandler() { Parameter<Integer> a = Parameters.intValue("a"); Parameter<Integer> b = Parameters.intValue("b"); Handler2<Integer, Integer> divide = new Handler2<Integer, Integer>() { @Override public RouteResult apply(RequestContext ctx, Integer a, Integer b) { int result = a / b; return ctx.complete("The result is: " + result); } }; ExceptionHandler handleDivByZero = new ExceptionHandler() { @Override public Route handle(RuntimeException exception) { try { throw exception; } catch (ArithmeticException t) { return complete( HttpResponse.create() .withStatus(400) .withEntity("Congratulations you provoked a division by zero!")); } } }; TestRoute route = testRoute( handleExceptions(handleDivByZero, path("divide").route(handleWith2(a, b, divide)))); route.run(HttpRequest.GET("/divide?a=10&b=5")).assertEntity("The result is: 2"); route .run(HttpRequest.GET("/divide?a=10&b=0")) .assertStatusCode(400) .assertEntity("Congratulations you provoked a division by zero!"); }
@Test public void testHandleMethodRejection() { RejectionHandler rejectionHandler = new RejectionHandler() { @Override public RouteResult handleMethodRejection(RequestContext ctx, HttpMethod supported) { return ctx.complete( HttpResponse.create() .withStatus(400) .withEntity( "Whoopsie! Unsupported method. Supported would have been " + supported.value())); } }; TestRoute route = testRoute(handleRejections(rejectionHandler, get(complete("Successful!")))); route.run(HttpRequest.GET("/")).assertStatusCode(200).assertEntity("Successful!"); route .run(HttpRequest.POST("/")) .assertStatusCode(400) .assertEntity("Whoopsie! Unsupported method. Supported would have been GET"); }
public static void client(int port, ActorSystem system) { final ActorMaterializer materializer = ActorMaterializer.create(system); // Todo display in logger Http.get(system).singleRequest(HttpRequest.create("localhost:" + port), materializer); }