forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_test.go
262 lines (214 loc) · 5.19 KB
/
write_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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package imap
import (
"bytes"
"testing"
"time"
)
func newWriter() (w *Writer, b *bytes.Buffer) {
b = &bytes.Buffer{}
w = NewWriter(b)
return
}
func TestWriter_WriteCrlf(t *testing.T) {
w, b := newWriter()
if err := w.writeCrlf(); err != nil {
t.Error(err)
}
if b.String() != "\r\n" {
t.Error("Not a CRLF")
}
}
func TestWriter_WriteField_Nil(t *testing.T) {
w, b := newWriter()
if err := w.writeField(nil); err != nil {
t.Error(err)
}
if b.String() != "NIL" {
t.Error("Not NIL")
}
}
func TestWriter_WriteField_Number(t *testing.T) {
w, b := newWriter()
if err := w.writeField(uint32(42)); err != nil {
t.Error(err)
}
if b.String() != "42" {
t.Error("Not the expected number")
}
}
func TestWriter_WriteField_Atom(t *testing.T) {
w, b := newWriter()
if err := w.writeField(Atom("BODY[]")); err != nil {
t.Error(err)
}
if b.String() != "BODY[]" {
t.Error("Not the expected atom")
}
}
func TestWriter_WriteString_Quoted(t *testing.T) {
w, b := newWriter()
if err := w.writeField("I love potatoes!"); err != nil {
t.Error(err)
}
if b.String() != "\"I love potatoes!\"" {
t.Error("Not the expected quoted string")
}
}
func TestWriter_WriteString_Quoted_WithSpecials(t *testing.T) {
w, b := newWriter()
if err := w.writeField("I love \"1984\"!"); err != nil {
t.Error(err)
}
if b.String() != "\"I love \\\"1984\\\"!\"" {
t.Error("Not the expected quoted string")
}
}
func TestWriter_WriteField_ForcedQuoted(t *testing.T) {
w, b := newWriter()
if err := w.writeField(Quoted("dille")); err != nil {
t.Error(err)
}
if b.String() != "\"dille\"" {
t.Error("Not the expected atom:", b.String())
}
}
func TestWriter_WriteField_8bitString(t *testing.T) {
w, b := newWriter()
if err := w.writeField("☺"); err != nil {
t.Error(err)
}
if b.String() != "{3}\r\n☺" {
t.Error("Not the expected atom")
}
}
func TestWriter_WriteField_NilString(t *testing.T) {
w, b := newWriter()
if err := w.writeField("NIL"); err != nil {
t.Error(err)
}
if b.String() != "\"NIL\"" {
t.Error("Not the expected quoted string")
}
}
func TestWriter_WriteField_EmptyString(t *testing.T) {
w, b := newWriter()
if err := w.writeField(""); err != nil {
t.Error(err)
}
if b.String() != "\"\"" {
t.Error("Not the expected quoted string")
}
}
func TestWriter_WriteField_DateTime(t *testing.T) {
w, b := newWriter()
dt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
if err := w.writeField(dt); err != nil {
t.Error(err)
}
if b.String() != `"10-Nov-2009 23:00:00 +0000"` {
t.Error("Invalid date:", b.String())
}
}
func TestWriter_WriteField_ZeroDateTime(t *testing.T) {
w, b := newWriter()
dt := time.Time{}
if err := w.writeField(dt); err != nil {
t.Error(err)
}
if b.String() != "NIL" {
t.Error("Invalid nil date:", b.String())
}
}
func TestWriter_WriteFields(t *testing.T) {
w, b := newWriter()
if err := w.writeFields([]interface{}{"hey", 42}); err != nil {
t.Error(err)
}
if b.String() != "hey 42" {
t.Error("Not the expected fields")
}
}
func TestWriter_WriteField_SimpleList(t *testing.T) {
w, b := newWriter()
if err := w.writeField([]interface{}{"hey", 42}); err != nil {
t.Error(err)
}
if b.String() != "(hey 42)" {
t.Error("Not the expected list")
}
}
func TestWriter_WriteField_NestedList(t *testing.T) {
w, b := newWriter()
list := []interface{}{
"toplevel",
[]interface{}{
"nested",
0,
},
22,
}
if err := w.writeField(list); err != nil {
t.Error(err)
}
if b.String() != "(toplevel (nested 0) 22)" {
t.Error("Not the expected list")
}
}
func TestWriter_WriteField_Literal(t *testing.T) {
w, b := newWriter()
literal := bytes.NewBufferString("hello world")
if err := w.writeField(literal); err != nil {
t.Error(err)
}
if b.String() != "{11}\r\nhello world" {
t.Error("Not the expected literal")
}
}
func TestWriter_WriteField_SeqSet(t *testing.T) {
w, b := newWriter()
seqSet, _ := ParseSeqSet("3:4,6,42:*")
if err := w.writeField(seqSet); err != nil {
t.Error(err)
}
if s := b.String(); s != "3:4,6,42:*" {
t.Error("Not the expected sequence set", s)
}
}
func TestWriter_WriteField_BodySectionName(t *testing.T) {
w, b := newWriter()
name, _ := ParseBodySectionName("BODY.PEEK[HEADER.FIELDS (date subject from to cc)]")
if err := w.writeField(name.resp()); err != nil {
t.Error(err)
}
if s := b.String(); s != "BODY[HEADER.FIELDS (date subject from to cc)]" {
t.Error("Not the expected body section name", s)
}
}
func TestWriter_WriteRespCode_NoArgs(t *testing.T) {
w, b := newWriter()
if err := w.writeRespCode("READ-ONLY", nil); err != nil {
t.Error(err)
}
if b.String() != "[READ-ONLY]" {
t.Error("Not the expected response code")
}
}
func TestWriter_WriteRespCode_WithArgs(t *testing.T) {
w, b := newWriter()
args := []interface{}{"IMAP4rev1", "STARTTLS", "LOGINDISABLED"}
if err := w.writeRespCode("CAPABILITY", args); err != nil {
t.Error(err)
}
if b.String() != "[CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED]" {
t.Error("Not the expected response code")
}
}
func TestWriter_WriteLine(t *testing.T) {
w, b := newWriter()
if err := w.writeLine(Atom("*"), "OK"); err != nil {
t.Error(err)
}
if b.String() != "* OK\r\n" {
t.Error("Not the expected line:", b.String())
}
}