storage

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IndexKey

type IndexKey string

IndexKey is the key for indexing responses using string concatenation. We use a single string to allow map usage while avoiding allocations during lookup.

type MockResponse

type MockResponse struct {
	RequestID       string            `json:"request_id"`
	Path            string            `json:"path"`
	Method          string            `json:"method"`
	MethodBytes     []byte            `json:"-"` // Pre-computed method as bytes to avoid allocation
	MockID          string            `json:"mock_id"`
	ContentType     string            `json:"content_type"`
	StatusCode      int               `json:"status_code"`
	Headers         map[string]string `json:"headers"`
	HeaderKeysLower map[string]string `json:"-"` // Pre-computed lowercase keys for fast lookup
	Body            []byte            // Pre-serialized body ready to send
	OriginalBody    interface{}       `json:"-"` // Keep for listing endpoints
	FullURL         string            `json:"full_url"`
	Delay           float64           `json:"delay"` // Total request duration
	SSEEvents       []SSEEvent        `json:"-"`     // SSE events with timestamps
	IsSSE           bool              `json:"-"`     // Whether this is SSE response
}

MockResponse represents a stored mock response with pre-serialized body.

type MockStorage

type MockStorage struct {
	BaseDir   string
	Responses map[IndexKey][]*MockResponse
	// ResponsesByPathMockID is indexed by "path|mockID" for Accept: */* lookups
	ResponsesByPathMockID map[IndexKey][]*MockResponse

	// Timing configuration
	ReplayTiming bool
	Jitter       float64
	// contains filtered or unexported fields
}

MockStorage handles loading and searching mock responses.

func NewMockStorage

func NewMockStorage(baseDir string) (*MockStorage, error)

NewMockStorage creates a new MockStorage instance.

func (*MockStorage) FindResponse

func (s *MockStorage) FindResponse(path, mockID, contentType, method string) *MockResponse

FindResponse is kept for backwards compatibility (mainly for tests).

func (*MockStorage) FindResponseBytes

func (s *MockStorage) FindResponseBytes(pathBytes, mockIDBytes, contentTypeBytes, methodBytes []byte) *MockResponse

FindResponse finds a mock response by path, mock_id, and content_type. Zero allocations: builds key directly from []byte without string conversion.

func (*MockStorage) FindResponseBytesAnyContentType added in v1.0.5

func (s *MockStorage) FindResponseBytesAnyContentType(pathBytes, mockIDBytes, methodBytes []byte) *MockResponse

FindResponseBytesAnyContentType finds a mock response by path and mock_id, accepting any content_type. Returns the first matching response for the given method. Zero-allocation implementation: parses key inline without string splits.

func (*MockStorage) GetMockListJSON

func (s *MockStorage) GetMockListJSON() []byte

GetMockListJSON returns pre-serialized JSON mock list (for serving).

func (*MockStorage) GetStats

func (s *MockStorage) GetStats() map[string]interface{}

GetStats returns pre-serialized statistics (for display purposes).

func (*MockStorage) GetStatsJSON

func (s *MockStorage) GetStatsJSON() []byte

GetStatsJSON returns pre-serialized JSON stats (for serving).

func (*MockStorage) HasScenarios

func (s *MockStorage) HasScenarios() bool

HasScenarios returns true when scenario-based routing is active.

func (*MockStorage) ListAllMocks

func (s *MockStorage) ListAllMocks() []*MockResponse

ListAllMocks returns all stored mock responses.

func (*MockStorage) LoadScenarioConfig

func (s *MockStorage) LoadScenarioConfig(configPath string) error

LoadScenarioConfig enables scenario-based matching using the supplied YAML file. When scenarios are present the legacy mock-id lookup path is disabled.

func (*MockStorage) MatchScenarioResponse

func (s *MockStorage) MatchScenarioResponse(pathBytes, methodBytes, body []byte) *MockResponse

MatchScenarioResponse evaluates the configured scenarios in declaration order and returns the first response whose method and filter match.

func (*MockStorage) SetTimingConfig

func (s *MockStorage) SetTimingConfig(replayTiming bool, jitter float64)

SetTimingConfig configures timing replay behavior

type NotFoundLogger added in v1.0.4

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

NotFoundLogger writes 404 request/response pairs to JSON files.

func NewNotFoundLogger added in v1.0.4

func NewNotFoundLogger(baseDir string) (*NotFoundLogger, error)

NewNotFoundLogger creates a new logger that writes to the specified directory.

func (*NotFoundLogger) LogNotFound added in v1.0.4

func (l *NotFoundLogger) LogNotFound(ctx *fasthttp.RequestCtx) error

LogNotFound logs a 404 request with its response to a JSON file.

type SSEEvent

type SSEEvent struct {
	Data           interface{} `json:"data"`
	Timestamp      float64     `json:"timestamp"`
	SerializedData []byte      `json:"-"` // Pre-serialized data for performance
}

SSEEvent represents a single SSE event with timestamp

Jump to

Keyboard shortcuts

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