MockServer

interface MockServer : Closeable

A server for testing Kotlin Multiplatform applications using HTTP and WebSockets.

A MockServer binds to localhost and allows to enqueue predefined responses using enqueue, enqueueString, enqueueMultipart and enqueueWebSocket

MockServer is very simple and should not be used for production applications. HTTPS is a non-goal as well as performance. Also, MockServer makes no attempt at flow control:

  • data is read as fast as possible from the network and buffered until takeRequest or awaitAnyRequest is called.

  • queued responses from enqueue are buffered until they can be transmitted to the network. If you're using MockServer to handle large payloads, it will use a lot of memory.

Types

Link copied to clipboard
class Builder
Link copied to clipboard
interface Listener

Functions

Link copied to clipboard
Link copied to clipboard
abstract suspend fun awaitAnyRequest(timeout: Duration = 1.seconds): MockRequestBase

Wait for a request and return it.

Link copied to clipboard
suspend fun MockServer.awaitRequest(timeout: Duration = 30.seconds): MockRequest
Link copied to clipboard
Link copied to clipboard
abstract override fun close()

Closes the server.

Link copied to clipboard
abstract fun enqueue(mockResponse: MockResponse)

Enqueue a response.

Link copied to clipboard
fun MockServer.enqueue(string: String = "", delayMs: Long = 0, statusCode: Int = 200)
Link copied to clipboard
fun MockServer.enqueueError(statusCode: Int)
Link copied to clipboard
fun MockServer.enqueueGraphQLString(string: String, delayMs: Long = 0)
Link copied to clipboard
fun MockServer.enqueueString(string: String = "", delayMs: Long = 0, statusCode: Int = 200, contentType: String = "text/plain")
Link copied to clipboard
fun MockServer.enqueueWebSocket(statusCode: Int = 101, headers: Map<String, String> = emptyMap(), keepAlive: Boolean = true): WebSocketBody
Link copied to clipboard
abstract suspend fun port(): Int

Returns the port used by this server.

Link copied to clipboard
Link copied to clipboard
abstract fun takeRequest(): MockRequest

Return a request from the recorded requests or throw if no request has been received.

Link copied to clipboard
abstract suspend fun url(): String

Returns the url for this server in the form "http://ip:port/".