clienttest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clienttest is an in-process fake of Rex's control endpoint for hermetic tests of rexuikit/client and the packages built on it.

It serves over httptest's TCP loopback rather than a unix socket because the sandbox the default test run uses denies unix-socket bind(); a unix fake would fail every sandboxed `make check`. Server is also an http.Handler, so a test that can bind a unix socket may serve it there.

The fake speaks the wire protocol from docs/rex-api/llms-r7.md ("## The Control Connection") and nothing more: methods answer only what a test registers with Handle, and everything else gets method_not_found.

It deliberately does not import rexuikit/client, so tests inside package client can use it without an import cycle.

Index

Constants

View Source
const Subprotocol = "rex.control.v1"

Variables

This section is empty.

Functions

func BlockEventOf

func BlockEventOf(sessionID, blockID, creator, name string, payload any) any

BlockEventOf is a block's own event inside a session_event.

func DefaultWelcome

func DefaultWelcome(Hello) any

DefaultWelcome is the welcome a revision-7 server sends.

func EventOf

func EventOf(typ string, payload any) any

EventOf is a top-level event such as session_created.

func SessionEventOf

func SessionEventOf(sessionID, typ string, payload any) any

SessionEventOf wraps an event in a session_event ("### Events").

Types

type Close

type Close struct {
	Code   websocket.StatusCode
	Reason string
}

Close, returned from a WelcomeFunc, makes the fake close the connection with this status instead of welcoming the client, as the real server does for no common API revision (status 1008).

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Status  int    `json:"status"`
}

Error is a scripted error response ("### Errors").

func (*Error) Error

func (e *Error) Error() string

type HandlerFunc

type HandlerFunc func(ctx context.Context, req Request) (result any, err *Error)

HandlerFunc answers one request. It runs on its own goroutine, so a handler may block (on ctx or a test channel) to force responses out of order. ctx ends when the connection does. A non-nil *Error becomes an error response; result is marshalled as the payload otherwise.

type Hello

type Hello struct {
	Type           string          `json:"type"`
	APIRevision    int             `json:"api_revision"`
	APIMinRevision int             `json:"api_min_revision"`
	ReconnectToken string          `json:"reconnect_token,omitempty"`
	Client         json.RawMessage `json:"client"`
	Raw            []byte          `json:"-"`
}

type Request

type Request struct {
	ID      string          `json:"id"`
	Method  string          `json:"method"`
	Payload json.RawMessage `json:"payload"`
}

type Server

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

Server is the fake. Configure it before the client dials.

func New

func New(t testing.TB) *Server

New starts a fake on TCP loopback and closes it when the test ends.

func (*Server) Close

func (s *Server) Close()

func (*Server) CloseConn

func (s *Server) CloseConn(code websocket.StatusCode, reason string) error

func (*Server) Drop

func (s *Server) Drop() error

Drop closes the client's connection without a close frame, as a crashed server or a lost network would.

func (*Server) Emit

func (s *Server) Emit(event any) (int64, error)

Emit sends an event to the connected client with the next seq and returns that seq. event is the inner event, {type, payload}; build one with EventOf, SessionEventOf or BlockEventOf.

func (*Server) Errs

func (s *Server) Errs() []error

Errs returns protocol violations and I/O failures the fake saw. The fake never calls t.Fatal from its goroutines (that would hang the test); tests assert on this instead.

func (*Server) Fail

func (s *Server) Fail(method, code string, status int, message string)

Fail registers a handler that always fails with the given error.

func (*Server) HTTPClient

func (s *Server) HTTPClient() *http.Client

func (*Server) Handle

func (s *Server) Handle(method string, h HandlerFunc)

func (*Server) Hellos

func (s *Server) Hellos() []Hello

func (*Server) Reply

func (s *Server) Reply(method string, result any)

Reply registers a handler that always returns result.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns every request received, in arrival order.

func (*Server) Send

func (s *Server) Send(msg any) error

Send writes an arbitrary message, bypassing the fake's bookkeeping. Tests use it for malformed or out-of-sequence traffic.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) SetWelcome

func (s *Server) SetWelcome(f WelcomeFunc)

SetWelcome replaces the welcome behaviour for the next connection.

func (*Server) WaitConnected

func (s *Server) WaitConnected(ctx context.Context) error

WaitConnected blocks until a client has been welcomed, or ctx ends.

type WelcomeFunc

type WelcomeFunc func(Hello) any

WelcomeFunc chooses the server's first message for a hello. Return a value to marshal and send, a Close to refuse the client, or nil to send nothing at all (a server that never welcomes).

Jump to

Keyboard shortcuts

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