Interface Simulator


public interface Simulator
Simulates server behavior of accepting a request and returning a response, useful for writing integration tests.

Server-Sent Event simulation is also supported.

Instances of Simulator are made available via Soklet.runSimulator(SokletConfig, Consumer).

Usage example:

 @Test
public void basicIntegrationTest() {
  // Build your configuration however you like
  SokletConfig config = obtainMySokletConfig();

  // Instead of running on a real HTTP server that listens on a port,
  // a simulator is provided against which you can issue requests
  // and receive responses.
  Soklet.runSimulator(config, (simulator -> {
    // Construct a request.
    // You may alternatively specify query parameters directly in the URI
    // as a query string, e.g. "/hello?name=Mark"
    Request request = Request.with(HttpMethod.GET, "/hello")
      .queryParameters(Map.of("name", Set.of("Mark")))
      .build();

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

    // Verify status code
    Integer expectedCode = 200;
    Integer actualCode = result.getMarshaledResponse().getStatusCode();
    Assert.assertEquals("Bad status code", expectedCode, actualCode);
  }));
}

Full documentation is available at https://www.soklet.com/docs/automated-testing.

Author:
Mark Allen