Documentation
¶
Overview ¶
Package servertest provides a reusable contract suite for codec.ServerCodec implementations. The same suite is meant to run, unmodified beyond its Config, against every dialect's real server codec (anthropicapi, openairesponses, openaiapi, geminiapi) as well as against hand-written test fakes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run exercises cfg.NewCodec against the shared server-codec contract: exactly one route match, content-type behavior, malformed-body rejection, native error shape, streaming flush, clean finish, cancellation, and single ownership of request/stream closure. Call it from a dialect package's own tests with that dialect's real codec and fixtures; codec/servertest's own contract_test.go calls it against a fake to prove the suite itself is exercised.
Types ¶
type Config ¶
type Config struct {
// NewCodec builds the ServerCodec under test.
NewCodec Factory
// Method and Path are a request line this codec's dialect owns; MatchRequest
// must report true for it.
Method string
Path string
// ContentType is the header value a well-formed request for Method/Path
// carries. Defaults to "application/json" when empty.
ContentType string
// ValidBody is a well-formed native request body for Method/Path; served with
// ContentType, DecodeRequest must accept it without error.
ValidBody []byte
// UnmatchedMethod and UnmatchedPath each describe, independently, a request
// line this codec's dialect does not own; MatchRequest must report false for
// UnmatchedMethod paired with Path, and for Method paired with UnmatchedPath.
UnmatchedMethod string
UnmatchedPath string
// WrongContentType is a Content-Type value this codec's dialect does not
// accept for Method/Path (for example "text/plain"). DecodeRequest must
// reject ValidBody served with this header instead of the configured
// ContentType.
WrongContentType string
// MalformedBody is a syntactically invalid body (for example truncated JSON)
// for Method/Path. DecodeRequest must reject it with an error and must never
// panic.
MalformedBody []byte
// SampleResponse is fed to WriteResponse for the clean non-streaming path.
SampleResponse *inference.Response
// SampleChunks are fed to StreamEncoder.WriteChunk, in order, during the
// clean streaming path. At least one chunk is required so the streaming
// flush assertion is meaningful.
SampleChunks []content.Chunk
// SampleResult is fed to StreamEncoder.Finish to end the clean streaming
// path.
SampleResult stream.StreamResult
// SampleError is fed to WriteError and to StreamEncoder.Fail.
SampleError error
// ForeignProviderStateResponse, if set, is fed to WriteResponse to prove
// this codec never forwards a content.ThinkingBlock.ProviderState whose
// ProviderStateFormat does not match this codec's own dialect label — the
// written response bytes must not contain ForeignProviderStateMarker.
// Optional: a codec with no ProviderState-based opaque-replay mechanism
// leaves both fields at their zero value, and RejectsForeignProviderState
// is skipped rather than failing.
ForeignProviderStateResponse *inference.Response
ForeignProviderStateMarker string
}
Config parameterizes Run so the identical suite can pin every dialect's real ServerCodec as well as a hand-written fake. Every field is required; Run fails fast with a clear message if one is missing, rather than silently skipping part of the contract.
type Factory ¶
type Factory func() codec.ServerCodec
Factory builds a fresh, ready-to-use ServerCodec for the suite to exercise. Per codec.ServerCodec's own contract, implementations must be stateless and safe for concurrent use, so a Factory may legally return the same value on every call; it exists so the suite never assumes anything about how a codec is constructed.