aboutsummaryrefslogtreecommitdiff
path: root/pkg/rfc822/writer_test.go
blob: 34c9d4a254a01b9b937ab55ceb67159ae434af7f (plain)
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
package rfc822

import (
	"bytes"
	"io"
	"testing"
)

func TestRfc822Writer_Write(t *testing.T) {
	tests := []struct {
		before string
		after  string
	}{
		{"", ""},
		{"foo", "foo"},
		{"foo\r", "foo\r\n"},
		{"foo\n", "foo\r\n"},
		{"foo\r\n", "foo\r\n"},
		{"\r", "\r\n"},
		{"\n", "\r\n"},
		{"\r\n", "\r\n"},
		{"foo\rbar", "foo\r\nbar"},
		{"foo\nbar", "foo\r\nbar"},
		{"foo\r\nbar", "foo\r\nbar"},
		{"\r\r", "\r\n\r\n"},
		{"\n\n", "\r\n\r\n"},
		{"\r\r\n", "\r\n\r\n"},
		{"\n\r", "\r\n\r\n"},
		{"\rbar", "\r\nbar"},
		{"\nbar", "\r\nbar"},
		{"\r\nbar", "\r\nbar"},
	}
	for _, tt := range tests {
		t.Run(tt.before, func(t *testing.T) {
			b := bytes.Buffer{}
			w := Writer(&b)
			n, err := io.WriteString(w, tt.before)
			if err != nil {
				t.Errorf("Error: %v", err)
				return
			}
			if n != len(tt.before) {
				t.Errorf("Unexpected number of bytes written: %d, expected: %d", n, len(tt.before))
			}
			res := b.String()
			if tt.after != res {
				t.Errorf("Expected: %q, got: %q", tt.after, res)
			}
		})
	}
}