mocktunnelservice

package
v0.0.11-dev Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleMockTunnelService_sessionPropagation

func ExampleMockTunnelService_sessionPropagation()

ExampleMockTunnelService_sessionPropagation enables session propagation so subsequent commands receive the MCP session headers captured from responses.

func ExampleNewMockTunnelService_basic

func ExampleNewMockTunnelService_basic()

ExampleNewMockTunnelService_basic configures a mock service with a single scripted command and expected response.

func NewCommand

func NewCommand(requestID string, jsonrpc json.RawMessage, headers http.Header) json.RawMessage

NewCommand builds a raw tunnel command payload with the supplied JSON-RPC request and headers.

func NewOAuthDiscoveryCommand

func NewOAuthDiscoveryCommand(requestID string, headers http.Header) json.RawMessage

NewOAuthDiscoveryCommand builds a raw tunnel command payload for OAuth discovery.

func NewSessionTerminationCommand

func NewSessionTerminationCommand(requestID string, headers http.Header) json.RawMessage

NewSessionTerminationCommand builds a raw tunnel command payload for explicit MCP session close.

Types

type CommandMutator

type CommandMutator func(json.RawMessage, SharedStorage) json.RawMessage

CommandMutator can modify a command payload before it is delivered to the client.

type CommandResponse

type CommandResponse struct {
	Command           json.RawMessage
	CommandMutator    CommandMutator
	DeliverAfter      <-chan struct{}
	ExpectedResponses []ExpectedResponse
	// NoResponseExpected marks the command complete as soon as it is delivered.
	// Use it for paths where the client intentionally does not POST a response,
	// such as a dispatcher connection TTL expiring while a request is in flight.
	NoResponseExpected bool
}

CommandResponse ties a polled command to the response the mock expects.

type ExpectedResponse

type ExpectedResponse struct {
	RequestID   string
	Headers     http.Header
	Assert      func(testing.TB, ReceivedResponse)
	PostProcess ResponsePostProcessor
}

ExpectedResponse defines one expected POST /v1/tunnels/{tunnel_id}/response payload.

type IncomingHTTPRequest

type IncomingHTTPRequest struct {
	Method   string
	Path     string
	RawQuery string
	Headers  http.Header
}

IncomingHTTPRequest captures an HTTP request received by the mock tunnel service.

type MockTunnelService

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

MockTunnelService simulates the subset of tunnel-service endpoints the control-plane client calls.

func NewMockTunnelService

func NewMockTunnelService(opts ...Option) *MockTunnelService

NewMockTunnelService constructs a mock with optional configuration overrides.

func (*MockTunnelService) APIKey

func (m *MockTunnelService) APIKey() string

APIKey exposes the API key the client should send in Authorization headers.

func (*MockTunnelService) AllowPending

func (m *MockTunnelService) AllowPending()

AllowPending enables skipping pending command assertions on shutdown.

func (*MockTunnelService) BaseURL

func (m *MockTunnelService) BaseURL() *url.URL

BaseURL returns the base URL the client should target.

func (*MockTunnelService) Close

func (m *MockTunnelService) Close()

Close stops the server and asserts all commands and expected responses were consumed.

func (*MockTunnelService) DeliveredCommands

func (m *MockTunnelService) DeliveredCommands() []json.RawMessage

DeliveredCommands returns a snapshot of all commands that have been issued to clients.

func (*MockTunnelService) PendingScriptDebugInfo

func (m *MockTunnelService) PendingScriptDebugInfo() (pendingCommands []json.RawMessage, awaitingResponses []json.RawMessage)

PendingScriptDebugInfo returns the outstanding script commands that have not yet been delivered as well as those waiting on a response.

func (*MockTunnelService) ReceivedHTTPRequests

func (m *MockTunnelService) ReceivedHTTPRequests() []IncomingHTTPRequest

ReceivedHTTPRequests returns all HTTP requests observed by the mock tunnel service.

func (*MockTunnelService) ReceivedResponses

func (m *MockTunnelService) ReceivedResponses(filter ResponseMatchFilter) []ReceivedResponse

ReceivedResponses returns a snapshot of all captured responses.

