or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-features.mdindex.mdrequest-verification.mdresponse-configuration.mdserver-management.md
tile.json

tessl/maven-com-squareup-okhttp3--mockwebserver

A scriptable web server for testing HTTP clients with support for HTTP/1.1, HTTP/2, WebSockets, and SSL/TLS

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.squareup.okhttp3/mockwebserver@4.12.x

To install, run

npx @tessl/cli install tessl/maven-com-squareup-okhttp3--mockwebserver@4.12.0

index.mddocs/

MockWebServer

MockWebServer 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.

Package Information

  • Package Name: com.squareup.okhttp3:mockwebserver
  • Package Type: maven
  • Language: Kotlin/Java
  • Installation: Add to your build.gradle: testImplementation 'com.squareup.okhttp3:mockwebserver:4.12.0'

Core Imports

import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.RecordedRequest

For Java:

import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;

Basic Usage

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()

Architecture

MockWebServer is built around several key components:

  • MockWebServer: The main server class that listens for connections and serves responses
  • MockResponse: Scriptable response objects with headers, body, delays, and connection policies
  • RecordedRequest: Immutable objects representing requests received by the server
  • Dispatcher: Strategy pattern for determining which response to serve for each request
  • SocketPolicy: Enum controlling connection lifecycle and failure simulation

Capabilities

Server Management

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
}

Server Management

Response Configuration

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
}

Response Configuration

Request Verification

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?
}

Request Verification

Advanced Features

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
)

Advanced Features

Types

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
}