-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
142 lines (119 loc) · 2.66 KB
/
main_test.go
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
package postbox
import (
"io"
"strings"
"testing"
"time"
)
// TestWritingHeaders test if the correct headers are written to the given io.Writer
func TestWritingHeaders(t *testing.T) {
headers := []string{
"From: [email protected]",
"To: [email protected]",
"Reply-To: [email protected]",
"Mime-Version: 1.0",
"Date: Tue, 10 Nov 2009 23:00:00 +0100",
"Cc: [email protected]; [email protected]",
"Subject: hello world",
}
loc, _ := time.LoadLocation("Europe/Amsterdam")
envelope := Envelope{
Date: time.Date(2009, 11, 10, 23, 0, 0, 0, loc),
From: "[email protected]",
Sender: "[email protected]",
ReplyTo: "[email protected]",
To: []string{"[email protected]"},
Cc: []string{"[email protected]", "[email protected]"},
Subject: "hello world",
Charset: "UTF-8",
}
reader, writer := io.Pipe()
go envelope.Write(writer)
last := ""
line := ""
buffer := make([]byte, 1)
reader:
for {
n, err := reader.Read(buffer)
if err != nil && err != io.EOF {
t.Fatal(err)
}
str := string(buffer[:n])
if str == LF && last == CR {
for index, header := range headers {
if header == line[:len(line)-1] {
line = ""
headers = append(headers[:index], headers[index+1:]...)
continue reader
}
}
if len(headers) != 0 {
t.Fatal("Unexpected header:", line)
}
}
line += str
if err == io.EOF {
break
}
last = str
}
}
// TestWritingContent test if the correct headers are written to the given io.Writer
func TestWritingContent(t *testing.T) {
plain := "hello world"
html := "<p>hello <b>world</b></p>"
expected := []string{
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: UTF-8",
plain,
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: UTF-8",
html,
}
envelope := Envelope{
Charset: "UTF-8",
Parts: []*Part{
{
ContentType: "text/plain",
Encoding: "UTF-8",
Reader: strings.NewReader(plain),
},
{
ContentType: "text/html",
Encoding: "UTF-8",
Reader: strings.NewReader(html),
},
},
}
reader, writer := io.Pipe()
go envelope.Write(writer)
last := ""
line := ""
buffer := make([]byte, 1)
for {
n, err := reader.Read(buffer)
if err != nil && err != io.EOF {
t.Fatal(err)
}
str := string(buffer[:n])
if str == LF && last == CR {
compare := line[:len(line)-1]
for index, value := range expected {
if value == compare {
expected = append(expected[:index], expected[index+1:]...)
break
}
}
line = ""
} else {
line += str
}
if err == io.EOF {
break
}
last = str
}
if len(expected) != 0 {
t.Fatal("Not all expectations were met:", expected)
}
}