Documentation
¶
Overview ¶
Package wsmock provides an in-memory WebSocket mock server for Go testing. It simplifies testing WebSocket clients by providing declarative request-response expectations, chaos fault injection (abrupt socket drops, custom close frames, delays), connection lifecycle hooks, and message assertions.
Index ¶
- type Action
- type Conn
- type Expectation
- func (e *Expectation) AnyTimes() *Expectation
- func (e *Expectation) CloseWithCode(code int, reason string) *Expectation
- func (e *Expectation) Delay(d time.Duration) *Expectation
- func (e *Expectation) DropConnection() *Expectation
- func (e *Expectation) Once() *Expectation
- func (e *Expectation) Reply(msg string) *Expectation
- func (e *Expectation) ReplyBinary(data []byte) *Expectation
- func (e *Expectation) ReplyJSON(v any) *Expectation
- func (e *Expectation) Times(n int) *Expectation
- type Matcher
- type Option
- type ReceivedMessage
- type Server
- func (s *Server) AssertExpectationsMet()
- func (s *Server) AssertReceived(expected string)
- func (s *Server) AssertReceivedCount(expectedCount int)
- func (s *Server) AssertReceivedJSON(expected any)
- func (s *Server) Broadcast(msg string)
- func (s *Server) BroadcastJSON(v any) error
- func (s *Server) ClientCount() int
- func (s *Server) Close()
- func (s *Server) Connections() []*Conn
- func (s *Server) ExpectBinary(data []byte) *Expectation
- func (s *Server) ExpectCustom(matcher Matcher, desc string) *Expectation
- func (s *Server) ExpectJSON(v any) *Expectation
- func (s *Server) ExpectMessage(msg string) *Expectation
- func (s *Server) OnConnect(fn func(*Conn)) *Server
- func (s *Server) OnDisconnect(fn func(*Conn)) *Server
- func (s *Server) ReceivedMessages() []ReceivedMessage
- func (s *Server) URL() string
- func (s *Server) WaitMessages(count int, timeout time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents an active client connection to the mock server.
func (*Conn) CloseWithCode ¶
CloseWithCode cleanly closes the WebSocket connection with a specific RFC 6455 code and reason.
func (*Conn) DropConnection ¶
DropConnection abruptly closes the underlying TCP connection without sending a WebSocket close frame, simulating unexpected network loss or process crash.
func (*Conn) SendBinary ¶
SendBinary sends binary payload to this connection.
func (*Conn) SendMessage ¶
SendMessage sends a text message to this connection.
type Expectation ¶
type Expectation struct {
// contains filtered or unexported fields
}
Expectation represents a mocked client-server interaction rule.
func (*Expectation) AnyTimes ¶
func (e *Expectation) AnyTimes() *Expectation
AnyTimes removes any invocation limit on this expectation.
func (*Expectation) CloseWithCode ¶
func (e *Expectation) CloseWithCode(code int, reason string) *Expectation
CloseWithCode cleanly closes the connection with code & reason upon match.
func (*Expectation) Delay ¶
func (e *Expectation) Delay(d time.Duration) *Expectation
Delay introduces an artificial response latency before subsequent actions.
func (*Expectation) DropConnection ¶
func (e *Expectation) DropConnection() *Expectation
DropConnection drops the connection when this expectation matches.
func (*Expectation) Once ¶
func (e *Expectation) Once() *Expectation
Once is a shorthand for Times(1).
func (*Expectation) Reply ¶
func (e *Expectation) Reply(msg string) *Expectation
Reply queues a text response back to the client.
func (*Expectation) ReplyBinary ¶
func (e *Expectation) ReplyBinary(data []byte) *Expectation
ReplyBinary queues binary bytes back to the client.
func (*Expectation) ReplyJSON ¶
func (e *Expectation) ReplyJSON(v any) *Expectation
ReplyJSON serializes v as JSON and queues it back to the client.
func (*Expectation) Times ¶
func (e *Expectation) Times(n int) *Expectation
Times sets the number of times this expectation is allowed to match.
type ReceivedMessage ¶
ReceivedMessage represents an incoming message recorded by the mock server.
func (ReceivedMessage) JSON ¶
func (m ReceivedMessage) JSON(target any) error
JSON unmarshals the payload into the given target pointer.
func (ReceivedMessage) Text ¶
func (m ReceivedMessage) Text() string
Text returns the payload as a string.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the in-memory mock WebSocket server.
func (*Server) AssertExpectationsMet ¶
func (s *Server) AssertExpectationsMet()
AssertExpectationsMet verifies that all registered expectations were satisfied.
func (*Server) AssertReceived ¶
AssertReceived fails the test if no message matching expected was received.
func (*Server) AssertReceivedCount ¶
AssertReceivedCount fails the test if total received messages != expectedCount.
func (*Server) AssertReceivedJSON ¶
AssertReceivedJSON fails the test if no message matching expected JSON was received.
func (*Server) BroadcastJSON ¶
BroadcastJSON serializes v and sends it to all currently connected clients.
func (*Server) ClientCount ¶
ClientCount returns the number of currently active connections.
func (*Server) Close ¶
func (s *Server) Close()
Close terminates the mock server and cleans up all active connections.
func (*Server) Connections ¶
Connections returns a snapshot of currently active connections.
func (*Server) ExpectBinary ¶
func (s *Server) ExpectBinary(data []byte) *Expectation
ExpectBinary registers an expectation matching exact binary payloads.
func (*Server) ExpectCustom ¶
func (s *Server) ExpectCustom(matcher Matcher, desc string) *Expectation
ExpectCustom registers an expectation with custom matcher logic.
func (*Server) ExpectJSON ¶
func (s *Server) ExpectJSON(v any) *Expectation
ExpectJSON registers an expectation matching incoming JSON messages.
func (*Server) ExpectMessage ¶
func (s *Server) ExpectMessage(msg string) *Expectation
ExpectMessage registers an expectation matching incoming text messages exactly.
func (*Server) OnConnect ¶
OnConnect registers a callback invoked whenever a new WebSocket connection is established.
func (*Server) OnDisconnect ¶
OnDisconnect registers a callback invoked whenever a WebSocket connection closes.
func (*Server) ReceivedMessages ¶
func (s *Server) ReceivedMessages() []ReceivedMessage
ReceivedMessages returns all recorded messages received by the server.