Class Soklet

java.lang.Object
com.soklet.Soklet
All Implemented Interfaces:
AutoCloseable

@ThreadSafe public final class Soklet extends Object implements AutoCloseable
Soklet's main class - manages a Server (and optionally a ServerSentEventServer) using the provided system configuration.

// Use out-of-the-box defaults
SokletConfig config = SokletConfig.withServer(
  Server.withPort(8080).build()
).build();

try (Soklet soklet = Soklet.withConfig(config)) {
  soklet.start();
  System.out.println("Soklet started, press [enter] to exit");
  soklet.awaitShutdown(ShutdownTrigger.ENTER_KEY);
}

Soklet also offers an off-network Simulator concept via runSimulator(SokletConfig, Consumer), useful for integration testing.

Given a Resource Method...

public class HelloResource {
  @GET("/hello")
  public String hello(@QueryParameter String name) {
    return String.format("Hello, %s", name);
  }
}
...we might test it like this:
@Test
public void integrationTest() {
  // Just use your app's existing configuration
  SokletConfig config = obtainMySokletConfig();

  // Instead of running on a real HTTP server that listens on a port,
  // a non-network Simulator is provided against which you can
  // issue requests and receive responses.
  Soklet.runSimulator(config, (simulator -> {
    // Construct a request
    Request request = Request.withPath(HttpMethod.GET, "/hello")
      .queryParameters(Map.of("name", Set.of("Mark")))
      .build();

    // Perform the request and get a handle to the response
    RequestResult result = simulator.performRequest(request);
    MarshaledResponse marshaledResponse = result.getMarshaledResponse();

    // Verify status code
    Integer expectedCode = 200;
    Integer actualCode = marshaledResponse.getStatusCode();
    assertEquals(expectedCode, actualCode, "Bad status code");

    // Verify response body
    marshaledResponse.getBody().ifPresentOrElse(body -> {
      String expectedBody = "Hello, Mark";
      String actualBody = new String(body, StandardCharsets.UTF_8);
      assertEquals(expectedBody, actualBody, "Bad response body");
    }, () -> {
      Assertions.fail("No response body");
    });
  }));
}

The Simulator also supports Server-Sent Events.

Integration testing documentation is available at https://www.soklet.com/docs/testing.

Author:
Mark Allen