mock

package
Version: v0.0.0-...-a6241b9 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2016 License: BSD-3-Clause Imports: 16 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

View Source
var ServerName = "imap.mock.net"

ServerName is the hostname used by the scripted server.

View Source
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

func NewConn(addrA, addrB string, bufSize int) (A *Conn, B *Conn)

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

func (c *Conn) Close() error

Close closes the connection. Any blocked Read or Write operations will be unblocked and return errors.

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

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

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline sets the Read and Write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

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

func (c *Conn) SetTimeout(d time.Duration) error

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

func (c *Conn) SetWriteDeadline(t time.Time) error

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).

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

Write writes data to the connection. It can be made to time out and return a net.Error with Timeout() == true after a deadline or a per-Write timeout; see SetDeadline, SetWriteDeadline, and 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

type T struct {
	*testing.T
	// contains filtered or unexported fields
}

T wraps existing test state and provides methods for testing the IMAP client against the scripted server.

func Server

func Server(t *testing.T, script ...interface{}) *T

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

func (t *T) Dial() (*imap.Client, error)

Dial returns a new Client connected to the scripted server or an error if the connection could not be established.

func (*T) DialTLS

func (t *T) DialTLS(config *tls.Config) (*imap.Client, error)

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

func (t *T) Join(err error)

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.

func (*T) StartTLS

func (t *T) StartTLS(config *tls.Config) error

StartTLS performs client-side TLS negotiation. Config should be nil when used in combination with the predefined STARTTLS script action.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL