-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Hinderberger
committed
Jun 21, 2024
1 parent
14c21af
commit 21e67d0
Showing
9 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2575,3 +2575,6 @@ The expected response: | |
} | ||
} | ||
``` | ||
## SMTP Server | ||
TODO: Add section about SMTP Server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package smtp | ||
|
||
import "net/http" | ||
|
||
// RegisterRoutes sets up HTTP routes for inspecting the SMTP Server's | ||
// received messages. | ||
func (s *Server) RegisterRoutes(mux *http.ServeMux, prefix string) { | ||
// TODO | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package smtp | ||
|
||
import "time" | ||
|
||
// ReceivedMessage contains a single email message as received via SMTP. | ||
type ReceivedMessage struct { | ||
smtpFrom string | ||
smtpRcptTo []string | ||
rawMessageData []byte | ||
receivedAt time.Time | ||
} | ||
|
||
// TODO: Constructor that takes in from, rcptTo, rawMessageData, receivedAt and that also parses the message | ||
|
||
// TODO: Getters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package smtp | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"sync" | ||
|
||
"github.com/emersion/go-smtp" | ||
) | ||
|
||
// Server contains a basic SMTP server for testing purposes. | ||
// | ||
// It will accept incoming messages and save them to an internal list of | ||
// received messages, which can be queried using the appropriate methods | ||
// of Server. | ||
type Server struct { | ||
server smtp.Server | ||
receivedMessages []*ReceivedMessage | ||
|
||
mutex sync.RWMutex | ||
} | ||
|
||
type session struct { | ||
// TODO | ||
} | ||
|
||
func NewServer(addr string) *Server { | ||
server := &Server{ | ||
receivedMessages: make([]receivedMessage), | ||
} | ||
|
||
backend := panic("not implemented") // TODO | ||
|
||
server.server = smtp.NewServer(backend) | ||
server.Addr = addr | ||
// TODO: Enable SMTPUTF8? | ||
// TODO: Enable BINARYMIME? | ||
|
||
return server | ||
} | ||
|
||
// ListenAndServe runs the SMTP server. It will not return until the server is | ||
// shut down or otherwise aborts. | ||
func (s *Server) ListenAndServe() error { | ||
return s.server.ListenAndServe() | ||
} | ||
|
||
// Shutdown shuts down the SMTP server that was previously started using | ||
// ListenAndServe. | ||
func (s *Server) Shutdown() error { | ||
return s.server.Shutdown() | ||
} | ||
|
||
// ReceivedMessage returns a message that the server has retrieved | ||
// by its index in the list of received messages. | ||
func (s *Server) ReceivedMessage(idx int) (*ReceivedMessage, error) { | ||
s.mutex.RLock() | ||
defer s.mutex.RUnlock() | ||
|
||
msg, ok := s.receivedMessages[idx] | ||
if !ok { | ||
return nil, fmt.Errorf( | ||
"Server does not contain message with index %d", idx, | ||
) | ||
} | ||
|
||
return msg, nil | ||
} | ||
|
||
// ReceivedMessages returns the list of all messages that the server has | ||
// retrieved. | ||
func (s *Server) ReceivedMessages() []ReceivedMessage { | ||
s.mutex.RLock() | ||
defer s.mutex.RUnlock() | ||
|
||
// We copy the slice to avoid race conditions when the receivedMessages slice is updated. | ||
// It's just a slice of pointers, so it should be relatively lightweight. | ||
view := make([]*ReceivedMessage, len(s.receivedMessage)) | ||
copy(view, s.receivedMessages) | ||
|
||
return view | ||
} | ||
|
||
// SearchByHeader returns the list of all received messages that have at | ||
// least one header matching the given regular expression. | ||
func (s *Server) SearchByHeader(re *regexp.Regexp) []ReceivedMessage { | ||
s.mutex.RLock() | ||
defer s.mutex.RUnlock() | ||
|
||
// TODO | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
esmtp "github.com/emersion/go-smtp" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/programmfabrik/apitest/internal/smtp" | ||
) | ||
|
||
// StartSmtpServer starts the testing SMTP server, if configured. | ||
func (ats *Suite) StartSmtpServer() { | ||
if ats.SmtpServer == nil || ats.smtpServer != nil { | ||
return | ||
} | ||
|
||
ats.smtpServer = smtp.NewServer(ats.SmtpServer.Addr) | ||
|
||
go func() { | ||
if !ats.Config.LogShort { | ||
logrus.Infof("Starting SMTP Server: %s", ats.SmtpServer.Addr) | ||
} | ||
|
||
err := ats.smtpServer.ListenAndServe() | ||
if !errors.Is(err, esmtp.ErrServerClosed) { | ||
// Error starting or closing listener: | ||
logrus.Fatal("SMTP server ListenAndServe:", err) | ||
} | ||
}() | ||
} | ||
|
||
// StopSmtpServer stops the SMTP server that was started using StartSMTPServer. | ||
func (ats *Suite) StopSmtpServer() { | ||
if ats.SmtpServer == nil || ats.smtpServer == nil { | ||
return | ||
} | ||
|
||
err := ats.smtpServer.Shutdown() | ||
if err != nil { | ||
// logrus.Error is used instead of Fatal, because an error | ||
// during closing of a server shouldn't affect the outcome of | ||
// the test. | ||
logrus.Error("SMTP Server shutdown:", err) | ||
} else if !ats.Config.LogShort { | ||
logrus.Info("SMTP Server stopped") | ||
} | ||
} |