mockwebserver / okhttp3.mockwebserver / MockResponse

MockResponse

class MockResponse : Cloneable

A scripted response to be replayed by the mock web server.

Constructors

<init>

MockResponse()

A scripted response to be replayed by the mock web server.

Properties

duplexResponseBody

var duplexResponseBody: DuplexResponseBody?

headers

var headers: Headers

The HTTP headers, such as "Content-Length: 0".

http2ErrorCode

var http2ErrorCode: Int

Sets the HTTP/2 error code to be returned when resetting the stream. This is only valid with SocketPolicy.RESET_STREAM_AT_START.

isDuplex

val isDuplex: Boolean

pushPromises

val pushPromises: List<PushPromise>

Returns the streams the server will push with this response.

settings

var settings: Settings

socketPolicy

var socketPolicy: SocketPolicy

status

var status: String

Returns the HTTP response line, such as "HTTP/1.1 200 OK".

throttleBytesPerPeriod

var throttleBytesPerPeriod: Long

trailers

var trailers: Headers

webSocketListener

var webSocketListener: WebSocketListener?

Functions

addHeader

fun addHeader(header: String): MockResponse

Adds header as an HTTP header. For well-formed HTTP header should contain a name followed by a colon and a value.

fun addHeader(name: String, value: Any): MockResponse

Adds a new header with the name and value. This may be used to add multiple headers with the same name.

addHeaderLenient

fun addHeaderLenient(name: String, value: Any): MockResponse

Adds a new header with the name and value. This may be used to add multiple headers with the same name. Unlike addHeader this does not validate the name and value.

clearHeaders

fun clearHeaders(): MockResponse

Removes all HTTP headers including any "Content-Length" and "Transfer-encoding" headers that were added by default.

clone

fun clone(): MockResponse

getBody

fun getBody(): Buffer?

Returns a copy of the raw HTTP payload.

getBodyDelay

fun getBodyDelay(unit: TimeUnit): Long

getHeadersDelay

fun getHeadersDelay(unit: TimeUnit): Long

getThrottlePeriod

fun getThrottlePeriod(unit: TimeUnit): Long

removeHeader

fun removeHeader(name: String): MockResponse

Removes all headers named name.

setBody

fun setBody(body: Buffer): MockResponse
fun setBody(duplexResponseBody: DuplexResponseBody): MockResponsefun setBody(body: String): MockResponse

Sets the response body to the UTF-8 encoded bytes of body.

setBodyDelay

fun setBodyDelay(delay: Long, unit: TimeUnit): MockResponse

Set the delayed time of the response body to delay. This applies to the response body only; response headers are not affected.

setChunkedBody

fun setChunkedBody(body: Buffer, maxChunkSize: Int): MockResponse

Sets the response body to body, chunked every maxChunkSize bytes.

fun setChunkedBody(body: String, maxChunkSize: Int): MockResponse

Sets the response body to the UTF-8 encoded bytes of body, chunked every maxChunkSize bytes.

setHeader

fun setHeader(name: String, value: Any): MockResponse

Removes all headers named name, then adds a new header with the name and value.

setHeadersDelay

fun setHeadersDelay(delay: Long, unit: TimeUnit): MockResponse

setResponseCode

fun setResponseCode(code: Int): MockResponse

throttleBody

fun throttleBody(bytesPerPeriod: Long, period: Long, unit: TimeUnit): MockResponse

Throttles the request reader and response writer to sleep for the given period after each series of bytesPerPeriod bytes are transferred. Use this to simulate network behavior.

toString

fun toString(): String

withPush

fun withPush(promise: PushPromise): MockResponse

When protocols include HTTP_2, this attaches a pushed stream to this response.

withSettings

fun withSettings(settings: Settings): MockResponse

When protocols include HTTP_2, this pushes settings before writing the response.

withWebSocketUpgrade

fun withWebSocketUpgrade(listener: WebSocketListener): MockResponse

Attempts to perform a web socket upgrade on the connection. This will overwrite any previously set status or body.