MokksyServer

class MokksyServer @JvmOverloads constructor(host: String = DEFAULT_HOST, port: Int = 0, val configuration: ServerConfiguration, configurer: Application.() -> Unit = {}) : MokksyHandler(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().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: Application.() -> Unit = {})

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: Application.() -> Unit = {})

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

Properties

Link copied to clipboard

Functions

Link copied to clipboard
suspend fun awaitStarted(): MokksyServer

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 DELETE stub with a String request body.

Defines a DELETE stub with a String request body and a full StubConfiguration.

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

Defines a DELETE stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines a DELETE stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.delete(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.delete with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.delete with an optional stub name. Infers P from the call site.

Link copied to clipboard

Returns all HTTP requests that were successfully matched by a stub. Only populated when ServerConfiguration.journalMode is JournalMode.FULL.

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 GET stub with a String request body.

Defines a GET stub with a String request body and a full StubConfiguration.

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

Defines a GET stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines a GET stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.get(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.get with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.get with an optional stub name. Infers P from the call site.

Link copied to clipboard
open suspend override fun handle(context: RoutingContext)

Dispatches an incoming request to the appropriate stub and writes the response.

Link copied to clipboard

Defines a HEAD stub with a String request body.

Defines a HEAD stub with a String request body and a full StubConfiguration.

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

Defines a HEAD stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines a HEAD stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.head(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.head with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.head with an optional stub name. Infers P from the call site.

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
inline fun <P : Any> MokksyServer.method(configuration: StubConfiguration, httpMethod: HttpMethod, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.method with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.method with an optional stub name. Infers P from the call site.

Link copied to clipboard

Defines an OPTIONS stub with a String request body.

Defines an OPTIONS stub with a String request body and a full StubConfiguration.

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

Defines an OPTIONS stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines an OPTIONS stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.options(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.options with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.options with an optional stub name. Infers P from the call site.

Link copied to clipboard

Defines a PATCH stub with a String request body.

Defines a PATCH stub with a String request body and a full StubConfiguration.

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

Defines a PATCH stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines a PATCH stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.patch(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.patch with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.patch with an optional stub name. Infers P from the call site.

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 POST stub with a String request body.

Defines a POST stub with a String request body and a full StubConfiguration.

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

Defines a POST stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines a POST stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.post(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.post with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.post with an optional stub name. Infers P from the call site.

Link copied to clipboard

Defines a PUT stub with a String request body.

Defines a PUT stub with a String request body and a full StubConfiguration.

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

Defines a PUT stub that matches a typed request body of type P using a full StubConfiguration.

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

Defines a PUT stub that matches a typed request body of type P.

Link copied to clipboard
inline fun <P : Any> MokksyServer.put(configuration: StubConfiguration, noinline block: RequestSpecificationBuilder<P>.() -> Unit): BuildingStep<P>

Reified shortcut for MokksyServer.put with a StubConfiguration. Infers P from the call site.

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

Reified shortcut for MokksyServer.put with an optional stub name. Infers P from the call site.

Link copied to clipboard

Resets the match state of all currently registered stubs to unmatched, and clears the request journal.

Link copied to clipboard

Resets the match state of all currently registered stubs to unmatched, and clears the request journal.

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

Stops the Mokksy server, blocking until shutdown is complete.

fun MokksyServer.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): MokksyServer

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.