Documentation
¶
Overview ¶
Package testmail provides in-memory IMAP and SMTP servers for tests.
Index ¶
- Constants
- type DKIMFixture
- type GmailMessage
- type IMAP
- func (s *IMAP) AddGmail(raw string, flags []imap.Flag, folders ...string) *GmailMessage
- func (s *IMAP) BlockLogin() (entered <-chan struct{}, release func())
- func (s *IMAP) BlockLoginAfter(n int) (entered <-chan struct{}, release func())
- func (s *IMAP) BytesSent() int64
- func (s *IMAP) DropAfterGmailLabelStore(n int)
- func (s *IMAP) DropAfterNextAppend()
- func (s *IMAP) DropFetchAfter(n int)
- func (s *IMAP) DropNextFetch()
- func (s *IMAP) DropOnNextUIDExpunge()
- func (s *IMAP) GmailLabelStores() int64
- func (s *IMAP) IDs() []map[string]string
- func (s *IMAP) Logins() int64
- func (s *IMAP) MovesAndCopies() int64
- func (s *IMAP) RejectNextGmailSearch()
- func (s *IMAP) RejectNextGmailSelect()
- func (s *IMAP) Restart()
- func (s *IMAP) SetPassword(pw string)
- func (s *IMAP) Stop()
- type Inbox
- func (in *Inbox) AuthAttempts() int
- func (in *Inbox) BlockData() (entered <-chan struct{}, release func())
- func (in *Inbox) DisableAuth(off bool)
- func (in *Inbox) FailAfterData(n int)
- func (in *Inbox) Messages() []Received
- func (in *Inbox) RejectAuth(reject bool)
- func (in *Inbox) Reply(to string, at SMTPStage, resp *smtp.SMTPError, once bool)
- func (in *Inbox) SetPassword(pw string)
- type Option
- type Received
- type SMTPOption
- type SMTPStage
Constants ¶
const ( GmailAllMail = "[Gmail]/All Mail" GmailSent = "[Gmail]/Sent Mail" GmailDrafts = "[Gmail]/Drafts" GmailTrash = "[Gmail]/Trash" GmailSpam = "[Gmail]/Spam" )
Gmail's folders, which a Gmail server starts with besides INBOX.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DKIMFixture ¶ added in v1.21.0
DKIMFixture signs real message bytes and serves only deterministic public DNS test records. No real mailbox, key or external DNS service is used.
func NewDKIMFixture ¶ added in v1.21.0
func NewDKIMFixture(t testing.TB) *DKIMFixture
func (*DKIMFixture) Sign ¶ added in v1.21.0
func (f *DKIMFixture) Sign(t testing.TB, from, domain string, options ...func(*dkim.SignOptions)) []byte
type GmailMessage ¶ added in v1.30.0
type GmailMessage struct {
ID uint64 // X-GM-MSGID
// contains filtered or unexported fields
}
GmailMessage is one Gmail message, in the folders it has a UID in.
func (*GmailMessage) Folders ¶ added in v1.30.0
func (m *GmailMessage) Folders() []string
Folders lists the folders the message is in, sorted.
func (*GmailMessage) Label ¶ added in v1.30.0
func (m *GmailMessage) Label(folder string)
Label adds the message to folder, as another client does: a new UID there, with the message's flags.
func (*GmailMessage) UID ¶ added in v1.30.0
func (m *GmailMessage) UID(folder string) imap.UID
UID is the message's UID in folder, 0 when it is not there.
func (*GmailMessage) Unlabel ¶ added in v1.30.0
func (m *GmailMessage) Unlabel(folder string)
Unlabel takes the message out of folder, as archiving or removing a label does in another client.
type IMAP ¶
type IMAP struct {
Addr string
User *imapmemserver.User
// contains filtered or unexported fields
}
IMAP is a running in-memory IMAP server with one user, alice/secret.
func StartIMAP ¶
StartIMAP starts a server on 127.0.0.1:0 with INBOX, Sent, Trash and Archive (with Gmail, INBOX and the Gmail folders).
func (*IMAP) AddGmail ¶ added in v1.30.0
AddGmail adds a new Gmail message with flags to folders, which must exist.
func (*IMAP) BlockLogin ¶
func (s *IMAP) BlockLogin() (entered <-chan struct{}, release func())
BlockLogin is BlockLoginAfter(0).
func (*IMAP) BlockLoginAfter ¶
BlockLoginAfter lets n logins (LOGIN or XOAUTH2) through and holds the next one until release is called (release may be called more than once); the held login checks its password or token only then. entered fires once it is held.
func (*IMAP) DropAfterGmailLabelStore ¶ added in v1.30.0
DropAfterGmailLabelStore drops the response after the nth next label STORE.
func (*IMAP) DropAfterNextAppend ¶
func (s *IMAP) DropAfterNextAppend()
DropAfterNextAppend makes the next APPEND store the message and then close the client's connection before the tagged OK.
func (*IMAP) DropFetchAfter ¶
DropFetchAfter lets n FETCH commands complete and drops the connection during the one after, like DropNextFetch.
func (*IMAP) DropNextFetch ¶
func (s *IMAP) DropNextFetch()
DropNextFetch makes the next FETCH write at most one write of response and then close the client's connection.
func (*IMAP) DropOnNextUIDExpunge ¶
func (s *IMAP) DropOnNextUIDExpunge()
DropOnNextUIDExpunge makes the next UID EXPUNGE close the client's connection without expunging anything.
func (*IMAP) GmailLabelStores ¶ added in v1.30.0
GmailLabelStores is the number of label STORE commands handled.
func (*IMAP) MovesAndCopies ¶
MovesAndCopies is how many MOVE and COPY commands the server has handled.
func (*IMAP) RejectNextGmailSearch ¶ added in v1.30.0
func (s *IMAP) RejectNextGmailSearch()
RejectNextGmailSearch makes one identity check return NO without a result.
func (*IMAP) RejectNextGmailSelect ¶ added in v1.30.0
func (s *IMAP) RejectNextGmailSelect()
RejectNextGmailSelect makes one membership check return NO for an existing folder.
func (*IMAP) Restart ¶
func (s *IMAP) Restart()
Restart listens again on the same address with the same data.
func (*IMAP) SetPassword ¶
SetPassword changes alice's password, as a user does on the provider's website.
type Inbox ¶
type Inbox struct {
// contains filtered or unexported fields
}
Inbox records what the test SMTP server received.
func StartSMTP ¶
func StartSMTP(t *testing.T, opts ...SMTPOption) (addr string, in *Inbox)
StartSMTP starts a plaintext SMTP server accepting only alice/secret (PLAIN).
func (*Inbox) AuthAttempts ¶
AuthAttempts is how many AUTH exchanges the server has handled.
func (*Inbox) BlockData ¶
func (in *Inbox) BlockData() (entered <-chan struct{}, release func())
BlockData makes the next sessions hold their reply after reading the full DATA until release is called (release may be called more than once). entered receives once per held session.
func (*Inbox) DisableAuth ¶ added in v1.32.0
DisableAuth makes AUTH answer what Microsoft does for a mailbox whose SMTP AUTH is disabled (535 5.7.139), whatever the credentials.
func (*Inbox) FailAfterData ¶
FailAfterData makes the next n sessions read the full DATA and then drop the connection without replying. The server has the message, so it is recorded.
func (*Inbox) RejectAuth ¶
RejectAuth makes the server refuse (or, with false, accept again) alice/secret.
func (*Inbox) Reply ¶ added in v1.14.0
Reply makes the server answer resp for the recipient to at stage: at its RCPT TO, or once the full DATA of a message to it is read (the message is then not recorded). With once it answers only the next time, then accepts again; otherwise every time until Reply is called again. A nil resp clears it.
func (*Inbox) SetPassword ¶
SetPassword changes alice's password.
type Option ¶
type Option func(*config)
Option customises StartIMAP and StartSMTP; each option says which servers it affects.
func Gmail ¶ added in v1.30.0
func Gmail() Option
Gmail makes the IMAP server behave as Gmail: it advertises X-GM-EXT-1, starts with INBOX and the Gmail folders (special-use in LIST), and keeps messages added with AddGmail as Gmail does. One such message is in several folders, each with its own UID; flags other than \Deleted are shared; FETCH answers X-GM-MSGID and X-GM-LABELS; COPY and MOVE add the destination to its folders (with the UID it already has there, if any), and MOVE and EXPUNGE take it out of the source; a MOVE to Trash or Spam takes it out of every other folder. Identity is never derived from content: two AddGmail calls make two messages, and an APPEND makes a new one.
func RequireID ¶
func RequireID() Option
RequireID makes the IMAP server refuse SELECT with "NO Unsafe Login" until the client has sent ID, as NetEase does.
func WithXOAuth2 ¶
WithXOAuth2 makes the IMAP and SMTP servers offer XOAUTH2 and accept token for alice.
func WithXOAuth2Func ¶
WithXOAuth2Func is WithXOAuth2 with a validator, such as a testoauth server's ValidAccess. The XOAUTH2 user may be alice or an alice@ address.
func WithoutCOPYUID ¶
func WithoutCOPYUID() Option
WithoutCOPYUID keeps UIDPLUS advertised but suppresses COPYUID response codes.
func WithoutIdle ¶
func WithoutIdle() Option
WithoutIdle removes IDLE from the advertised capabilities (the server adds it on its own, so the connection strips it from CAPABILITY lines).
func WithoutMove ¶
func WithoutMove() Option
WithoutMove removes MOVE from the advertised capabilities.
func WithoutUIDPlus ¶
func WithoutUIDPlus() Option
WithoutUIDPlus removes UIDPLUS and suppresses COPYUID/APPENDUID response codes (the memory backend always returns them).
type SMTPOption ¶
type SMTPOption = Option
SMTPOption customises StartSMTP; WithXOAuth2 applies to both servers.
func LoginOnly ¶ added in v1.24.1
func LoginOnly() SMTPOption
LoginOnly makes the SMTP server offer AUTH LOGIN and not PLAIN, as Exchange does.
func SaveToSent ¶
func SaveToSent(im *IMAP) SMTPOption
SaveToSent makes the SMTP server file every accepted message into im's Sent folder, as providers that auto-save sent mail do.