testserver

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testserver provides a configurable HTTP test server with delay simulation capabilities for testing concurrent request scenarios.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create[T Model](repository Repository) http.HandlerFunc

func GetAll

func GetAll(repository Repository) http.HandlerFunc

func GetByID

func GetByID(repository Repository) http.HandlerFunc

func Healthcheck

func Healthcheck(serverID *int) http.HandlerFunc

func NewMux

func NewMux(config *Server, repository *Provider) http.Handler

Types

type ErrorMessage

type ErrorMessage struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
}

type HelthcheckMessage

type HelthcheckMessage struct {
	ServerID  string `json:"server_id"`
	Status    string `json:"status"`
	Timestamp string `json:"timestamp"`
}

type Impl

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

func (*Impl) Create

func (i *Impl) Create(model Model) (created Model)

Create implements Repository.

func (*Impl) Delete

func (i *Impl) Delete(id uint)

Delete implements Repository.

func (*Impl) GetAll

func (i *Impl) GetAll() (models []Model)

GetAll implements Repository.

func (*Impl) GetByID

func (i *Impl) GetByID(id uint) (model Model, found bool)

GetByID implements Repository.

type Manager

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

func NewManager

func NewManager() *Manager

func (*Manager) NewServer

func (m *Manager) NewServer() *httptest.Server

func (*Manager) NewServerBuilder

func (m *Manager) NewServerBuilder() *ServerBuilder

type Middleware

type Middleware func(http.Handler) http.Handler

func BusyMiddleware

func BusyMiddleware(config *Server) Middleware

func HeaderDebugMiddleware

func HeaderDebugMiddleware(config *Server) Middleware

func SleepMiddleware

func SleepMiddleware(config *Server) Middleware

type Model

type Model interface {
	SetID(id uint)
	GetID() uint
	String() string
}

type Namespace

type Namespace string
const (
	UserNamespace     Namespace = "users"
	ResourceNamespace Namespace = "resources"
)

type Provider

type Provider struct {
	User     Repository
	Resource Repository
}

func NewProvider

func NewProvider(state *State) *Provider

type Repository

type Repository interface {
	GetAll() (models []Model)
	GetByID(id uint) (model Model, found bool)
	Create(model Model) (created Model)
	Delete(id uint)
}

type Resource

type Resource struct {
	ID        uint      `json:"id"`
	Type      string    `json:"type"`
	Data      string    `json:"data"`
	Timestamp time.Time `json:"timestamp"`
}

func (*Resource) GetID

func (r *Resource) GetID() uint

GetID implements Model.

func (*Resource) SetID

func (r *Resource) SetID(id uint)

SetID implements Model.

func (*Resource) String

func (r *Resource) String() string

String implements Model.

type Server

type Server struct {
	ID                int
	URL               string
	EnableBusy        bool
	EnableHeaderDebug bool

	// Interval specifies the base sleep duration for request delays
	Interval time.Duration
	// BackoffRate specifies the multiplier for exponential backoff (e.g., 2.0 for doubling delay)
	BackoffRate float64
}

type ServerBuilder

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

func (*ServerBuilder) Build

func (b *ServerBuilder) Build() *httptest.Server

func (*ServerBuilder) EnableBusy

func (b *ServerBuilder) EnableBusy() *ServerBuilder

func (*ServerBuilder) EnableHeaderDebug

func (b *ServerBuilder) EnableHeaderDebug() *ServerBuilder

func (*ServerBuilder) SleepFor

func (b *ServerBuilder) SleepFor(d time.Duration, backoff float64) *ServerBuilder

type State

type State map[Namespace]Table

func NewState

func NewState() *State

type Table

type Table map[uint]Model

type User

type User struct {
	ID        uint      `json:"id"`
	Name      string    `json:"name"`
	Birthdate time.Time `json:"birthdate"`
}

func (*User) GetID

func (u *User) GetID() uint

GetID implements Model.

func (*User) SetID

func (u *User) SetID(id uint)

SetID implements Model.

func (*User) String

func (u *User) String() string

String implements Model.

Jump to

Keyboard shortcuts

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