Fixture

class Fixture(val status: Int, val headers: HttpHeaders, body: ByteArray)(source)

A recorded or hand-written response: a status, headers and body bytes, read from and written to the .http format.

The format: optional leading # ... provenance lines (ignored on read), a status line HTTP/<d>[.<d>] <three digits>[ <reason>] (the reason is ignored; HTTP/1.1 200 OK, HTTP/2 404 and HTTP/1.1 304 are all valid), Name: value header lines (the name an RFC 9110 token, the value trimmed of SP and HTAB, strict UTF-8, no NUL, no obs-fold), a blank line, and every remaining byte the body, verbatim; no blank line means an empty body. LF or CRLF ends a head line; a lone CR is refused. toBytes writes LF, HTTP/1.1, the RFC 9110 reason for the common statuses and Name: value per entry in order, so writing the parse of a canonical file reproduces it, and the body round-trips byte for byte. A consumer keeps its fixture files under its own test resources and marks them -text in its .gitattributes (a pattern over every .http file below a fixtures directory) so a checkout cannot rewrite their line endings.

Stable Core, source compatibility only. The body is copied in and read back as copies. Equality and hash are on the status, the headers and the body bytes. toString() prints the status, the header names (through HttpHeaders.toString()) and the body size, never a header value or the body. Immutable, so safe to share between threads.

Parameters

status

the status, in 100..599.

headers

the response headers.

body

the body; copied.

Throws

if status is outside 100..599.

Constructors

Link copied to clipboard
constructor(status: Int, headers: HttpHeaders, body: ByteArray)

Types

Link copied to clipboard
object Companion

The parser.

Properties

Link copied to clipboard

The body's size in bytes.

Link copied to clipboard

The response headers.

Link copied to clipboard
val status: Int

The status, in 100..599.

Functions

Link copied to clipboard

Returns the body.

Link copied to clipboard

Returns the body as text.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Two Fixtures are equal when their status, headers and body bytes are equal.

Link copied to clipboard
open override fun hashCode(): Int

Returns a hash code consistent with equals: it is computed from the same properties.

Link copied to clipboard
fun toBytes(comments: List<String> = emptyList()): ByteArray

Serialises the fixture in the .http format.

Link copied to clipboard
fun Fixture.toMockResponse(method: HttpMethod = HttpMethod.Get): MockResponse

Writes this fixture as a mockwebserver3.MockResponse that cannot hang or corrupt the exchange. The status line is HTTP/1.1 <status> <reason> with the fixture codec's reason phrase (none for an uncommon status); every header is written as given, in order, except Content-Length and Transfer-Encoding, which the framing decides: a 204 or 205 gets no body and no Content-Length; a 304, or any response to a HEAD, gets no body and keeps the declared length (for HEAD, the body size when none was declared); a fixture carrying Transfer-Encoding is framed as chunked; every other fixture gets its body and Content-Length from the bytes. Content-Length is written after the other headers. A fixture carrying Content-Encoding: gzip must hold encoded bytes: a mismatch fails the exchange fast with an IOException, never hangs.

Link copied to clipboard

Builds the response a transport would return for this fixture.

Link copied to clipboard
open override fun toString(): String

A diagnostic string for logs: status, the headers' names and the body size, never a header value or the body. Its format is not part of the API and may change.