testtrx

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package testtrx can be used to test mailfilter based filter functions

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Modification

type Modification struct {
	Kind  ModificationKind
	Addr  string
	Args  string
	Index int
	Name  string
	Value string
	Body  []byte
}

Modification is a modification that a mailfilter.DecisionModificationFunc made to the Trx.

type ModificationKind

type ModificationKind int
const (
	ChangeFrom   ModificationKind = iota // a change mail from modification would have been sent to the MTA
	AddRcptTo                            // an add-recipient modification would have been sent to the MTA
	DelRcptTo                            // a delete-recipient modification would have been sent to the MTA
	InsertHeader                         // an insert-header modification would have been sent to the MTA
	ChangeHeader                         // a change-header modification would have been sent to the MTA
	ReplaceBody                          // a replace-body modification would have been sent to the MTA
)

type Trx

type Trx struct {
	// contains filtered or unexported fields
}

Trx implements mailfilter.Trx for unit tests. Use this struct when you want to test your decision functions. You can use the fluent Set* methods of this struct to build up the transaction you want to test. After you passed the Trx to your decision function, you can call Trx.Modifications to check that your decision function did what was expected of it.

Example
package main

import (
	"context"
	"fmt"

	"github.com/d--j/go-milter/mailfilter"
	"github.com/d--j/go-milter/mailfilter/addr"
	"github.com/d--j/go-milter/mailfilter/testtrx"
)

func Filter(_ context.Context, trx mailfilter.Trx) (mailfilter.Decision, error) {
	trx.ChangeMailFrom("", "A=B")
	return mailfilter.Accept, nil
}

func main() {
	trx := (&testtrx.Trx{}).SetMTA(mailfilter.MTA{
		Version: "Postfix 2.0.0",
		FQDN:    "mx.example.net",
		Daemon:  "smtpd",
	}).SetConnect(mailfilter.Connect{
		Host:   "localhost",
		Family: "tcp",
		Port:   25,
		Addr:   "127.0.0.1",
		IfName: "lo",
		IfAddr: "127.0.0.1",
	}).SetHelo(mailfilter.Helo{
		Name:        "localhost",
		TlsVersion:  "",
		Cipher:      "",
		CipherBits:  "",
		CertSubject: "",
		CertIssuer:  "",
	}).
		SetQueueId("ABCD").
		SetMailFrom(addr.NewMailFrom("root@localhost", "", "local", "", "")).
		SetRcptTosList("root@localhost", "postmaster@example.com").
		SetHeadersRaw([]byte("Subject: test\n\n")).
		SetBodyBytes([]byte("test body"))

	decision, _ := Filter(context.Background(), trx)
	if decision != mailfilter.Accept {
		fmt.Println("decision wrong")
	}
	for _, m := range trx.Modifications() {
		fmt.Println(m)
	}

}
Output:

{0  A=B 0   []}

func (*Trx) AddRcptTo

func (t *Trx) AddRcptTo(rcptTo string, esmtpArgs string)

func (*Trx) Body

func (t *Trx) Body() io.ReadSeeker

func (*Trx) ChangeMailFrom

func (t *Trx) ChangeMailFrom(from string, esmtpArgs string)

func (*Trx) Connect

func (t *Trx) Connect() *mailfilter.Connect

func (*Trx) DelRcptTo

func (t *Trx) DelRcptTo(rcptTo string)

func (*Trx) HasRcptTo

func (t *Trx) HasRcptTo(rcptTo string) bool

func (*Trx) Headers

func (t *Trx) Headers() header2.Header

func (*Trx) HeadersEnforceOrder added in v0.8.2

func (t *Trx) HeadersEnforceOrder()

func (*Trx) Helo

func (t *Trx) Helo() *mailfilter.Helo

func (*Trx) MTA

func (t *Trx) MTA() *mailfilter.MTA

func (*Trx) MailFrom

func (t *Trx) MailFrom() *addr.MailFrom

func (*Trx) Modifications

func (t *Trx) Modifications() []Modification

func (*Trx) QueueId

func (t *Trx) QueueId() string

func (*Trx) RcptTos

func (t *Trx) RcptTos() []*addr.RcptTo

func (*Trx) ReplaceBody

func (t *Trx) ReplaceBody(r io.Reader)

func (*Trx) SetBody

func (t *Trx) SetBody(body io.ReadSeeker) *Trx

func (*Trx) SetBodyBytes

func (t *Trx) SetBodyBytes(b []byte) *Trx

func (*Trx) SetConnect

func (t *Trx) SetConnect(connect mailfilter.Connect) *Trx

func (*Trx) SetHeaders

func (t *Trx) SetHeaders(headers header2.Header) *Trx

func (*Trx) SetHeadersRaw

func (t *Trx) SetHeadersRaw(raw []byte) *Trx

func (*Trx) SetHelo

func (t *Trx) SetHelo(helo mailfilter.Helo) *Trx

func (*Trx) SetMTA

func (t *Trx) SetMTA(mta mailfilter.MTA) *Trx

func (*Trx) SetMailFrom

func (t *Trx) SetMailFrom(mailFrom addr.MailFrom) *Trx

func (*Trx) SetQueueId

func (t *Trx) SetQueueId(value string) *Trx

func (*Trx) SetRcptTos

func (t *Trx) SetRcptTos(rcptTos []*addr.RcptTo) *Trx

func (*Trx) SetRcptTosList

func (t *Trx) SetRcptTosList(tos ...string) *Trx

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL