mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package mailer
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/mail"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAddressesToStrings(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
scenarios := []struct {
|
|
withName bool
|
|
addresses []mail.Address
|
|
expected []string
|
|
}{
|
|
{
|
|
true,
|
|
[]mail.Address{{Name: "John Doe", Address: "[email protected]"}, {Name: "Jane Doe", Address: "[email protected]"}},
|
|
[]string{`"John Doe" <[email protected]>`, `"Jane Doe" <[email protected]>`},
|
|
},
|
|
{
|
|
true,
|
|
[]mail.Address{{Name: "John Doe", Address: "[email protected]"}, {Address: "[email protected]"}},
|
|
[]string{`"John Doe" <[email protected]>`, `[email protected]`},
|
|
},
|
|
{
|
|
false,
|
|
[]mail.Address{{Name: "John Doe", Address: "[email protected]"}, {Name: "Jane Doe", Address: "[email protected]"}},
|
|
[]string{`[email protected]`, `[email protected]`},
|
|
},
|
|
}
|
|
|
|
for _, s := range scenarios {
|
|
t.Run(fmt.Sprintf("%v_%v", s.withName, s.addresses), func(t *testing.T) {
|
|
result := addressesToStrings(s.addresses, s.withName)
|
|
|
|
if len(s.expected) != len(result) {
|
|
t.Fatalf("Expected\n%v\ngot\n%v", s.expected, result)
|
|
}
|
|
|
|
for k, v := range s.expected {
|
|
if v != result[k] {
|
|
t.Fatalf("Expected %d address %q, got %q", k, v, result[k])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDetectReaderMimeType(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
str := "#!/bin/node\n" + strings.Repeat("a", 10000) // ensure that it is large enough to remain after the signature sniffing
|
|
|
|
r, mime, err := detectReaderMimeType(strings.NewReader(str))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedMime := "text/javascript"
|
|
if mime != expectedMime {
|
|
t.Fatalf("Expected mime %q, got %q", expectedMime, mime)
|
|
}
|
|
|
|
raw, err := io.ReadAll(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rawStr := string(raw)
|
|
|
|
if rawStr != str {
|
|
t.Fatalf("Expected content\n%s\ngot\n%s", str, rawStr)
|
|
}
|
|
}
|