-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools/smtpmock): mock smtp server with a REST API (#4774)
- Loading branch information
Showing
11 changed files
with
239 additions
and
4 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
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,7 @@ | ||
module github.com/ovh/cds/tools/smtpmock/cmd/smtpmockcli | ||
|
||
go 1.12 | ||
|
||
require github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9 | ||
|
||
replace github.com/ovh/cds/tools/smtpmock => ../../ |
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,2 @@ | ||
github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9 h1:mrsrfaVJ42WA/bOYc+TB2/EbSB0W6vEkIrCDXnm/60I= | ||
github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9/go.mod h1:BzEGArrdGbhhTMh5+83jlF5fb/QHfWh0tmBn2iXTouw= |
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,24 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/fsamin/smtp" | ||
|
||
"github.com/ovh/cds/tools/smtpmock" | ||
) | ||
|
||
func main() { | ||
log.Fatal(smtpmock.StartServer(context.Background(), ":2023", | ||
smtpmock.Handle("*@*", func(envelope *smtp.Envelope) error { | ||
fmt.Println("Message Received", envelope.MessageTo) | ||
fmt.Println("From:", envelope.MessageFrom, envelope.RemoteAddr) | ||
fmt.Println("To:", envelope.MessageTo) | ||
io.Copy(os.Stdout, envelope.MessageData) | ||
return nil | ||
}))) | ||
} |
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,12 @@ | ||
module github.com/ovh/cds/tools/smtpmock/cmd/smtpmocksrv | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/labstack/echo v3.3.10+incompatible // indirect | ||
github.com/labstack/gommon v0.3.0 // indirect | ||
github.com/ovh/cds/tools/smtpmock v0.0.0-00010101000000-000000000000 // indirect | ||
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect | ||
) | ||
|
||
replace github.com/ovh/cds/tools/smtpmock => ../../ |
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,33 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9 h1:mrsrfaVJ42WA/bOYc+TB2/EbSB0W6vEkIrCDXnm/60I= | ||
github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9/go.mod h1:BzEGArrdGbhhTMh5+83jlF5fb/QHfWh0tmBn2iXTouw= | ||
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= | ||
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= | ||
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= | ||
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= | ||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= | ||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= | ||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= | ||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= | ||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= | ||
github.com/ovh/cds v0.0.0-20190903085051-3186373a910c h1:48bqV5nDe5o3nzVjGzLy7xOlLfSnt9i9H4LbMjIjHSU= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8= | ||
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 h1:Gv7RPwsi3eZ2Fgewe3CBsuOebPwO27PoXzRpJPsvSSM= | ||
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
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,104 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/fsamin/smtp" | ||
"github.com/ovh/cds/tools/smtpmock" | ||
|
||
"github.com/labstack/echo" | ||
) | ||
|
||
type Message struct { | ||
FromAgent string | ||
RemoteAddress string | ||
User string | ||
From string | ||
To string | ||
Content string | ||
} | ||
|
||
var ( | ||
flagSMTPAddress string | ||
flagHTTPAddress string | ||
allMessages = make(map[string][]Message) | ||
allMessagesMutex sync.Mutex | ||
messagesCounter int | ||
) | ||
|
||
func init() { | ||
flag.StringVar(&flagSMTPAddress, "smtp-address", ":2023", "SMTP Server address") | ||
flag.StringVar(&flagHTTPAddress, "http-address", ":2024", "HTTP Server address") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
go func() { | ||
if err := smtpmock.StartServer( | ||
context.Background(), | ||
flagSMTPAddress, | ||
smtpmock.Handle("*@*", smtpHandler), | ||
); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
e := echo.New() | ||
e.GET("/", func(c echo.Context) error { | ||
var s = fmt.Sprintf("SMTP Server listenning on %s\n", flagSMTPAddress) | ||
s += fmt.Sprintf("%d mails received to %d recipents\n", messagesCounter, len(allMessages)) | ||
|
||
return c.String(http.StatusOK, s) | ||
}) | ||
e.GET("/messages", func(c echo.Context) error { | ||
return c.JSON(http.StatusOK, allMessages) | ||
}) | ||
e.GET("/messages/:recipent", func(c echo.Context) error { | ||
return c.JSON(http.StatusOK, allMessages[c.Param("recipent")]) | ||
}) | ||
e.GET("/messages/:recipent/latest", func(c echo.Context) error { | ||
messages := allMessages[c.Param("recipent")] | ||
if len(messages) == 0 { | ||
return c.JSON(http.StatusNotFound, "not found") | ||
} | ||
|
||
return c.JSON(http.StatusOK, messages[0]) | ||
}) | ||
|
||
e.Logger.Fatal(e.Start(flagHTTPAddress)) | ||
|
||
} | ||
|
||
func smtpHandler(envelope *smtp.Envelope) error { | ||
allMessagesMutex.Lock() | ||
defer allMessagesMutex.Unlock() | ||
|
||
list := allMessages[envelope.MessageTo] | ||
|
||
m := Message{ | ||
RemoteAddress: envelope.RemoteAddr, | ||
FromAgent: envelope.FromAgent, | ||
To: envelope.MessageTo, | ||
From: envelope.MessageFrom, | ||
User: envelope.User, | ||
} | ||
|
||
btes, err := ioutil.ReadAll(envelope.MessageData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m.Content = string(btes) | ||
|
||
list = append([]Message{m}, list...) | ||
allMessages[envelope.MessageTo] = list | ||
messagesCounter++ | ||
return nil | ||
} |
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,5 @@ | ||
module github.com/ovh/cds/tools/smtpmock | ||
|
||
go 1.12 | ||
|
||
require github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9 |
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,2 @@ | ||
github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9 h1:mrsrfaVJ42WA/bOYc+TB2/EbSB0W6vEkIrCDXnm/60I= | ||
github.com/fsamin/smtp v0.0.0-20190904085838-54a7c428d4f9/go.mod h1:BzEGArrdGbhhTMh5+83jlF5fb/QHfWh0tmBn2iXTouw= |
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,24 @@ | ||
package smtpmock | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fsamin/smtp" | ||
) | ||
|
||
type Handler struct { | ||
pattern string | ||
handler smtp.HandlerFunc | ||
} | ||
|
||
func Handle(pattern string, handler smtp.HandlerFunc) Handler { | ||
return Handler{pattern, handler} | ||
} | ||
|
||
func StartServer(ctx context.Context, address string, handlers ...Handler) error { | ||
srv := smtp.NewServeMux() | ||
for i := range handlers { | ||
srv.HandleFunc(handlers[i].pattern, handlers[i].handler) | ||
} | ||
return smtp.ListenAndServeWithContext(ctx, address, srv) | ||
} |