ktor-server-test-host / io.ktor.server.testing / TestApplicationEngine

TestApplicationEngine

class TestApplicationEngine : BaseApplicationEngine, CoroutineScope

ktor test engine that provides way to simulate application calls to existing application module(s) without actual HTTP connection

Types

Configuration

class Configuration : Configuration

Test application engine configuration

Constructors

<init>

TestApplicationEngine(environment: ApplicationEngineEnvironment = createTestEnvironment(), configure: Configuration.() -> Unit = {})

ktor test engine that provides way to simulate application calls to existing application module(s) without actual HTTP connection

Properties

callInterceptor

var callInterceptor: <ERROR CLASS><Unit, ApplicationCall>

interceptor for engine calls. can be modified to emulate certain engine behaviour (e.g. error handling)

client

val client: HttpClient

A client instance connected to this test server instance. Only works until engine stop invocation.

coroutineContext

val coroutineContext: <ERROR CLASS>

engine

val engine: HttpClientEngine

An instance of client engine user to be used in client.

Functions

createCall

fun createCall(readResponse: Boolean = false, closeRequest: Boolean = true, setup: TestApplicationRequest.() -> Unit): TestApplicationCall

Creates an instance of test call but doesn't start request processing

handleRequest

fun handleRequest(closeRequest: Boolean = true, setup: TestApplicationRequest.() -> Unit): TestApplicationCall

Make a test request

handleWebSocket

fun handleWebSocket(uri: String, setup: TestApplicationRequest.() -> Unit): TestApplicationCall

Make a test request that setup a websocket session and wait for completion

handleWebSocketConversation

fun handleWebSocketConversation(uri: String, setup: TestApplicationRequest.() -> Unit = {}, callback: suspend TestApplicationCall.(incoming: ReceiveChannel<Frame>, outgoing: SendChannel<Frame>) -> Unit): TestApplicationCall

Make a test request that setup a websocket session and invoke callback function that does conversation with server

hookRequests

fun hookRequests(processRequest: TestApplicationRequest.(setup: TestApplicationRequest.() -> Unit) -> Unit, processResponse: TestApplicationCall.() -> Unit, block: () -> Unit): Unit

Install a hook for test requests

start

fun start(wait: Boolean): ApplicationEngine

stop

fun stop(gracePeriodMillis: Long, timeoutMillis: Long): Unit

Extension Functions

cookiesSession

fun TestApplicationEngine.cookiesSession(callback: () -> Unit): Unit

Keep cookies between requests inside the callback.

handleRequest

fun TestApplicationEngine.handleRequest(method: HttpMethod, uri: String, setup: TestApplicationRequest.() -> Unit = {}): TestApplicationCall

Make a test request