func (*MockTunnelService) SharedStorageSnapshot

func (m *MockTunnelService) SharedStorageSnapshot() map[string]string

SharedStorageSnapshot returns a copy of the shared storage contents.

func (*MockTunnelService) Start

func (m *MockTunnelService) Start(t testing.TB)

Start launches the underlying httptest.Server and registers cleanup with t.

func (*MockTunnelService) WaitForHTTPRequests

func (m *MockTunnelService) WaitForHTTPRequests(
	ctx context.Context,
	n int,
	predicate func(IncomingHTTPRequest) bool,
) error

WaitForHTTPRequests blocks until at least n recorded HTTP requests satisfy predicate.

func (*MockTunnelService) WaitForResponses

func (m *MockTunnelService) WaitForResponses(ctx context.Context, n int) error

WaitForResponses blocks until at least n responses have been observed or ctx expires.

func (*MockTunnelService) WaitUntilIdle

func (m *MockTunnelService) WaitUntilIdle(ctx context.Context) error

WaitUntilIdle blocks until every scripted command has been delivered and all expected responses have been observed, or until ctx expires.

type Option

type Option func(*MockTunnelService)

Option configures a MockTunnelService instance.

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey overrides the randomly generated API key used by the mock.

func WithAllowPendingCommands

func WithAllowPendingCommands() Option

WithAllowPendingCommands skips strict pending command/response assertions on Close.

func WithCommandResponses

func WithCommandResponses(commands ...CommandResponse) Option

WithCommandResponses appends scripted command/response pairs to the queue using the Option pattern.

func WithInitializationPhaseCommands

func WithInitializationPhaseCommands() Option

WithInitializationPhaseCommands pre-populates the mock with the standard MCP initialization handshake commands (initialize + notifications/initialized) and asserts the responses comply with the protocol. The MCP session id captured in the initialize response is persisted into shared storage so it can be propagated to subsequent commands via WithSessionHeaderPropagation.

func WithInitializationPhaseCommandsWithoutSessionHeaders

func WithInitializationPhaseCommandsWithoutSessionHeaders() Option

WithInitializationPhaseCommandsWithoutSessionHeaders pre-populates the mock with the standard MCP initialization handshake commands (initialize + notifications/initialized) but skips asserting session header propagation in the initialize response.

func WithPollWaitLimit

func WithPollWaitLimit(limit time.Duration) Option

WithPollWaitLimit overrides the amount of time GET /poll should block before returning an empty command list.

func WithSessionHeaderPropagation

func WithSessionHeaderPropagation() Option

WithSessionHeaderPropagation enables automatic propagation of MCP session headers between responses and subsequent commands.

func WithTunnelID

func WithTunnelID(id string) Option

WithTunnelID overrides the tunnel identifier the mock expects in request paths.

func WithUnixSocketPath

func WithUnixSocketPath(path string) Option

WithUnixSocketPath makes the mock serve HTTP over the provided Unix-domain socket.

type ReceivedResponse

type ReceivedResponse struct {
	RequestID       string
	JSONResponse    json.RawMessage
	ResponseHeaders http.Header
	ResponseCode    int
	ResponseType    string
	MatchedCommand  bool
}

ReceivedResponse captures a single payload posted to the mock tunnel-service.

type ResponseMatchFilter

type ResponseMatchFilter int

ResponseMatchFilter controls which received responses are returned when querying the mock.

const (
	// ResponseMatchAll returns every received response regardless of whether it matched a command.
	ResponseMatchAll ResponseMatchFilter = iota
	// ResponseMatchUnexpected returns only responses that did not correspond to a delivered command.
	ResponseMatchUnexpected
	// ResponseMatchMatched returns only responses that matched an existing command.
	ResponseMatchMatched
)

type ResponsePostProcessor

type ResponsePostProcessor func(ReceivedResponse, SharedStorage)

ResponsePostProcessor captures data from a received response into shared storage.

type SharedStorage

type SharedStorage interface {
	Get(key string) (string, bool)
	Set(key, value string)
	Delete(key string)
}

SharedStorage exposes a concurrency-safe map that command and response processors can use.

Jump to

Keyboard shortcuts

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