This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/receiver_test.go

110 lines
2.3 KiB
Go
Raw Normal View History

2019-06-01 04:38:35 +02:00
package component
import (
"testing"
"github.com/stretchr/testify/assert"
"gosrc.io/xmpp"
2019-06-28 03:03:38 +02:00
"gosrc.io/xmpp/stanza"
2019-06-01 04:38:35 +02:00
)
2019-06-20 13:40:22 +02:00
type dummyComp struct {
Component
2019-06-28 03:03:38 +02:00
LastPacket stanza.Packet
2019-06-20 13:40:22 +02:00
}
2019-06-01 04:38:35 +02:00
2019-06-28 03:03:38 +02:00
func (d *dummyComp) Connect() (chan stanza.Packet, error) {
2019-06-20 13:40:22 +02:00
return nil, nil
}
2019-06-28 03:03:38 +02:00
func (d *dummyComp) Send(a stanza.Packet) {
2019-06-20 13:40:22 +02:00
d.LastPacket = a
}
2019-06-01 04:38:35 +02:00
2019-06-20 13:40:22 +02:00
type dummyXMPP struct {
xmpp.Sender
2019-06-28 03:03:38 +02:00
LastPacket stanza.Packet
2019-06-20 13:40:22 +02:00
}
2019-06-01 04:38:35 +02:00
2019-06-28 03:03:38 +02:00
func (d *dummyXMPP) Send(a stanza.Packet) error {
2019-06-20 13:40:22 +02:00
d.LastPacket = a
return nil
}
func TestReceive(t *testing.T) {
assert := assert.New(t)
s := &dummyXMPP{}
comp := &dummyComp{}
c := Config{
Host: "example.org",
Type: "monkeyservice",
XMPPDebug: true,
comp: comp,
}
2019-06-01 04:38:35 +02:00
// message
2019-06-28 03:03:38 +02:00
c.handleMessage(s, stanza.IQ{})
2019-06-20 13:40:22 +02:00
assert.Nil(comp.LastPacket)
2019-06-28 03:03:38 +02:00
c.handleMessage(s, stanza.Message{})
_, ok := comp.LastPacket.(stanza.Message)
2019-06-20 13:40:22 +02:00
assert.True(ok)
2019-06-01 04:38:35 +02:00
// unsupported iq
2019-06-28 03:03:38 +02:00
c.handleIQ(s, stanza.IQ{})
2019-06-20 13:40:22 +02:00
assert.Nil(s.LastPacket)
2019-06-28 03:03:38 +02:00
c.handleIQ(s, stanza.IQ{
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
2019-06-20 13:40:22 +02:00
})
assert.NotNil(s.LastPacket)
2019-06-28 03:03:38 +02:00
iq := s.LastPacket.(stanza.IQ)
assert.Equal(stanza.IQTypeError, iq.Type)
2019-06-01 04:38:35 +02:00
assert.Equal("feature-not-implemented", iq.Error.Reason)
2019-06-20 13:40:22 +02:00
s.LastPacket = nil
2019-06-01 04:38:35 +02:00
// iq disco info
2019-06-28 03:03:38 +02:00
c.handleDiscoInfo(s, stanza.IQ{
Payload: &stanza.DiscoInfo{},
2019-06-20 13:40:22 +02:00
})
assert.Nil(s.LastPacket)
2019-06-28 03:03:38 +02:00
c.handleDiscoInfo(s, stanza.IQ{
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
2019-06-20 13:40:22 +02:00
})
assert.Nil(s.LastPacket)
2019-06-28 03:03:38 +02:00
c.handleDiscoInfo(s, stanza.IQ{
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
Payload: &stanza.DiscoInfo{},
2019-06-01 04:38:35 +02:00
})
2019-06-20 13:40:22 +02:00
assert.NotNil(s.LastPacket)
2019-06-28 03:03:38 +02:00
iq = s.LastPacket.(stanza.IQ)
assert.Equal(stanza.IQTypeResult, iq.Type)
dinfo := iq.Payload.(*stanza.DiscoInfo)
assert.Equal("monkeyservice", dinfo.Identity[0].Name)
2019-06-20 13:40:22 +02:00
s.LastPacket = nil
2019-06-01 04:38:35 +02:00
// iq disco items
2019-06-28 03:03:38 +02:00
c.handleDiscoItems(s, stanza.IQ{
Payload: &stanza.DiscoItems{},
2019-06-20 13:40:22 +02:00
})
assert.Nil(s.LastPacket)
2019-06-28 03:03:38 +02:00
c.handleDiscoItems(s, stanza.IQ{
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
2019-06-20 13:40:22 +02:00
})
assert.Nil(s.LastPacket)
2019-06-28 03:03:38 +02:00
c.handleDiscoItems(s, stanza.IQ{
Attrs: stanza.Attrs{Type: stanza.IQTypeGet},
Payload: &stanza.DiscoItems{},
2019-06-01 04:38:35 +02:00
})
2019-06-20 13:40:22 +02:00
assert.NotNil(s.LastPacket)
2019-06-28 03:03:38 +02:00
iq = s.LastPacket.(stanza.IQ)
assert.Equal(stanza.IQTypeResult, iq.Type)
ditems := iq.Payload.(*stanza.DiscoItems)
2019-06-01 04:38:35 +02:00
assert.Equal("monkeyservice", ditems.Items[0].Name)
2019-06-20 13:40:22 +02:00
s.LastPacket = nil
2019-06-01 04:38:35 +02:00
}