testserver

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2026 License: BSD-2-Clause-Views Imports: 9 Imported by: 0

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

func NewHandler(opts ...Option) http.Handler

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

func WithBaseURL(url string) Option

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

func WithMockRegistry(name string, decision bool, resourceTypes []string) Option

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.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is an embedded test server for go-trust AuthZEN endpoints.

func New

func New(opts ...Option) *Server

New creates a new embedded test server.

func (*Server) Client

func (s *Server) Client() *http.Client

Client returns the HTTP client configured for the test server.

func (*Server) Close

func (s *Server) Close()

Close shuts down the test server.

func (*Server) URL

func (s *Server) URL() string

URL returns the base URL of the test server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL