github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/testutil/vcr.go (about) 1 package testutil 2 3 import ( 4 "net/http" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/dnaeon/go-vcr/recorder" 10 ) 11 12 // NewVCRHTTPClient creates an *http.Client wired to a go-vcr recorder. 13 // Cassette files are stored under testdata/cassettes. 14 // If ANTHROPIC_LIVE=1, the recorder runs in recording mode; otherwise replay-only. 15 func NewVCRHTTPClient(t *testing.T, cassetteName string) (*http.Client, *recorder.Recorder) { 16 t.Helper() 17 18 mode := recorder.ModeReplaying 19 if os.Getenv("ANTHROPIC_LIVE") == "1" { 20 mode = recorder.ModeRecording 21 } 22 23 // Let go-vcr handle the .yaml extension to avoid accidental double suffixes 24 cassettePath := filepath.Join("testdata", "cassettes", cassetteName) 25 r, err := recorder.NewAsMode(cassettePath, mode, nil) 26 if err != nil { 27 t.Fatalf("failed to create recorder: %v", err) 28 } 29 30 t.Cleanup(func() { 31 _ = r.Stop() 32 }) 33 34 httpClient := &http.Client{Transport: r} 35 return httpClient, r 36 }