summaryrefslogtreecommitdiffstats
path: root/message_test.go
blob: 6955fc32905dfb61bc18795082432b64f158ee34 (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
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 cnp

import (
	"bytes"
	"io"
	"strconv"
	"strings"
	"testing"
)

var (
	messageTests = []messageTest{
		{
			"qweasd", "text/plain", nil, nil,
			"cnp/0.3 test1 length=6 type=text/plain\nqweasd",
			&Message{
				Header: Header{
					VersionMajor: 0,
					VersionMinor: 3,
					Intent:       "test1",
					Parameters: Parameters{
						"type":   "text/plain",
						"length": "6",
					},
				},
				Body: strings.NewReader("qweasd"),
			},
		},

		{
			"qweasd", "text", nil, ErrorInvalid{},
			"cnp/0.3 test2 length=w type=text/plain\nqweasd",
			&Message{
				Header: Header{
					VersionMajor: 0,
					VersionMinor: 3,
					Intent:       "test2",
					Parameters: Parameters{
						"type":   "text/plain",
						"length": "w",
					},
				},
				Body: strings.NewReader("qweasd"),
			},
		},

		{
			"", "text/plain", ErrorSyntax{}, nil,
			"cnp/0.3 test3 type=text/plain",
			nil,
		},

		{
			"", "text/plain", nil, nil,
			"cnp/0.3 test4 type=text/plain\n",
			&Message{
				Header: Header{
					VersionMajor: 0,
					VersionMinor: 3,
					Intent:       "test4",
					Parameters: Parameters{
						"type": "text/plain",
					},
				},
				Body: nil,
			},
		},
	}
)

type messageTest struct {
	b, t string
	p, v error
	s    string
	m    *Message
}

func (tst *messageTest) Reset() {
	if tst.m != nil && tst.b != "" {
		tst.m.Body = strings.NewReader(tst.b)
	}
}

func TestParse(t *testing.T) {
	for _, tst := range messageTests {
		tst.Reset()
		msg, err := ParseMessage(strings.NewReader(tst.s))
		if tst.p != nil || err != nil {
			if !errorEqual(tst.p, err) {
				t.Errorf("ParseMessage(%q): expected error %+v, got %+v (%v)", tst.s, tst.p, err, err)
				continue
			}
		} else if !msgEqual(msg, tst.m) {
			t.Errorf("\nexpected: %+v\ngot:      %+v", tst.m, msg)
		} else if l := msg.Length(); tst.v == nil && l != int64(len(tst.b)) {
			t.Errorf("%+v: expected length %d, got %d", tst.m, len(tst.b), l)
		} else if err = msg.Validate(); !errorEqual(err, tst.v) {
			t.Errorf("%+v: expected validation error %+v, got %+v (%s)", msg, tst.v, err, err)
		}
	}
}

func TestNew(t *testing.T) {
	for i, tst := range messageTests {
		if tst.v != nil || tst.p != nil {
			continue // skip invalid messages
		}
		tst.Reset()
		var r io.Reader
		if tst.b != "" {
			r = strings.NewReader(tst.b)
		}
		msg := NewMessage("test"+strconv.Itoa(i+1), r)
		msg.SetParam("type", tst.t)
		if !msgEqual(msg, tst.m) {
			t.Errorf("\nexpected: %+v\ngot:      %+v", tst.m, msg)
		}
	}
}

func TestWrite(t *testing.T) {
	for _, tst := range messageTests {
		if tst.p != nil {
			continue // skip invalid messages
		}
		tst.Reset()
		var buf bytes.Buffer
		err := tst.m.Write(&buf)
		if err != nil {
			t.Errorf("%+v: error writing message body: %s", tst.m, err)
		}
		if buf.String() != tst.s {
			t.Errorf("%+v:\nexpected: %q\ngot:      %q", tst.m, tst.s, buf.String())
		}
	}
}

func TestComputeLength(t *testing.T) {
	s := "qweasd"
	r := &testStringReader{"qweasd"}
	msg := NewMessage("ok", r)
	if _, ok := msg.Header.Parameters["length"]; msg.Length() != 0 || msg.Param("length") != "" || ok {
		t.Fatalf("%+v: expected no length parameter", msg)
	}
	if err := msg.ComputeLength(); err != nil {
		t.Fatalf("%+v.ComputeLength(): error: %s", msg, err)
	}
	if l := msg.Length(); l != int64(len(s)) {
		t.Fatalf("%+v: invalid length %d, expected %d\n", msg, l, len(s))
	}
	if _, ok := msg.Body.(*testStringReader); r.s != "" || ok || r == msg.Body {
		t.Fatalf("%+v: did not buffer body correctly", msg)
	}
	if !bodyEqual(msg.Body, strings.NewReader(s)) {
		t.Fatalf("%+v: incorrect buffered body", msg)
	}
}

func TestParseTooLarge(t *testing.T) {
	s := "cnp/0.3 ok text="
	str := s + strings.Repeat(".", MaxHeaderLength-len(s)) + "\n"
	_, err := ParseMessage(strings.NewReader(str))
	if _, ok := err.(ErrorTooLarge); !ok {
		t.Errorf("\nexpected: ErrorTooLarge\ngot:      %+v", err)
	}
}

type noopCloser struct {
	r      io.Reader
	closed bool
}

func (n *noopCloser) Close() error {
	n.closed = true
	return nil
}

func (n *noopCloser) Read(b []byte) (int, error) {
	return n.r.Read(b)
}

func TestClose(t *testing.T) {
	var r io.Reader = strings.NewReader("cnp/0.3 ok\nqweasd")
	if msg, err := ParseMessage(r); err != nil {
		t.Errorf("ParseMessage error: %v", err)
	} else if err = msg.Close(); err != nil {
		t.Errorf("Error closing message: %v", err)
	}
	r = &noopCloser{r: strings.NewReader("cnp/0.3 ok\nqweasd")}
	if msg, err := ParseMessage(r); err != nil {
		t.Errorf("ParseMessage error: %v", err)
	} else if err = msg.Close(); err != nil {
		t.Errorf("Error closing message: %v", err)
	} else if !r.(*noopCloser).closed {
		t.Errorf("Reader was not closed")
	}
	r = &noopCloser{r: strings.NewReader("qweasd")}
	msg := NewMessage("ok", r)
	if err := msg.Close(); err != nil {
		t.Errorf("Error closing message: %v", err)
	} else if !r.(*noopCloser).closed {
		t.Errorf("Reader was not closed")
	}
}