1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
package mail_test
import (
"io"
"io/ioutil"
"log"
"strings"
"testing"
"github.com/emersion/go-message/mail"
)
func ExampleReader() {
// Let's assume r is an io.Reader that contains a mail.
var r io.Reader
// Create a new mail reader
mr, err := mail.CreateReader(r)
if err != nil {
log.Fatal(err)
}
// Read each mail's part
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
switch h := p.Header.(type) {
case *mail.InlineHeader:
b, _ := ioutil.ReadAll(p.Body)
log.Printf("Got text: %v\n", string(b))
case *mail.AttachmentHeader:
filename, _ := h.Filename()
log.Printf("Got attachment: %v\n", filename)
}
}
}
func testReader(t *testing.T, r io.Reader) {
mr, err := mail.CreateReader(r)
if err != nil {
t.Fatalf("mail.CreateReader(r) = %v", err)
}
defer mr.Close()
wantSubject := "Your Name"
subject, err := mr.Header.Subject()
if err != nil {
t.Errorf("mr.Header.Subject() = %v", err)
} else if subject != wantSubject {
t.Errorf("mr.Header.Subject() = %v, want %v", subject, wantSubject)
}
i := 0
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
var expectedBody string
switch i {
case 0:
h, ok := p.Header.(*mail.InlineHeader)
if !ok {
t.Fatalf("Expected a InlineHeader, but got a %T", p.Header)
}
if mediaType, _, _ := h.ContentType(); mediaType != "text/plain" {
t.Errorf("Expected a plaintext part, not an HTML part")
}
expectedBody = "Who are you?"
case 1:
h, ok := p.Header.(*mail.AttachmentHeader)
if !ok {
t.Fatalf("Expected an AttachmentHeader, but got a %T", p.Header)
}
if filename, err := h.Filename(); err != nil {
t.Error("Expected no error while parsing filename, but got:", err)
} else if filename != "note.txt" {
t.Errorf("Expected filename to be %q but got %q", "note.txt", filename)
}
expectedBody = "I'm Mitsuha."
}
if b, err := ioutil.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, but got:", err)
} else if string(b) != expectedBody {
t.Errorf("Expected part body to be:\n%v\nbut got:\n%v", expectedBody, string(b))
}
i++
}
if i != 2 {
t.Errorf("Expected exactly two parts but got %v", i)
}
}
func TestReader(t *testing.T) {
testReader(t, strings.NewReader(mailString))
}
func TestReader_nonMultipart(t *testing.T) {
s := "Subject: Your Name\r\n" +
"\r\n" +
"Who are you?"
mr, err := mail.CreateReader(strings.NewReader(s))
if err != nil {
t.Fatal("Expected no error while creating reader, got:", err)
}
defer mr.Close()
p, err := mr.NextPart()
if err != nil {
t.Fatal("Expected no error while reading part, got:", err)
}
if _, ok := p.Header.(*mail.InlineHeader); !ok {
t.Fatalf("Expected a InlineHeader, but got a %T", p.Header)
}
expectedBody := "Who are you?"
if b, err := ioutil.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, but got:", err)
} else if string(b) != expectedBody {
t.Errorf("Expected part body to be:\n%v\nbut got:\n%v", expectedBody, string(b))
}
if _, err := mr.NextPart(); err != io.EOF {
t.Fatal("Expected io.EOF while reading part, but got:", err)
}
}
func TestReader_closeImmediately(t *testing.T) {
s := "Content-Type: text/plain\r\n" +
"\r\n" +
"Who are you?"
mr, err := mail.CreateReader(strings.NewReader(s))
if err != nil {
t.Fatal("Expected no error while creating reader, got:", err)
}
mr.Close()
if _, err := mr.NextPart(); err != io.EOF {
t.Fatal("Expected io.EOF while reading part, but got:", err)
}
}
func TestReader_nested(t *testing.T) {
r := strings.NewReader(nestedMailString)
mr, err := mail.CreateReader(r)
if err != nil {
t.Fatalf("mail.CreateReader(r) = %v", err)
}
defer mr.Close()
i := 0
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
switch i {
case 0:
_, ok := p.Header.(*mail.InlineHeader)
if !ok {
t.Fatalf("Expected a InlineHeader, but got a %T", p.Header)
}
expectedBody := "I forgot."
if b, err := ioutil.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, but got:", err)
} else if string(b) != expectedBody {
t.Errorf("Expected part body to be:\n%v\nbut got:\n%v", expectedBody, string(b))
}
case 1:
_, ok := p.Header.(*mail.AttachmentHeader)
if !ok {
t.Fatalf("Expected an AttachmentHeader, but got a %T", p.Header)
}
testReader(t, p.Body)
}
i++
}
}
|