NetworkCaptureSink

HTTP-client-agnostic sink that records network transactions.

The OkHttp adapter lives in :lustro; this SPI lets any HTTP client report the lifecycle of a request: beginRequest on dispatch, then one or more completeRequest calls (progressive for event streams) or a single failRequest. A request that a mock rule failed reports the rule with it.

The built-in sink returns from each call right away and records the transaction on a background thread once it's redacted, so a transaction can take a moment to show up.

Functions

Link copied to clipboard
abstract fun beginRequest(url: String, method: String, headers: Headers, requestBody: CapturedBody?, contentType: MediaType?): TransactionId

Records the start of a request to url with the given method, headers, optional requestBody, and contentType, returning a TransactionId to correlate completion calls. The requestBody carries the captured text plus its truncation flag and full byte size.

Link copied to clipboard
abstract fun completeRequest(id: TransactionId, response: CapturedResponse)

Records the response to the transaction id. A request whose body the adapter captures whole reports one complete response. One whose body the app reads after that, such as a download or an event stream, reports an in-flight one when its headers arrive, and more as it reads if the adapter has progress to show, then a complete one when the app reaches the end of the body or closes it (see CapturedResponse.isComplete).

Link copied to clipboard
abstract fun failRequest(id: TransactionId, durationMs: Long, error: String)

Records a failed transaction id after durationMs, with the error description. After a response was reported, it records that the body failed while the app read it, and the response's status and headers are kept.

open fun failRequest(id: TransactionId, durationMs: Long, error: String, mockedBy: MockRule)

Records the transaction id as failed after durationMs, with the error description, because the mock rule mockedBy failed it: a rule with a MockRule.failure, as returned by findMockRule. The Network tab then says which rule failed the request, as CapturedResponse.Builder.mockedBy does for a rule that answered one.

Link copied to clipboard
abstract fun findMockRule(url: String, method: String): MockRule?

Returns the mock rule matching the url/method, or null when no enabled rule matches.