fromFile

fun fromFile(fileName: String, content: String, statusTextProvider: (Int) -> String = ::getStatusText): MockResponse?

Creates a MockResponse from a file name and content.

This factory method parses the file name to extract the status code and generates an appropriate display name for UI presentation.

File Name Parsing

Expected format: {endpointId}-{statusCode}[-{suffix}].json

  • getUser-200.json → status = 200, suffix = null

  • getUser-404-simple.json → status = 404, suffix = "simple"

  • get-user-200.json → status = 200 (hyphenated endpoint ID supported)

Status code extraction is delegated to parseStatusCode, which is the single source of truth for this parsing logic.

Custom Status Text

By default, status codes are mapped to human-readable text using the built-in getStatusText function. A custom statusTextProvider lambda can be supplied to override this — useful for future HTTP client modules or integrators with non-standard status code conventions:

MockResponse.fromFile(
fileName = "getUser-200.json",
content = responseBody,
statusTextProvider = { code -> myHttpClient.statusText(code) }
)

Return

A MockResponse instance, or null if the file name format is invalid

Parameters

fileName

The response file name

content

The raw JSON content as a String

statusTextProvider

Optional lambda that maps a status code to its display text. Defaults to the built-in getStatusText mapping.