Documentation ¶
Overview ¶
Package mock implements a scripted IMAP server for testing client behavior.
The mock server understands low-level details of the IMAP protocol (lines, literals, compression, encryption, etc.). It doesn't know anything about commands, users, mailboxes, messages, or any other high-level concepts. The server follows a script that tells it what to send/receive and when. Everything received from the client is checked against the script and an error is returned if there is a mismatch.
See mock_test.go for examples of how to use this package in your unit tests.
Index ¶
- Variables
- func NewConn(addrA, addrB string, bufSize int) (A *Conn, B *Conn)
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Read(b []byte) (n int, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetTimeout(d time.Duration) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) Write(b []byte) (n int, err error)
- type Recv
- type ScriptFunc
- type Send
- type T
Constants ¶
This section is empty.
Variables ¶
var ( STARTTLS = func(s imap.MockServer) error { return s.EnableTLS(serverTLS()) } DEFLATE = func(s imap.MockServer) error { return s.EnableDeflate(-1) } CLOSE = func(s imap.MockServer) error { return s.Close(true) } )
Predefined script actions for controlling server state.
var ServerName = "imap.mock.net"
ServerName is the hostname used by the scripted server.
var Timeout = 500 * time.Millisecond
Timeout is the maximum execution time for each Read and Write call on the simulated network connection. When the client or server aborts execution due to an unexpected error, this timeout prevents the other side from blocking indefinitely.
Functions ¶
func NewConn ¶
NewConn creates a pair of connected net.Conn instances. The addresses are arbitrary strings used to distinguish the two ends of the connection. bufSize is the maximum number of bytes that can be written to each connection before Write will block. A value <= 0 for bufSize means use a default of 4096 bytes.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is an in-memory implementation of net.Conn.
func (*Conn) Close ¶
Close closes the connection. Any blocked Read or Write operations will be unblocked and return errors.
func (*Conn) Read ¶
Read reads data from the connection. It can be made to time out and return a net.Error with Timeout() == true after a deadline or a per-Read timeout; see SetDeadline, SetReadDeadline, and SetTimeout.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*Conn) SetDeadline ¶
SetDeadline sets the Read and Write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
func (*Conn) SetReadDeadline ¶
SetReadDeadline sets the deadline for future Read calls. A zero value for t means Read will not time out (but see SetTimeout).
func (*Conn) SetTimeout ¶
SetTimeout sets the per-call timeout for future Read and Write calls. It works in addition to any configured deadlines. A value <= 0 for d means Read and Write will not time out (unless a deadline is set).
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets the deadline for future Write calls. Even if Write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out (but see SetTimeout).
type Recv ¶
type Recv []byte
Send and Recv are script actions for sending and receiving raw bytes (usually literal strings).
type ScriptFunc ¶
type ScriptFunc func(s imap.MockServer) error
ScriptFunc is function type called during script execution to control the server state. STARTTLS, DEFLATE, and CLOSE are predefined script actions for the most common operations.
type Send ¶
type Send []byte
Send and Recv are script actions for sending and receiving raw bytes (usually literal strings).
type T ¶
T wraps existing test state and provides methods for testing the IMAP client against the scripted server.
func Server ¶
Server launches a new scripted server that can handle one client connection. The script should contain the initial server greeting. It should also use the STARTTLS action (or a custom ScriptFunc for negotiating encryption) prior to sending the greeting if the client is using DialTLS to connect.
func (*T) Dial ¶
Dial returns a new Client connected to the scripted server or an error if the connection could not be established.
func (*T) DialTLS ¶
DialTLS returns a new Client connected to the scripted server or an error if the connection could not be established. The server is expected to negotiate encryption before sending the initial greeting. Config should be nil when used in combination with the predefined STARTTLS script action.
func (*T) Join ¶
Join waits for script completion and reports any errors encountered by the client or the server.
func (*T) Script ¶
func (t *T) Script(script ...interface{})
Script runs a server script in a separate goroutine. A script is a sequence of string, Send, Recv, and ScriptFunc actions. Strings represent lines of text to be sent ("S: ...") or received ("C: ...") by the server. There is an implicit CRLF at the end of each line. Send and Recv allow the server to send and receive raw bytes (usually literal strings). ScriptFunc allows server state changes by calling methods on the provided imap.MockServer instance.