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
- func BlockEventOf(sessionID, blockID, creator, name string, payload any) any
- func DefaultWelcome(Hello) any
- func EventOf(typ string, payload any) any
- func SessionEventOf(sessionID, typ string, payload any) any
- type Close
- type Error
- type HandlerFunc
- type Hello
- type Request
- type Server
- func (s *Server) Close()
- func (s *Server) CloseConn(code websocket.StatusCode, reason string) error
- func (s *Server) Drop() error
- func (s *Server) Emit(event any) (int64, error)
- func (s *Server) Errs() []error
- func (s *Server) Fail(method, code string, status int, message string)
- func (s *Server) HTTPClient() *http.Client
- func (s *Server) Handle(method string, h HandlerFunc)
- func (s *Server) Hellos() []Hello
- func (s *Server) Reply(method string, result any)
- func (s *Server) Requests() []Request
- func (s *Server) Send(msg any) error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetWelcome(f WelcomeFunc)
- func (s *Server) WaitConnected(ctx context.Context) error
- type WelcomeFunc
Constants ¶
const Subprotocol = "rex.control.v1"
Variables ¶
This section is empty.
Functions ¶
func BlockEventOf ¶
BlockEventOf is a block's own event inside a session_event.
func DefaultWelcome ¶
DefaultWelcome is the welcome a revision-7 server sends.
func SessionEventOf ¶
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").
type HandlerFunc ¶
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 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 (*Server) CloseConn ¶
func (s *Server) CloseConn(code websocket.StatusCode, reason string) error
func (*Server) Drop ¶
Drop closes the client's connection without a close frame, as a crashed server or a lost network would.
func (*Server) Emit ¶
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 ¶
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) HTTPClient ¶
func (*Server) Handle ¶
func (s *Server) Handle(method string, h HandlerFunc)
func (*Server) Send ¶
Send writes an arbitrary message, bypassing the fake's bookkeeping. Tests use it for malformed or out-of-sequence traffic.
func (*Server) SetWelcome ¶
func (s *Server) SetWelcome(f WelcomeFunc)
SetWelcome replaces the welcome behaviour for the next connection.
type WelcomeFunc ¶
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).