Documentation
¶
Overview ¶
Package fake provides a VCR-based proxy for replaying recorded AI API responses. This is useful for E2E testing without making real API calls.
Index ¶
- func APIKeyHeaderUpdater(host string, req *http.Request)
- func DefaultMatcher(onError func(err error)) recorder.MatcherFunc
- func Handle(transport http.RoundTripper, ...) echo.HandlerFunc
- func IsStreamResponse(resp *http.Response) bool
- func RemoveHeadersHook(i *cassette.Interaction) error
- func SimulatedStreamCopy(c echo.Context, resp *http.Response, chunkDelay time.Duration) error
- func StartProxy(cassettePath string, opts ...ProxyOption) (string, func() error, error)
- func StartProxyWithOptions(cassettePath string, mode recorder.Mode, matcher recorder.MatcherFunc, ...) (string, func() error, error)
- func StartRecordingProxy(cassettePath string) (string, func() error, error)
- func StartStreamingRecordingProxy(cassettePath string, headerUpdater func(host string, req *http.Request)) (string, func() error, error)
- func StreamCopy(c echo.Context, resp *http.Response) error
- func TargetURLForHost(host string) func(req *http.Request) string
- type ProxyOption
- type ProxyOptions
- type StreamingRecorder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIKeyHeaderUpdater ¶
APIKeyHeaderUpdater injects API keys from environment variables into request headers. This is used when recording API interactions to ensure real API calls succeed.
func DefaultMatcher ¶ added in v1.18.8
func DefaultMatcher(onError func(err error)) recorder.MatcherFunc
DefaultMatcher creates a matcher that normalizes tool call IDs for consistent matching. The onError callback is called if reading the request body fails (nil logs and returns false).
func Handle ¶
func Handle(transport http.RoundTripper, headerUpdater func(host string, req *http.Request), options *ProxyOptions) echo.HandlerFunc
Handle creates an echo handler that proxies requests through the VCR transport. The headerUpdater is called with the host and request to update headers (e.g., for adding API keys). The options parameter controls streaming simulation behavior.
func IsStreamResponse ¶
IsStreamResponse checks if the response should be streamed. It checks Content-Type headers first, then falls back to peeking at the body for SSE format (useful when headers are stripped in recorded cassettes).
func RemoveHeadersHook ¶
func RemoveHeadersHook(i *cassette.Interaction) error
RemoveHeadersHook strips headers from recorded interactions for security.
func SimulatedStreamCopy ¶ added in v1.19.4
SimulatedStreamCopy copies a streaming SSE response to the client with artificial delays between events to simulate real-time streaming behavior.
func StartProxy ¶
func StartProxy(cassettePath string, opts ...ProxyOption) (string, func() error, error)
StartProxy starts an internal HTTP proxy that replays cassette responses. It returns the proxy URL and a cleanup function that should be called when done.
func StartProxyWithOptions ¶
func StartProxyWithOptions( cassettePath string, mode recorder.Mode, matcher recorder.MatcherFunc, headerUpdater func(host string, req *http.Request), options *ProxyOptions, ) (string, func() error, error)
StartProxyWithOptions starts an internal HTTP proxy with configurable options. - mode: recorder mode (ModeReplayOnly, ModeRecordOnce, etc.) - matcher: custom matcher function (nil uses DefaultMatcher) - headerUpdater: optional function to update request headers (for recording with real API keys) - options: proxy options for stream simulation, etc.
func StartRecordingProxy ¶
StartRecordingProxy starts a proxy that records AI API interactions to a cassette file. It injects API keys from environment variables for the actual API calls. The recorded cassette can later be replayed using StartProxy. This uses a streaming-aware recorder that allows responses to stream through in real-time while being recorded, unlike the standard VCR recorder.
func StartStreamingRecordingProxy ¶ added in v1.19.4
func StartStreamingRecordingProxy( cassettePath string, headerUpdater func(host string, req *http.Request), ) (string, func() error, error)
StartStreamingRecordingProxy starts a recording proxy with streaming support. Unlike StartProxyWithOptions which buffers entire responses, this allows streaming responses to pass through in real-time while being recorded.
func StreamCopy ¶
StreamCopy copies a streaming response to the client. It properly handles context cancellation during blocking reads.
Types ¶
type ProxyOption ¶ added in v1.19.4
type ProxyOption func(*ProxyOptions)
ProxyOption is a function that configures ProxyOptions.
func WithSimulateStream ¶ added in v1.19.4
func WithSimulateStream(enabled bool) ProxyOption
WithSimulateStream enables simulated streaming with delays between chunks.
func WithStreamChunkDelay ¶ added in v1.19.4
func WithStreamChunkDelay(d time.Duration) ProxyOption
WithStreamChunkDelay sets the delay between SSE chunks.
type ProxyOptions ¶ added in v1.19.4
type ProxyOptions struct {
// SimulateStream adds delays between SSE chunks to simulate real streaming.
SimulateStream bool
// StreamChunkDelay is the delay between SSE chunks when SimulateStream is true.
// Defaults to 15ms if not set.
StreamChunkDelay time.Duration
}
ProxyOptions configures the fake proxy behavior.
type StreamingRecorder ¶ added in v1.19.4
type StreamingRecorder struct {
// contains filtered or unexported fields
}
StreamingRecorder wraps an http.RoundTripper to record interactions while allowing streaming responses to pass through in real-time. Unlike the standard VCR recorder which buffers entire responses, this recorder tees the response body so it can be streamed to the client while simultaneously being captured for recording.
func NewStreamingRecorder ¶ added in v1.19.4
func NewStreamingRecorder(cassettePath string) (*StreamingRecorder, error)
NewStreamingRecorder creates a new streaming recorder that will save interactions to the specified cassette file.
func (*StreamingRecorder) RoundTrip ¶ added in v1.19.4
RoundTrip implements http.RoundTripper. It makes the actual HTTP request, tees the response body for recording, and returns immediately so the response can be streamed to the client.
func (*StreamingRecorder) Stop ¶ added in v1.19.4
func (r *StreamingRecorder) Stop() error
Stop saves the cassette to disk.