2019-05-16 09:03:44 -07:00
|
|
|
package xmpp_test
|
2018-01-25 02:00:20 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-10 08:53:18 -08:00
|
|
|
"gosrc.io/xmpp"
|
|
|
|
|
2018-01-25 02:00:20 -08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGeneratePresence(t *testing.T) {
|
2019-06-22 02:13:33 -07:00
|
|
|
presence := xmpp.NewPresence(xmpp.Attrs{From: "admin@localhost", To: "test@localhost", Id: "1"})
|
|
|
|
presence.Show = xmpp.PresenceShowChat
|
2018-01-25 02:00:20 -08:00
|
|
|
|
|
|
|
data, err := xml.Marshal(presence)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure")
|
|
|
|
}
|
|
|
|
|
2019-02-10 08:53:18 -08:00
|
|
|
var parsedPresence xmpp.Presence
|
2018-01-25 02:00:20 -08:00
|
|
|
if err = xml.Unmarshal(data, &parsedPresence); err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !xmlEqual(parsedPresence, presence) {
|
|
|
|
t.Errorf("non matching items\n%s", cmp.Diff(parsedPresence, presence))
|
|
|
|
}
|
|
|
|
}
|
2019-02-10 08:53:18 -08:00
|
|
|
|
|
|
|
func TestPresenceSubElt(t *testing.T) {
|
|
|
|
// Test structure to ensure that show, status and priority are correctly defined as presence
|
|
|
|
// package sub-elements
|
|
|
|
type pres struct {
|
2019-06-22 02:13:33 -07:00
|
|
|
Show xmpp.PresenceShow `xml:"show"`
|
|
|
|
Status string `xml:"status"`
|
|
|
|
Priority int8 `xml:"priority"`
|
2019-02-10 08:53:18 -08:00
|
|
|
}
|
|
|
|
|
2019-06-22 02:13:33 -07:00
|
|
|
presence := xmpp.NewPresence(xmpp.Attrs{From: "admin@localhost", To: "test@localhost", Id: "1"})
|
|
|
|
presence.Show = xmpp.PresenceShowXA
|
2019-02-10 08:53:18 -08:00
|
|
|
presence.Status = "Coding"
|
2019-06-20 09:36:57 -07:00
|
|
|
presence.Priority = 10
|
2019-02-10 08:53:18 -08:00
|
|
|
|
|
|
|
data, err := xml.Marshal(presence)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure")
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsedPresence pres
|
|
|
|
if err = xml.Unmarshal(data, &parsedPresence); err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedPresence.Show != presence.Show {
|
|
|
|
t.Errorf("cannot read 'show' as presence subelement (%s)", parsedPresence.Show)
|
|
|
|
}
|
|
|
|
if parsedPresence.Status != presence.Status {
|
|
|
|
t.Errorf("cannot read 'status' as presence subelement (%s)", parsedPresence.Status)
|
|
|
|
}
|
|
|
|
if parsedPresence.Priority != presence.Priority {
|
2019-06-20 09:36:57 -07:00
|
|
|
t.Errorf("cannot read 'priority' as presence subelement (%d)", parsedPresence.Priority)
|
2019-02-10 08:53:18 -08:00
|
|
|
}
|
|
|
|
}
|