MokksyServer

open class MokksyServer @JvmOverloads constructor(host: String = DEFAULT_HOST, port: Int = 0, configuration: ServerConfiguration, configurer: ApplicationConfigurer = {})(source)

An embedded mock HTTP server for testing. Registers stubs for any HTTP method and verifies request expectations after the test.

Call start() (on JVM) or startSuspend to begin processing requests after construction.

Example:

val mokksy = Mokksy().apply { start() }

mokksy.get {
path("/ping")
} respondsWith {
body = """{"response":"Pong"}"""
}

Author

Konstantin Pavlov

Parameters

host

The host to bind to. Defaults to 127.0.0.1.

port

The port to bind to. Defaults to 0 (randomly assigned).

configuration
configurer

Additional Ktor Application configuration applied after the default routing setup.

Constructors

Link copied to clipboard
constructor(host: String = DEFAULT_HOST, port: Int = 0, configuration: ServerConfiguration, configurer: ApplicationConfigurer = {})

Creates a MokksyServer instance. Call start() (on JVM) or startSuspend to begin processing requests.

constructor(host: String = DEFAULT_HOST, port: Int = 0, verbose: Boolean = false, configurer: ApplicationConfigurer = {})

Creates a MokksyServer instance using a verbose flag instead of a full ServerConfiguration. Call start() (on JVM) or startSuspend to begin processing requests.

Functions

Link copied to clipboard
suspend fun awaitStarted()

Suspends until the server has fully started and the port is bound.

Link copied to clipboard

Constructs and returns the base URL for the server.

Link copied to clipboard

Defines a stub for an HTTP DELETE request with a string request body.

fun <P : Any> delete(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Registers a stub for an HTTP DELETE request with the specified configuration and request type.

fun <P : Any> delete(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP DELETE request with the specified request type and configuration block.

Link copied to clipboard

Returns all HTTP requests that arrived at the server but were not matched by any stub.

Link copied to clipboard

Returns all HTTP requests that arrived at the server but were not matched by any stub.

Link copied to clipboard

Returns all stub specifications that have not been matched by any incoming request.

Link copied to clipboard

Defines a stub for an HTTP GET request with a string body using the provided configuration block.

Defines a stub for an HTTP GET request with the specified configuration and request specification builder.

fun <P : Any> get(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Registers a stub for an HTTP GET request with the specified configuration and request type.

fun <P : Any> get(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP GET request with the specified request type and configuration block.

Link copied to clipboard

Defines a stub for an HTTP HEAD request with a string request body.

fun <P : Any> head(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for a HEAD HTTP request with the specified configuration and request type.

fun <P : Any> head(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP HEAD request with the specified request type and configuration block.

Link copied to clipboard
fun <P : Any> method(configuration: StubConfiguration, httpMethod: HttpMethod, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Creates a RequestSpecification for the given HTTP method and request type, and returns a BuildingStep for further stub configuration.

fun <P : Any> method(name: String? = null, httpMethod: HttpMethod, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stubbed HTTP RequestSpecification for the given method and request type, optionally naming the stub.

Link copied to clipboard

Defines an HTTP OPTIONS request stub with a string request body using the provided configuration block.

fun <P : Any> options(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP OPTIONS request with the specified configuration and request type.

fun <P : Any> options(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP OPTIONS request with the specified request type and configuration block.

Link copied to clipboard

Defines a PATCH request stub with a string request body using the provided configuration block.

fun <P : Any> patch(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Registers a stub for an HTTP PATCH request with the specified configuration and request type.

fun <P : Any> patch(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP PATCH request with the specified request type and configuration block.

Link copied to clipboard
fun port(): Int

Returns the actual port number the server is bound to after startup.

Link copied to clipboard

Defines a stub for an HTTP POST request with a string request body.

fun <P : Any> post(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a POST request stub with the specified configuration and request type.

fun <P : Any> post(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP POST request with the specified request type and configuration block.

Link copied to clipboard

Defines a stub for an HTTP PUT request with a string request body.

fun <P : Any> put(configuration: StubConfiguration, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP PUT request with the specified configuration and request type.

fun <P : Any> put(name: String? = null, requestType: KClass<P>, block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Defines a stub for an HTTP PUT request with the specified request type and configuration block.

Link copied to clipboard

Resets the match count of all registered stubs to zero and clears the request journal.

Link copied to clipboard
fun Mokksy.shutdown(gracePeriodMillis: Long = 500, timeoutMillis: Long = 1000)

Stops the Mokksy server, blocking until shutdown is complete.

fun Mokksy.shutdown(gracePeriodMillis: Long = 500, timeoutMillis: Long = 1000, dispatcher: CoroutineDispatcher)

Stops the Mokksy server on the given dispatcher, blocking until shutdown is complete.

Link copied to clipboard
suspend fun shutdownSuspend(gracePeriodMillis: Long = 500, timeoutMillis: Long = 1000)

Stops the embedded server and releases its resources with the specified grace period and timeout.

Link copied to clipboard

Starts the Mokksy server and blocks until the port is bound and ready to accept requests.

Starts the Mokksy server on the given dispatcher, blocking until the port is bound.

Link copied to clipboard
suspend fun startSuspend(wait: Boolean = false)

Initiates the server to begin processing requests asynchronously.

Link copied to clipboard

Verifies that every request received by the server was matched by a stub.

Link copied to clipboard

Verifies that all registered stubs have been matched at least once.