NetworkMockPlugin

Ktor client plugin for intercepting HTTP requests and returning mock responses.

This plugin integrates with the DevView Network Mock module to allow developers to mock API calls during development and testing. It intercepts outgoing HTTP requests and can return predefined mock responses instead of making actual network calls.

Features

  • Request Interception: Intercepts HTTP requests at the send phase

  • Selective Mocking: Mock individual endpoints while others use real network

  • Global Toggle: Master switch to enable/disable all mocking

  • Path Parameters: Supports path parameters like /users/{userId}

  • Multiple Hosts: Can mock different hosts (staging, production, etc.)

  • Request Body Disambiguation: When two operations would otherwise collide on path, method, and query, a request body's shape (required fields, and/or a discriminator field's value — see com.worldline.devview.networkmock.core.model.RequestBodyMatch) picks the right one. Narrow matching, not full schema validation.

  • State Persistence: Mock configuration persists across app restarts

  • Failure Simulation: An operation can deterministically simulate a network failure (see com.worldline.devview.networkmock.core.model.OperationMockState.Failure), or fail a configurable percentage of the time via x-devview.failureRate

How It Works

  1. Plugin intercepts every HTTP request using Ktor's HttpSend mechanism

  2. Checks if global mocking is enabled via DataStore state

  3. Attempts to match the request (host, path, method, query, and — for operations that declare their own constraints — request body) to a configured endpoint

  4. If matched and mock is enabled for that endpoint, loads and returns the mock response

  5. Otherwise, proceeds with the actual network call

Default Installation

When NetworkMock is registered via rememberModules, repositories are resolved automatically from com.worldline.devview.networkmock.core.NetworkMockInitializer:

val client = HttpClient(OkHttp) {
install(NetworkMockPlugin)
}

Custom Installation

For testing or advanced scenarios, repositories can be injected explicitly:

val client = HttpClient(OkHttp) {
install(NetworkMockPlugin) {
mockRepository = myMockConfigRepository
stateRepository = myMockStateRepository
}
}

Mock File Setup

Create an OpenAPI 3.x spec, e.g. composeResources/files/networkmocks/specs/my-backend.json:

{
"info": { "title": "My Backend" },
"servers": [{ "url": "https://api.example.com" }],
"paths": {
"/v1/users/{userId}": {
"get": {
"operationId": "getUser",
"summary": "Get User Profile",
"responses": {
"200": {
"content": {
"application/json": {
"examples": {
"default": { "externalValue": "responses/getUser-200.json" }
}
}
}
}
}
}
}
}
}

Error Handling

Missing/invalid configuration and unmatched requests pass through to the actual network. Once a mock is selected, an unavailable variant or response-read error fails the request without sending a real request or consuming a sequence step. State read/write failures and coroutine cancellation propagate. Simulated failures intentionally throw to the caller.

Concurrent Requests

Each request reads fresh state. Sequence consumption atomically validates the state revision and selected step before committing, so concurrent consumers advance in commit order. A superseded prepared response is discarded and retried against current state, with at most one probability roll and one configured delay per HTTP request. The final step remains selected.

See also