internal/storagetest
import "github.com/gofiber/fiber/v3/internal/storagetest"
Package storagetest provides the fiber.Storage fakes that the tests of the root package and of the storage-backed middleware keep needing: a map-backed storage with a per-operation error table and a log of the calls it served, and a storage that always fails. The fakes do not expire entries; Call.TTL records the expiration each Set was given.
Like internal/apptest, this package does not import fiber, so the in-package tests of the fiber package itself can use it without an import cycle. Every type has the full fiber.Storage method set, including the *WithContext variants, Reset and Close.
Constants
The operations whose errors Memory.SetErr configures and whose calls Memory.Calls reports.
Source: internal/storagetest/storagetest.go:28
const (
OpGet = "get"
OpSet = "set"
OpDelete = "del"
OpReset = "reset"
)Functions
func Marked(label string) context.Context
Source: internal/storagetest/storagetest.go:40
Marked returns a background context that carries label. Memory reports it as Call.Marker for every call that receives the context, so a test can tell which context reached the storage.
func MarkedCanceled(label string) context.Context
Source: internal/storagetest/storagetest.go:45
MarkedCanceled is Marked for a context that is already canceled.
func New() *Memory
Source: internal/storagetest/storagetest.go:92
New returns an empty Memory whose Data map is made, for tests that fill Data directly.
Types
type Call
Source: internal/storagetest/storagetest.go:52
Call is one operation a Memory served.
type Call struct {
// Op is OpGet, OpSet, OpDelete or OpReset.
Op string
// Key is the key the call named, empty for OpReset.
Key string
// Marker is the label of the Marked context the call received, or empty.
Marker string
// Value is a copy of the value an OpSet stored, nil for the other operations.
Value []byte
// TTL is the expiration an OpSet was given, which Memory otherwise ignores.
TTL time.Duration
// Canceled reports whether the call's context was already canceled. The
// methods without a context use a background context, which never is.
Canceled bool
}Fields
Op stringOp is OpGet, OpSet, OpDelete or OpReset.
Key stringKey is the key the call named, empty for OpReset.
Marker stringMarker is the label of the Marked context the call received, or empty.
Value []byteValue is a copy of the value an OpSet stored, nil for the other operations.
TTL time.DurationTTL is the expiration an OpSet was given, which Memory otherwise ignores.
Canceled boolCanceled reports whether the call's context was already canceled. The methods without a context use a background context, which never is.
type Failing
Source: internal/storagetest/storagetest.go:253
Failing is a storage whose every operation fails with Err, which may be nil.
type Failing struct {
// Err is what Get, Set, Delete and Reset return, with or without a context.
Err error
// CloseErr is what Close returns.
CloseErr error
// contains filtered or unexported fields
}Fields
Err errorErr is what Get, Set, Delete and Reset return, with or without a context.
CloseErr errorCloseErr is what Close returns.
func (f *Failing) Close() error
Source: internal/storagetest/storagetest.go:289
Close records that it ran and returns CloseErr.
func (f *Failing) Closed() bool
Source: internal/storagetest/storagetest.go:262
Closed reports whether Close was called.
func (f *Failing) Delete(string) error
Source: internal/storagetest/storagetest.go:280
Delete fails with Err.
func (f *Failing) DeleteWithContext(context.Context, string) error
Source: internal/storagetest/storagetest.go:277
DeleteWithContext fails with Err.
func (f *Failing) Get(string) ([]byte, error)
Source: internal/storagetest/storagetest.go:268
Get fails with Err.
func (f *Failing) GetWithContext(context.Context, string) ([]byte, error)
Source: internal/storagetest/storagetest.go:265
GetWithContext fails with Err.
func (f *Failing) Reset() error
Source: internal/storagetest/storagetest.go:286
Reset fails with Err.
func (f *Failing) ResetWithContext(context.Context) error
Source: internal/storagetest/storagetest.go:283
ResetWithContext fails with Err.
func (f *Failing) Set(string, []byte, time.Duration) error
Source: internal/storagetest/storagetest.go:274
Set fails with Err.
func (f *Failing) SetWithContext(context.Context, string, []byte, time.Duration) error
Source: internal/storagetest/storagetest.go:271
SetWithContext fails with Err.
type Memory
Source: internal/storagetest/storagetest.go:76
Memory is a map-backed storage for tests. Its zero value is ready to use.
Values are copied on Set and on Get, and expirations are ignored. An operation fails with the error SetErr configured for its key, or else for the key "*" that stands for any key. Every call is recorded, failed or not.
The methods lock Mu. Tests may read and write Data directly while no request is in flight, or while holding Mu.
type Memory struct {
// Data holds the stored values by key.
Data map[string][]byte
// Mu guards Data, the error table and the call log.
Mu sync.RWMutex
// HonorContext makes every *WithContext method fail with the error of an
// already canceled or expired context before it does anything else.
HonorContext bool
// contains filtered or unexported fields
}Fields
Data map[string][]byteData holds the stored values by key.
Mu sync.RWMutexMu guards Data, the error table and the call log.
HonorContext boolHonorContext makes every *WithContext method fail with the error of an already canceled or expired context before it does anything else.
func (m *Memory) CallKeys(op string) []string
Source: internal/storagetest/storagetest.go:152
CallKeys returns the keys of the recorded calls of op in order.
func (m *Memory) Calls(op string) []Call
Source: internal/storagetest/storagetest.go:139
Calls returns the recorded calls of op in order.
func (m *Memory) ClearCalls()
Source: internal/storagetest/storagetest.go:161
ClearCalls forgets the recorded calls and keeps the data.
func (m *Memory) ClearErrs()
Source: internal/storagetest/storagetest.go:117
ClearErrs removes every error SetErr configured.
func (*Memory) Close() error
Source: internal/storagetest/storagetest.go:250
Close does nothing and keeps the data.
func (m *Memory) Delete(key string) error
Source: internal/storagetest/storagetest.go:232
Delete is DeleteWithContext with a background context.
func (m *Memory) DeleteWithContext(ctx context.Context, key string) error
Source: internal/storagetest/storagetest.go:221
DeleteWithContext removes key.
func (m *Memory) Get(key string) ([]byte, error)
Source: internal/storagetest/storagetest.go:199
Get is GetWithContext with a background context.
func (m *Memory) GetWithContext(ctx context.Context, key string) ([]byte, error)
Source: internal/storagetest/storagetest.go:189
GetWithContext returns a copy of the value under key, or nil when there is none.
func (m *Memory) Holds(key string) bool
Source: internal/storagetest/storagetest.go:124
Holds reports whether m stores a value under key.
func (m *Memory) Keys() []string
Source: internal/storagetest/storagetest.go:132
Keys returns the stored keys in sorted order.
func (m *Memory) Reset() error
Source: internal/storagetest/storagetest.go:247
Reset is ResetWithContext with a background context.
func (m *Memory) ResetWithContext(ctx context.Context) error
Source: internal/storagetest/storagetest.go:236
ResetWithContext drops all data. Like a real storage, it keeps the errors SetErr configured; ClearErrs removes them.
func (m *Memory) Set(key string, val []byte, exp time.Duration) error
Source: internal/storagetest/storagetest.go:216
Set is SetWithContext with a background context.
func (m *Memory) SetErr(op, key string, err error) *Memory
Source: internal/storagetest/storagetest.go:99
SetErr makes op on key fail with err, key "*" making it fail on every key, or succeed again when err is nil. It returns m to chain, and panics when op is none of the Op constants, which would otherwise inject nothing.
func (m *Memory) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
Source: internal/storagetest/storagetest.go:202
SetWithContext stores a copy of val under key and ignores exp.