/
client_test.go
191 lines (138 loc) · 3.99 KB
/
client_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
package xmpp
import (
"bytes"
"fmt"
"os"
"regexp"
"template"
"testing"
)
type testIO struct {
out bytes.Buffer
in bytes.Buffer
}
func (buf *testIO) Read(p []byte) (int, os.Error) {
return buf.in.Read(p)
}
func (buf *testIO) Write(p []byte) (int, os.Error) {
return buf.out.Write(p)
}
// Resets both in and out buffers
func (buf *testIO) Reset() {
buf.in.Reset()
buf.out.Reset()
}
func (buf *testIO) PushString(str string) {
buf.in.Write(bytes.NewBufferString(str).Bytes())
}
func (buf *testIO) PushFixture(fixture string, data interface{}) {
buf.PushString(readFixture(fixture, data))
}
func (buf *testIO) PopString() string {
var byte_buf []byte
buf.out.Read(byte_buf)
return bytes.NewBuffer(byte_buf).String()
}
func setupClient() (io *testIO, client *Client) {
io = new(testIO)
client = &Client{conn: io}
return
}
type fixtureMessage struct {
Type, To, From, Body, Botid string
}
func TestLoop(t *testing.T) {
testio, client := setupClient()
testio.Reset()
// should return from looping, if testio returns EOF
client.Loop()
// if we got here, then loop must have existed, and we're good
}
func TestClientOnAny(t *testing.T) {
testio, client := setupClient()
var received string
client.OnAny(func(msg string) {
received = msg
})
testio.PushString("<hello world! />")
client.Loop()
assertMatch(t, "hello world!", received, "Should return the unknown message")
testio.PushString("<message><body>hello world!</body></message>")
client.Loop()
assertMatch(t, "message.*body.*hello world!.*/body.*/message", received, "Should return the message as well")
}
func TestClientOnMessage(t *testing.T) {
testio, client := setupClient()
testio.PushFixture("message", fixtureMessage{
Type: "any-possible-type",
Body: "Some stuff",
From: "joe",
To: "sam" })
var received *Message
client.OnMessage(func(msg Message) {
received = &msg
})
client.Loop()
should(t, "invoked callback", func() bool {
return received != nil
})
assertMatch(t, "any-possible-type", received.Type(), "Should capture its type")
}
func TestClientOnChatMessage(t *testing.T) {
testio, client := setupClient()
testio.PushFixture("message", fixtureMessage{
Type: "chat",
Body: "Hello world!",
From: "joe@example.com",
To: "sam@example.com" })
var received *Message
client.OnChatMessage(func(msg Message) {
received = &msg
})
client.Loop()
should(t, "have invoked onMessage callback", func() bool {
return received != nil
})
assertMatch(t, "chat", received.Type(), "Should be a chat message")
assertMatch(t, "Hello world!", received.Body(), "Should include the message")
assertMatch(t, "sam@example.com", received.To(), "Should include To")
assertMatch(t, "joe@example.com", received.From(), "Should include From")
}
func TestClientOnErrorMessage(t *testing.T) {
testio, client := setupClient()
testio.PushFixture("message", fixtureMessage{
Type: "error",
Body: "Something went wrong",
From: "joe",
To: "sam" })
testio.PushFixture("message", fixtureMessage{
Type: "chat",
Body: "Some message" })
var received *Message
client.OnErrorMessage(func(msg Message) {
received = &msg
})
client.Loop()
should(t, "have invoked the callback", func() bool {
return received != nil
})
assertMatch(t, "went.*wrong", received.Body(), "Should pick up the error only")
}
func TestClientOnUnknown(t *testing.T) {
testio, client := setupClient()
var received string
client.OnUnknown(func(msg string) {
received = msg
})
testio.PushString("<foobar>hello world!</foobar>")
client.Loop()
should(t, "invoke the unknown listener", func() bool {
match, _ := regexp.MatchString("<foobar>hello world!</foobar>", received)
return match
})
}
func readFixture(filename string, data interface{}) string {
buf := bytes.NewBufferString("")
template.MustParseFile(fmt.Sprintf("fixture/%s.xml.gt", filename), nil).Execute(buf, data)
return buf.String()
}