A scriptable web server for testing HTTP clients with support for HTTP/1.1, HTTP/2, WebSockets, and SSL/TLS
npx @tessl/cli install tessl/maven-com-squareup-okhttp3--mockwebserver@4.12.0MockWebServer is a scriptable web server for testing HTTP clients. It allows developers to create lightweight mock servers for unit testing HTTP interactions by enqueueing predefined responses and verifying the requests made by client code. The library supports HTTP/1.1, HTTP/2, WebSockets, and SSL/TLS protocols with comprehensive testing capabilities.
testImplementation 'com.squareup.okhttp3:mockwebserver:4.12.0'import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequestFor Java:
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.MockResponse
// Create and start mock server
val server = MockWebServer()
server.enqueue(MockResponse().setBody("Hello, World!"))
server.start()
// Get server URL for client to connect to
val baseUrl = server.url("/")
// Your HTTP client makes request to baseUrl
// ...
// Verify the request that was made
val recordedRequest = server.takeRequest()
assertEquals("GET", recordedRequest.method)
assertEquals("/", recordedRequest.path)
// Clean up
server.shutdown()MockWebServer is built around several key components:
Core server functionality for starting, stopping, and configuring MockWebServer instances. Essential for all testing scenarios.
class MockWebServer : ExternalResource(), Closeable {
fun start(port: Int = 0)
fun start(inetAddress: InetAddress, port: Int)
fun shutdown()
fun close()
val port: Int
val hostName: String
fun url(path: String): HttpUrl
}Comprehensive response building capabilities including headers, body content, delays, and connection policies. Supports both simple responses and complex scenarios like chunked encoding and HTTP/2 features.
class MockResponse : Cloneable {
fun setResponseCode(code: Int): MockResponse
fun setHeader(name: String, value: Any): MockResponse
fun setBody(body: String): MockResponse
fun setChunkedBody(body: String, maxChunkSize: Int): MockResponse
fun throttleBody(bytesPerPeriod: Long, period: Long, unit: TimeUnit): MockResponse
fun setSocketPolicy(socketPolicy: SocketPolicy): MockResponse
}Request inspection and verification functionality for validating client behavior. Provides access to all request details including headers, body, and metadata.
class RecordedRequest {
val requestLine: String
val method: String?
val path: String?
val headers: Headers
val body: Buffer
val requestUrl: HttpUrl?
fun getHeader(name: String): String?
}Advanced capabilities including custom dispatchers, HTTP/2 server push, duplex streaming, and SSL/TLS configuration.
abstract class Dispatcher {
abstract fun dispatch(request: RecordedRequest): MockResponse
}
class PushPromise(
val method: String,
val path: String,
val headers: Headers,
val response: MockResponse
)enum class SocketPolicy {
SHUTDOWN_SERVER_AFTER_RESPONSE,
KEEP_OPEN,
DISCONNECT_AT_END,
UPGRADE_TO_SSL_AT_END,
DISCONNECT_AT_START,
DISCONNECT_AFTER_REQUEST,
DISCONNECT_DURING_REQUEST_BODY,
DISCONNECT_DURING_RESPONSE_BODY,
DO_NOT_READ_REQUEST_BODY,
FAIL_HANDSHAKE,
SHUTDOWN_INPUT_AT_END,
SHUTDOWN_OUTPUT_AT_END,
STALL_SOCKET_AT_START,
NO_RESPONSE,
RESET_STREAM_AT_START,
EXPECT_CONTINUE,
CONTINUE_ALWAYS
}