dream_http_client/matching

Request matching logic

Provides helpers for building stable request match keys.

A match key is a function that converts a recording.RecordedRequest into a string. Requests that produce the same key are considered equivalent for recording lookup and playback.

Keys + transformers

For many use cases, a key alone is enough. For cases like “ignore one query parameter” or “scrub secrets but still match”, pair a key with a recorder.request_transformer(...) so both matching and persistence see the same normalized request.

Types

A function that produces a stable match key for a request.

A MatchKey is the core of playback matching: it maps a RecordedRequest to a deterministic string. Requests that produce the same key are considered the “same request” for playback lookup.

Tip: If your requests contain secrets (Authorization headers, tokens) or volatile fields (timestamps, request IDs), use a recorder.request_transformer to normalize/scrub the request before the key is computed.

Example

Key on method and URL Tested snippet

import dream_http_client/matching
import dream_http_client/recorder.{key}

pub fn method_and_url_builder() -> recorder.RecorderBuilder {
  let key_fn =
    matching.request_key(method: True, url: True, headers: False, body: False)

  recorder.new() |> key(key_fn)
}
pub type MatchKey =
  fn(recording.RecordedRequest) -> String

Values

pub fn request_key(
  method method: Bool,
  url url: Bool,
  headers headers: Bool,
  body body: Bool,
) -> fn(recording.RecordedRequest) -> String

Create a request key function from simple include/exclude flags.

This is intended for the most common matching policies. For more advanced matching (ignore specific headers, normalize paths, drop query params, etc.) compose a recorder.request_transformer(...) with a custom key function.

Parameters

  • method: Include the HTTP method in the key
  • url: Include the full URL (scheme + host + port + path + query) in the key
  • headers: Include request headers in the key (sorted by header name)
  • body: Include the request body in the key

Returns

A MatchKey function.

Example

Key without the Authorization header Tested snippet

import dream_http_client/matching
import dream_http_client/recorder.{
  directory, key, mode, request_transformer, start,
}
import dream_http_client/recording
import gleam/list
import gleam/result

// Drop Authorization before keying and before writing to disk.
fn drop_auth(request: recording.RecordedRequest) -> recording.RecordedRequest {
  let headers =
    request.headers
    |> list.filter(fn(header) { header.0 != "Authorization" })
  recording.RecordedRequest(..request, headers: headers)
}

pub fn start_scrubbed(mocks_directory: String) -> Result(Bool, String) {
  // Build a key that ignores headers and body.
  let key_fn =
    matching.request_key(method: True, url: True, headers: False, body: False)

  use rec <- result.map(
    recorder.new()
    |> mode(recorder.Record)
    |> directory(mocks_directory)
    |> key(key_fn)
    |> request_transformer(drop_auth)
    |> start(),
  )
  recorder.is_record_mode(rec)
}
✨ Search Document