Documentation
¶
Overview ¶
Package testserver provides an embedded test server for go-trust.
This package allows dependent applications to create lightweight test servers for integration testing without running a full go-trust instance with pipelines. It provides configurable mock registries and supports both httptest.Server integration and standalone usage.
Basic Usage ¶
Create a simple test server that accepts all requests:
srv := testserver.New(testserver.WithAcceptAll()) defer srv.Close() client := authzenclient.New(srv.URL()) resp, err := client.Evaluate(ctx, req)
Custom Mock Registries ¶
Create a server with specific trust responses:
srv := testserver.New(
testserver.WithMockRegistry("test-registry", true, []string{"x5c", "jwk"}),
)
defer srv.Close()
Decision Callback ¶
Use a callback for dynamic responses:
srv := testserver.New(
testserver.WithDecisionFunc(func(req *authzen.EvaluationRequest) (*authzen.EvaluationResponse, error) {
if req.Subject.ID == "trusted-subject" {
return &authzen.EvaluationResponse{Decision: true}, nil
}
return &authzen.EvaluationResponse{Decision: false}, nil
}),
)
HTTP Handler ¶
Get the HTTP handler for use with custom test setups:
handler := testserver.NewHandler(testserver.WithAcceptAll()) srv := httptest.NewServer(handler)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler creates an http.Handler for use with custom test setups. Use this when you need more control over the test server lifecycle. Note: WithBaseURL should be set explicitly when using NewHandler.
Types ¶
type DecisionFunc ¶
type DecisionFunc func(req *authzen.EvaluationRequest) (*authzen.EvaluationResponse, error)
DecisionFunc is a function that returns a trust decision for a given request.
type Option ¶
type Option func(*serverConfig)
Option configures a test server.
func WithAcceptAll ¶
func WithAcceptAll() Option
WithAcceptAll configures the server to accept all trust requests.
func WithBaseURL ¶
WithBaseURL sets the base URL reported in AuthZEN discovery. If not set, the httptest server URL is used.
func WithDecisionFunc ¶
func WithDecisionFunc(fn DecisionFunc) Option
WithDecisionFunc sets a callback function for dynamic trust decisions. This takes precedence over mock registries when set.
func WithMockRegistry ¶
WithMockRegistry adds a mock registry with the specified behavior.
func WithRegistry ¶
func WithRegistry(reg registry.TrustRegistry) Option
WithRegistry adds a custom TrustRegistry implementation.
func WithRejectAll ¶
func WithRejectAll() Option
WithRejectAll configures the server to reject all trust requests.