claude-api-test 0.5.3

Test utilities for claude-api: cassette-based replay of recorded HTTP exchanges
Documentation

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");
}