-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstanza_test.go
83 lines (73 loc) · 1.85 KB
/
stanza_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
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package stanza_test
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"strings"
"testing"
"mellium.im/xmlstream"
"mellium.im/xmpp/jid"
"mellium.im/xmpp/stanza"
)
type testReader []xml.Token
func (r *testReader) Token() (t xml.Token, err error) {
tr := *r
if len(tr) < 1 {
return nil, io.EOF
}
t, *r = tr[0], tr[1:]
return t, nil
}
var start = xml.StartElement{
Name: xml.Name{Local: "ping"},
}
type messageTest struct {
to string
typ stanza.MessageType
payload xml.TokenReader
out string
err error
}
var messageTests = [...]messageTest{
0: {
to: "[email protected]",
payload: &testReader{},
},
1: {
to: "[email protected]",
payload: &testReader{start, start.End()},
out: `<ping></ping>`,
typ: stanza.NormalMessage,
},
}
func TestMessage(t *testing.T) {
for i, tc := range messageTests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
b := new(bytes.Buffer)
e := xml.NewEncoder(b)
message := stanza.Message{To: jid.MustParse(tc.to), Type: tc.typ}.Wrap(tc.payload)
if _, err := xmlstream.Copy(e, message); err != tc.err {
t.Errorf("Unexpected error: want=`%v', got=`%v'", tc.err, err)
}
if err := e.Flush(); err != nil {
t.Fatalf("Error flushing: %q", err)
}
o := b.String()
jidattr := fmt.Sprintf(`to="%s"`, tc.to)
if !strings.Contains(o, jidattr) {
t.Errorf("Expected output to have attr `%s',\ngot=`%s'", jidattr, o)
}
typeattr := fmt.Sprintf(`type="%s"`, string(tc.typ))
if !strings.Contains(o, typeattr) {
t.Errorf("Expected output to have attr `%s',\ngot=`%s'", typeattr, o)
}
if !strings.Contains(o, tc.out) {
t.Errorf("Expected output to contain payload `%s',\ngot=`%s'", tc.out, o)
}
})
}
}