Documentation
¶
Overview ¶
Package testutil provides shared test helpers for solana-go. It is intended for use in _test packages only; import it with
import "github.com/MevYu/solana-go/internal/testutil"
Index ¶
- func MustHash(t testing.TB, s string) solana.Hash
- func MustNewKeypair(t testing.TB) (solana.PublicKey, ed25519.PrivateKey)
- func MustPublicKey(t testing.TB, s string) solana.PublicKey
- func NewMockRPCClient(t testing.TB, handler RPCHandler) *rpc.Client
- func NewMockRPCServer(t testing.TB, handler RPCHandler) *httptest.Server
- func ZeroHash() solana.Hash
- type RPCHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustNewKeypair ¶
MustNewKeypair generates a fresh Ed25519 keypair for use in tests. It calls t.Fatal on failure.
func MustPublicKey ¶
MustPublicKey decodes a base58 public key and calls t.Fatal on failure.
func NewMockRPCClient ¶
func NewMockRPCClient(t testing.TB, handler RPCHandler) *rpc.Client
NewMockRPCClient creates a mock HTTP server using handler and returns an rpc.Client pointed at it.
func NewMockRPCServer ¶
func NewMockRPCServer(t testing.TB, handler RPCHandler) *httptest.Server
NewMockRPCServer starts an httptest.Server that answers JSON-RPC 2.0 requests by dispatching to handler. It registers a cleanup function that stops the server when the test ends.
Example:
srv := testutil.NewMockRPCServer(t, func(method string, params json.RawMessage) (any, error) {
if method == "getBalance" {
return map[string]any{"context": map[string]any{"slot": 100}, "value": uint64(500)}, nil
}
return nil, fmt.Errorf("unexpected method %s", method)
})
client := rpc.NewClient(srv.URL, jsonrpc.Config{})
Types ¶
type RPCHandler ¶
type RPCHandler func(method string, params json.RawMessage) (any, error)
RPCHandler is a function that handles a single JSON-RPC method call in a mock server. It receives the method name and the raw params JSON, and returns a result value to be serialised into the response.