Package io.jooby.test

Class MockRouter

java.lang.Object
io.jooby.test.MockRouter

public class MockRouter extends Object
Utility class that allows us to execute routes using a MockContext.

App.java


 {

   get("/", ctx -> "OK");

 }
 
UnitTest:

 MockRouter router = new MockRouter(new App());

 assertEquals("OK", router.get("/"));
 
Since:
2.0.0
Author:
edgar
  • Constructor Details

    • MockRouter

      public MockRouter(@NonNull io.jooby.Jooby application)
      Creates a new mock router.
      Parameters:
      application - Source application.
  • Method Details

    • setSession

      @NonNull public MockRouter setSession(@NonNull MockSession session)
      Set a global session. So all route invocations are going to shared the same session as long as they don't use a custom context per invocation.
      Parameters:
      session - Global session.
      Returns:
      This router.
    • getWorker

      public Executor getWorker()
      Get the worker executor for running test.
      Returns:
      Worker executor or null.
    • setWorker

      public MockRouter setWorker(@NonNull Executor worker)
      Set the worker executor to use.
      Parameters:
      worker - Worker executor.
      Returns:
      This router.
    • get

      @NonNull public MockValue get(@NonNull String path)
      Execute a GET request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      Returns:
      Route response.
    • ws

      public MockWebSocketClient ws(@NonNull String path, Consumer<MockWebSocketClient> callback)
      Execute a GET request and find a websocket, perform the upgrade and produces a websocket client.

      App:

      
       ws("/path", (ctx, initializer) -> {
         initializer.onConnect(ws -> {
           ws.send("OnConnect");
         });
       });
       
      Test:
      
       MockRouter router = new MockRouter(new App());
       router.ws("/path", ws -> {
      
         ws.onMessage(message -> {
           System.out.println("Got: " + message);
         });
      
         ws.send("Another message");
       })
       
      Parameters:
      path - Path to match.
      callback - Websocket client callback.
      Returns:
      Web socket client.
    • get

      @NonNull public MockValue get(@NonNull String path, @NonNull io.jooby.Context context)
      Execute a GET request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      Returns:
      Route response.
    • get

      public MockValue get(@NonNull String path, @NonNull Consumer<MockResponse> consumer)
      Execute a GET request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • get

      public MockValue get(@NonNull String path, @NonNull MockContext context, @NonNull Consumer<MockResponse> consumer)
      Execute a GET request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • post

      public MockValue post(@NonNull String path)
      Execute a POST request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      Returns:
      Route response.
    • post

      @NonNull public MockValue post(@NonNull String path, @NonNull io.jooby.Context context)
      Execute a POST request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      Returns:
      Route response.
    • post

      public MockValue post(@NonNull String path, @NonNull Consumer<MockResponse> consumer)
      Execute a POST request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • post

      public MockValue post(@NonNull String path, @NonNull MockContext context, @NonNull Consumer<MockResponse> consumer)
      Execute a POST request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • delete

      public MockValue delete(@NonNull String path)
      Execute a DELETE request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      Returns:
      Route response.
    • delete

      @NonNull public MockValue delete(@NonNull String path, @NonNull io.jooby.Context context)
      Execute a DELETE request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      Returns:
      Route response.
    • delete

      public MockValue delete(@NonNull String path, @NonNull Consumer<MockResponse> consumer)
      Execute a DELETE request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • delete

      public MockValue delete(@NonNull String path, @NonNull MockContext context, @NonNull Consumer<MockResponse> consumer)
      Execute a DELETE request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • put

      public MockValue put(@NonNull String path)
      Execute a PUT request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      Returns:
      Route response.
    • put

      @NonNull public MockValue put(@NonNull String path, @NonNull io.jooby.Context context)
      Execute a PUT request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      Returns:
      Route response.
    • put

      public MockValue put(@NonNull String path, @NonNull Consumer<MockResponse> consumer)
      Execute a PUT request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • put

      public MockValue put(@NonNull String path, @NonNull MockContext context, @NonNull Consumer<MockResponse> consumer)
      Execute a PUT request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • patch

      public MockValue patch(@NonNull String path)
      Execute a PATCH request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      Returns:
      Route response.
    • patch

      @NonNull public MockValue patch(@NonNull String path, @NonNull io.jooby.Context context)
      Execute a PATCH request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      Returns:
      Route response.
    • patch

      public MockValue patch(@NonNull String path, @NonNull Consumer<MockResponse> consumer)
      Execute a PATCH request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • patch

      public MockValue patch(@NonNull String path, @NonNull MockContext context, @NonNull Consumer<MockResponse> consumer)
      Execute a PATCH request to the target application.
      Parameters:
      path - Path to match. Might includes the queryString.
      context - Context to use.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • call

      public MockValue call(@NonNull String method, @NonNull String path, @NonNull io.jooby.Context context)
      Execute a PATCH request to the target application.
      Parameters:
      method - HTTP method.
      path - Path to match. Might includes the queryString.
      context - Web context.
      Returns:
      Route response.
    • call

      public MockValue call(@NonNull String method, @NonNull String path, @NonNull Consumer<MockResponse> consumer)
      Execute a PATCH request to the target application.
      Parameters:
      method - HTTP method.
      path - Path to match. Might includes the queryString.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • call

      public MockValue call(@NonNull String method, @NonNull String path, @NonNull MockContext ctx, @NonNull Consumer<MockResponse> consumer)
      Execute a PATCH request to the target application.
      Parameters:
      method - HTTP method.
      path - Path to match. Might includes the queryString.
      ctx - Context to use.
      consumer - Response metadata callback.
      Returns:
      Route response.
    • setFullExecution

      public MockRouter setFullExecution(boolean enabled)
      Set whenever to execute the entire pipeline (decorators + handler) or just the handler. This flag is off by default, so only the handlers is executed.
      Parameters:
      enabled - True for enabled the entire pipeline.
      Returns:
      This mock router.