mockwebserver / okhttp3.mockwebserver / MockWebServer

MockWebServer

class MockWebServer : ExternalResource, Closeable

A scriptable web server. Callers supply canned responses and the server replays them upon request in sequence.

Constructors

<init>

MockWebServer()

A scriptable web server. Callers supply canned responses and the server replays them upon request in sequence.

Functions

after

fun after(): Unit

before

fun before(): Unit

close

fun close(): Unit

enqueue

fun enqueue(response: MockResponse!): Unit

Scripts response to be returned to a request made in sequence. The first request is served by the first enqueued response; the second request by the second enqueued response; and so on.

getDispatcher

fun getDispatcher(): Dispatcher!

Returns the dispatcher used to respond to HTTP requests. The default dispatcher is a but other dispatchers can be configured.

getHostName

fun getHostName(): String!

getPort

fun getPort(): Int

getRequestCount

fun getRequestCount(): Int

Returns the number of HTTP requests received thus far by this server. This may exceed the number of HTTP connections when connection reuse is in practice.

noClientAuth

fun noClientAuth(): Unit

Configure the server to not perform SSL authentication of the client. This leaves authentication to another layer such as in an HTTP cookie or header. This is the default and most common configuration.

protocols

fun protocols(): MutableList<Protocol!>!

requestClientAuth

fun requestClientAuth(): Unit

Configure the server to want client auth. If the client presents a certificate that is trusted the handshake will proceed normally. The connection will also proceed normally if the client presents no certificate at all! But if the client presents an untrusted certificate the handshake will fail and no connection will be established.

requireClientAuth

fun requireClientAuth(): Unit

Configure the server to need client auth. If the client presents a certificate that is trusted the handshake will proceed normally. If the client presents an untrusted certificate or no certificate at all the handshake will fail and no connection will be established.

setBodyLimit

fun setBodyLimit(maxBodyLength: Long): Unit

Sets the number of bytes of the POST body to keep in memory to the given limit.

setDispatcher

fun setDispatcher(dispatcher: Dispatcher!): Unit

Sets the dispatcher used to match incoming requests to mock responses. The default dispatcher simply serves a fixed sequence of responses from a queue; custom dispatchers can vary the response based on timing or the content of the request.

setProtocolNegotiationEnabled

fun setProtocolNegotiationEnabled(protocolNegotiationEnabled: Boolean): Unit

Sets whether ALPN is used on incoming HTTPS connections to negotiate a protocol like HTTP/1.1 or HTTP/2. Call this method to disable negotiation and restrict connections to HTTP/1.1.

setProtocols

fun setProtocols(protocols: MutableList<Protocol!>!): Unit

Indicates the protocols supported by ALPN on incoming HTTPS connections. This list is ignored when negotiation is disabled.

setServerSocketFactory

fun setServerSocketFactory(serverSocketFactory: ServerSocketFactory!): Unit

shutdown

fun shutdown(): Unit

start

fun start(): Unit

Equivalent to start(0).

fun start(port: Int): Unit

Starts the server on the loopback interface for the given port.

fun start(inetAddress: InetAddress!, port: Int): Unit

Starts the server on the given address and port.

takeRequest

fun takeRequest(): RecordedRequest!

Awaits the next HTTP request, removes it, and returns it. Callers should use this to verify the request was sent as intended. This method will block until the request is available, possibly forever.

fun takeRequest(timeout: Long, unit: TimeUnit!): RecordedRequest!

Awaits the next HTTP request (waiting up to the specified wait time if necessary), removes it, and returns it. Callers should use this to verify the request was sent as intended within the given time.

toProxyAddress

fun toProxyAddress(): Proxy!

toString

fun toString(): String

url

fun url(path: String!): HttpUrl!

Returns a URL for connecting to this server.

useHttps

fun useHttps(sslSocketFactory: SSLSocketFactory!, tunnelProxy: Boolean): Unit

Serve requests with HTTPS rather than otherwise.