Skip to main content

Crate claude_api_test

Crate claude_api_test 

Source
Expand description

Cassette-based replay for claude-api integration tests.

Records of request → response exchanges are stored as JSONL on disk and served via wiremock. Tests point a claude_api::Client at the wiremock server’s URL and exercise the live code paths against the canned responses – no network calls, deterministic, reviewable in version control.

§Format

Each line of a cassette file is one RecordedExchange:

{"method":"POST","path":"/v1/messages","status":200,"request":{...},"response":{...}}
{"method":"GET","path":"/v1/models","status":200,"request":null,"response":{...}}

request is the optional decoded JSON body; response is the response body. The matcher pairs a live request with the first cassette entry whose (method, path) and request match. Use Cassette::skip_request_match to disable body matching when you only care about the URL.

§Quick start

use claude_api::{Client, messages::CreateMessageRequest, types::ModelId};
use claude_api_test::{mount_cassette, Cassette};
use wiremock::MockServer;

#[tokio::test]
async fn replay_messages_create() {
    let cassette = Cassette::from_path("tests/cassettes/messages_create.jsonl")
        .await
        .unwrap();
    let server = MockServer::start().await;
    mount_cassette(&server, &cassette).await;

    let client = Client::builder()
        .api_key("sk-ant-test")
        .base_url(server.uri())
        .build()
        .unwrap();
    let req = CreateMessageRequest::builder()
        .model(ModelId::SONNET_4_6)
        .max_tokens(64)
        .user("hi")
        .build()
        .unwrap();
    let resp = client.messages().create(req).await.unwrap();
    assert_eq!(resp.id, "msg_replay");
}

Re-exports§

pub use recorder::DEFAULT_REDACT_HEADERS;
pub use recorder::Recorder;
pub use recorder::RecorderConfig;

Modules§

recorder
Live recording proxy for claude-api-test.

Structs§

Cassette
A collection of RecordedExchanges, typically loaded from a JSONL file. Mount on a wiremock::MockServer via mount_cassette.
RecordedExchange
One recorded HTTP exchange. Preserved on disk as one JSONL line.

Functions§

mount_cassette
Mount every exchange in cassette on server. Each exchange becomes one wiremock::Mock that matches (method, path) (and the request body, unless Cassette::skip_request_match was set).