Documentation
¶
Overview ¶
Package testutil exposes test fixtures and mock servers used by the SDK's internal tests. It is internal-only.
Index ¶
- type FakeCall
- type FakeHandler
- type FakeTransport
- func (f *FakeTransport) Call(_ context.Context, method string, params, out any) error
- func (f *FakeTransport) Calls() []FakeCall
- func (f *FakeTransport) Close() error
- func (f *FakeTransport) Handle(method string, h FakeHandler)
- func (f *FakeTransport) HandleError(method string, err error)
- func (f *FakeTransport) HandleResult(method string, result any)
- func (f *FakeTransport) LastCall() FakeCall
- func (f *FakeTransport) Reset()
- type MockHandler
- type MockRequest
- type MockServer
- type MockWSHandler
- type MockWSServer
- func (m *MockWSServer) Close()
- func (m *MockWSServer) DropClients()
- func (m *MockWSServer) Handle(method string, h MockWSHandler)
- func (m *MockWSServer) HandleResult(method string, result any)
- func (m *MockWSServer) Notify(channel string, data any)
- func (m *MockWSServer) Subscribed(channel string) bool
- func (m *MockWSServer) URL() string
- func (m *MockWSServer) WaitClients(n int, timeout time.Duration) bool
- func (m *MockWSServer) WaitSubscribed(channel string, timeout time.Duration) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakeCall ¶
type FakeCall struct {
Method string
Params json.RawMessage
}
FakeCall is a record of one Call invocation.
type FakeHandler ¶
type FakeHandler func(params json.RawMessage) (any, error)
FakeHandler returns the result and/or error for one method.
To reply with a JSON-RPC application error, return ( nil, &derrors.APIError{...} ). If the handler is unset, Call returns an "unhandled method" error.
type FakeTransport ¶
type FakeTransport struct {
// contains filtered or unexported fields
}
FakeTransport implements transport.Transport without any network access. Tests register handlers per-method and inspect captured calls afterwards.
func NewFakeTransport ¶
func NewFakeTransport() *FakeTransport
NewFakeTransport returns an empty FakeTransport.
func (*FakeTransport) Calls ¶
func (f *FakeTransport) Calls() []FakeCall
Calls returns a snapshot of recorded calls in order.
func (*FakeTransport) Close ¶
func (f *FakeTransport) Close() error
Close satisfies transport.Transport.
func (*FakeTransport) Handle ¶
func (f *FakeTransport) Handle(method string, h FakeHandler)
Handle registers a handler for one method name. Replaces any previous handler for that method.
func (*FakeTransport) HandleError ¶
func (f *FakeTransport) HandleError(method string, err error)
HandleError is a shortcut for Handle that always returns the given error.
func (*FakeTransport) HandleResult ¶
func (f *FakeTransport) HandleResult(method string, result any)
HandleResult is a shortcut for Handle that always returns the given value.
func (*FakeTransport) LastCall ¶
func (f *FakeTransport) LastCall() FakeCall
LastCall returns the most recent call, or zero-value if none.
func (*FakeTransport) Reset ¶
func (f *FakeTransport) Reset()
Reset clears recorded calls but keeps registered handlers.
type MockHandler ¶
type MockHandler func(req MockRequest) (any, *jsonrpc.Error)
MockHandler returns either a result value (any json-marshalable) or an error.
type MockRequest ¶
type MockRequest struct {
Method string
Params json.RawMessage
Headers http.Header
}
MockRequest is a record of one received call.
type MockServer ¶
type MockServer struct {
// contains filtered or unexported fields
}
MockServer is a tiny JSON-RPC HTTP server. Register one or more handlers per method name; unhandled methods return MethodNotFound. The HTTP method must always be POST.
func NewMockServer ¶
func NewMockServer() *MockServer
NewMockServer starts an httptest server and returns it.
func (*MockServer) Handle ¶
func (m *MockServer) Handle(method string, h MockHandler)
Handle registers a handler for one JSON-RPC method.
func (*MockServer) Requests ¶
func (m *MockServer) Requests() []MockRequest
Requests returns a snapshot of received requests.
type MockWSHandler ¶
type MockWSHandler func(params json.RawMessage) (result any, err *jsonrpc.Error)
MockWSHandler returns the typed response for one JSON-RPC method. Returning err != nil writes a JSON-RPC error frame.
type MockWSServer ¶
type MockWSServer struct {
// contains filtered or unexported fields
}
MockWSServer is an in-process JSON-RPC WebSocket server for testing. Register handlers per method; push notifications via Notify.
func NewMockWSServer ¶
func NewMockWSServer() *MockWSServer
NewMockWSServer starts the server. Defer Close().
func (*MockWSServer) Close ¶
func (m *MockWSServer) Close()
Close shuts down all connections and the underlying HTTP server.
func (*MockWSServer) DropClients ¶ added in v0.18.0
func (m *MockWSServer) DropClients()
DropClients severs every currently-open client connection while leaving the underlying HTTP server running on the same port — so a reconnecting client can redial against the same URL and succeed. Use this to simulate a transient connection drop in tests.
func (*MockWSServer) Handle ¶
func (m *MockWSServer) Handle(method string, h MockWSHandler)
Handle registers a handler for one method.
func (*MockWSServer) HandleResult ¶
func (m *MockWSServer) HandleResult(method string, result any)
HandleResult is a shortcut for Handle that always returns result.
func (*MockWSServer) Notify ¶
func (m *MockWSServer) Notify(channel string, data any)
Notify pushes a subscription notification to every connection currently subscribed to the channel.
func (*MockWSServer) Subscribed ¶
func (m *MockWSServer) Subscribed(channel string) bool
Subscribed reports whether at least one connection has subscribed to channel.
func (*MockWSServer) URL ¶
func (m *MockWSServer) URL() string
URL returns the ws:// URL for clients to dial.
func (*MockWSServer) WaitClients ¶ added in v0.18.0
func (m *MockWSServer) WaitClients(n int, timeout time.Duration) bool
WaitClients blocks until at least n connections have been registered on the server, or the timeout fires. The server records each new client only after upgrader.Upgrade returns, which is after the client's dial-side handshake completes — so a test that calls DropClients immediately after Connect can race with the server's registration. Call WaitClients first to close that window.
func (*MockWSServer) WaitSubscribed ¶
func (m *MockWSServer) WaitSubscribed(channel string, timeout time.Duration) bool
WaitSubscribed blocks until any connection has subscribed to channel or the timeout fires.