-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
session_message_test.go
133 lines (124 loc) · 3.5 KB
/
session_message_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
// Copyright 2021 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 xmpp_test
import (
"context"
"encoding/xml"
"strconv"
"testing"
"time"
"mellium.im/xmlstream"
"mellium.im/xmpp/internal/xmpptest"
"mellium.im/xmpp/stanza"
)
var sendMessageTests = [...]struct {
msg stanza.Message
canceled bool
payload xml.TokenReader
err error
writesBody bool
resp *stanza.Message
}{
0: {
msg: stanza.Message{ID: testIQID, Type: stanza.NormalMessage},
writesBody: true,
resp: &stanza.Message{ID: testIQID, Type: stanza.ErrorMessage},
},
1: {
msg: stanza.Message{ID: testIQID, Type: stanza.GroupChatMessage},
writesBody: true,
resp: &stanza.Message{ID: testIQID, Type: stanza.ErrorMessage},
},
2: {
msg: stanza.Message{Type: stanza.ErrorMessage, ID: testIQID},
writesBody: true,
},
3: {
msg: stanza.Message{ID: testIQID, Type: stanza.GroupChatMessage},
canceled: true,
writesBody: true,
err: context.Canceled,
},
}
func TestSendMessage(t *testing.T) {
for i, tc := range sendMessageTests {
tc := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Run("SendMessageElement", func(t *testing.T) {
s := xmpptest.NewClientServer(xmpptest.ServerHandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
if tc.resp != nil {
_, err := xmlstream.Copy(t, tc.resp.Wrap(nil))
return err
}
return nil
}))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
if tc.canceled {
cancel()
} else {
defer cancel()
}
resp, err := s.Client.SendMessageElement(ctx, tc.payload, tc.msg)
if err != tc.err {
t.Errorf("unexpected error, want=%q, got=%q", tc.err, err)
}
respMsg := stanza.Message{}
if resp != nil {
defer func() {
if err := resp.Close(); err != nil {
t.Errorf("Error closing response: %q", err)
}
}()
err = xml.NewTokenDecoder(resp).Decode(&respMsg)
if err != nil {
t.Errorf("error decoding response: %v", err)
}
}
switch {
case resp == nil && tc.resp != nil:
t.Errorf("expected response, but got none")
case resp != nil && tc.resp == nil:
t.Errorf("did not expect response, but got: %+v", respMsg)
}
})
t.Run("SendMessage", func(t *testing.T) {
s := xmpptest.NewClientServer(xmpptest.ServerHandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
if tc.resp != nil {
_, err := xmlstream.Copy(t, tc.resp.Wrap(nil))
return err
}
return nil
}))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
if tc.canceled {
cancel()
} else {
defer cancel()
}
resp, err := s.Client.SendMessage(ctx, tc.msg.Wrap(tc.payload))
if err != tc.err {
t.Errorf("unexpected error, want=%q, got=%q", tc.err, err)
}
respMsg := stanza.Message{}
if resp != nil {
defer func() {
if err := resp.Close(); err != nil {
t.Errorf("error closing response: %q", err)
}
}()
err = xml.NewTokenDecoder(resp).Decode(&respMsg)
if err != nil {
t.Errorf("error decoding response: %v", err)
}
}
switch {
case resp == nil && tc.resp != nil:
t.Errorf("expected response, but got none")
case resp != nil && tc.resp == nil:
t.Errorf("did not expect response, but got: %+v", respMsg)
}
})
})
}
}