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(SokletConfiguration, Consumer)
.
Usage example:
@Test
public void basicIntegrationTest() {
// Build your configuration however you like
SokletConfiguration 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
-
Method Summary
Modifier and TypeMethodDescriptionacquireServerSentEventBroadcaster
(ResourcePath resourcePath) Acquires a Server-Sent Event broadcaster for the givenResourcePath
.performRequest
(Request request) Given a request, process it and return response data (both logicalResponse
, if present, and theMarshaledResponse
bytes to be sent over the wire) as well as the matching Resource Method, if available.void
registerServerSentEventConsumer
(ResourcePath resourcePath, Consumer<ServerSentEvent> serverSentEventConsumer) Registers aServerSentEvent
"consumer" for the givenResourcePath
- similar to how a real client would listen for Server-Sent Events.
-
Method Details
-
performRequest
Given a request, process it and return response data (both logicalResponse
, if present, and theMarshaledResponse
bytes to be sent over the wire) as well as the matching Resource Method, if available.- Parameters:
request
- the request to process- Returns:
- the result (logical response, marshaled response, etc.) that corresponds to the request
-
registerServerSentEventConsumer
void registerServerSentEventConsumer(@Nonnull ResourcePath resourcePath, @Nonnull Consumer<ServerSentEvent> serverSentEventConsumer) Registers aServerSentEvent
"consumer" for the givenResourcePath
- similar to how a real client would listen for Server-Sent Events.See documentation at https://www.soklet.com/docs/server-sent-events#testing.
- Parameters:
resourcePath
- the Resource Path on which to listen for Server-Sent EventsserverSentEventConsumer
- function to be invoked when a Server-Sent Event has been broadcast on the Resource Path
-
acquireServerSentEventBroadcaster
@Nonnull ServerSentEventBroadcaster acquireServerSentEventBroadcaster(@Nonnull ResourcePath resourcePath) Acquires a Server-Sent Event broadcaster for the givenResourcePath
.See documentation at https://www.soklet.com/docs/server-sent-events#testing.
- Parameters:
resourcePath
- the Resource Path on which to broadcast Server-Sent Events- Returns:
- a Server-Sent Event broadcaster
-