mock

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package mock provides an HTTP handler that serves mock responses from OpenAPI schemas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSMiddleware

func CORSMiddleware(next http.Handler) http.Handler

CORSMiddleware adds CORS headers to all responses and handles OPTIONS preflight.

func IndexHandler

func IndexHandler(v *cvt.Validator, schemaIDs []string) http.Handler

IndexHandler returns an http.Handler that renders the endpoint index page.

Types

type HandlerConfig

type HandlerConfig struct {
	UseExamples      bool
	ValidateRequests bool
	LatencyMs        int
	Quiet            bool
}

HandlerConfig controls mock handler behavior.

type MockHandler

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

MockHandler serves mock HTTP responses from OpenAPI schemas.

func NewMockHandler

func NewMockHandler(v *cvt.Validator, schemaIDs []string, cfg HandlerConfig) *MockHandler

NewMockHandler creates a new mock HTTP handler.

func (*MockHandler) RecoverMiddleware

func (h *MockHandler) RecoverMiddleware(next http.Handler) http.Handler

RecoverMiddleware wraps an http.Handler with panic recovery that returns 500 JSON.

func (*MockHandler) ServeHTTP

func (h *MockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles incoming HTTP requests by matching them against registered OpenAPI schemas and returning generated mock responses.

type Server

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

Server is a mock HTTP server that generates responses from OpenAPI schemas.

func NewServer

func NewServer(cfg ServerConfig) *Server

NewServer creates a new mock server with the given configuration.

func (*Server) Start

func (s *Server) Start() error

Start loads schemas, starts the HTTP server, and blocks until shutdown.

type ServerConfig

type ServerConfig struct {
	Host             string
	Port             int
	SchemaFiles      []string // file paths or URLs
	Watch            bool
	ValidateRequests bool
	UseExamples      bool
	LatencyMs        int
	Quiet            bool
	Seed             *uint64 // Random seed for deterministic responses (nil = random)
}

ServerConfig holds configuration for the mock server.

type Watcher

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

Watcher watches schema files for changes and triggers a reload callback.

func NewWatcher

func NewWatcher(files []string, onReload func()) (*Watcher, error)

NewWatcher creates a file watcher that calls onReload when any watched file changes.

func (*Watcher) Close

func (w *Watcher) Close()

Close stops the watcher.

Jump to

Keyboard shortcuts

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