2019-02-10 16:53:18 +00:00
|
|
|
package xmpp_test // import "gosrc.io/xmpp"
|
2016-02-15 17:33:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2018-01-26 10:16:04 +00:00
|
|
|
"strings"
|
2016-02-15 17:33:51 +00:00
|
|
|
"testing"
|
2018-01-17 17:47:34 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-02-10 16:53:18 +00:00
|
|
|
"gosrc.io/xmpp"
|
2016-02-15 17:33:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalIqs(t *testing.T) {
|
2017-10-04 20:51:28 +00:00
|
|
|
//var cs1 = new(iot.ControlSet)
|
2016-02-15 17:33:51 +00:00
|
|
|
var tests = []struct {
|
|
|
|
iqString string
|
2019-02-10 16:53:18 +00:00
|
|
|
parsedIQ xmpp.IQ
|
2016-02-15 17:33:51 +00:00
|
|
|
}{
|
2019-02-10 16:53:18 +00:00
|
|
|
{"<iq id=\"1\" type=\"set\" to=\"test@localhost\"/>",
|
|
|
|
xmpp.IQ{XMLName: xml.Name{Space: "", Local: "iq"}, PacketAttrs: xmpp.PacketAttrs{To: "test@localhost", Type: "set", Id: "1"}}},
|
2018-01-13 17:50:17 +00:00
|
|
|
//{"<iq xmlns=\"jabber:client\" id=\"2\" type=\"set\" to=\"test@localhost\" from=\"server\"><set xmlns=\"urn:xmpp:iot:control\"/></iq>", IQ{XMLName: xml.Name{Space: "jabber:client", Local: "iq"}, PacketAttrs: PacketAttrs{To: "test@localhost", From: "server", Type: "set", Id: "2"}, Payload: cs1}},
|
2016-02-15 17:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2019-02-10 16:53:18 +00:00
|
|
|
parsedIQ := xmpp.IQ{}
|
2018-01-18 16:03:54 +00:00
|
|
|
err := xml.Unmarshal([]byte(test.iqString), &parsedIQ)
|
2016-02-15 17:33:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", test.iqString)
|
|
|
|
}
|
2018-01-18 16:03:54 +00:00
|
|
|
|
|
|
|
if !xmlEqual(parsedIQ, test.parsedIQ) {
|
|
|
|
t.Errorf("non matching items\n%s", cmp.Diff(parsedIQ, test.parsedIQ))
|
2016-02-15 17:33:51 +00:00
|
|
|
}
|
2018-01-18 16:03:54 +00:00
|
|
|
|
2016-02-15 17:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-15 11:28:34 +00:00
|
|
|
|
|
|
|
func TestGenerateIq(t *testing.T) {
|
2019-02-10 16:53:18 +00:00
|
|
|
iq := xmpp.NewIQ("result", "admin@localhost", "test@localhost", "1", "en")
|
|
|
|
payload := xmpp.DiscoInfo{
|
|
|
|
Identity: xmpp.Identity{
|
2018-01-16 21:33:21 +00:00
|
|
|
Name: "Test Gateway",
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "mqtt",
|
|
|
|
},
|
2019-02-10 16:53:18 +00:00
|
|
|
Features: []xmpp.Feature{
|
2018-01-17 17:47:34 +00:00
|
|
|
{Var: "http://jabber.org/protocol/disco#info"},
|
|
|
|
{Var: "http://jabber.org/protocol/disco#item"},
|
|
|
|
},
|
2018-01-16 21:33:21 +00:00
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
2018-01-17 17:47:34 +00:00
|
|
|
|
2018-01-16 21:33:21 +00:00
|
|
|
data, err := xml.Marshal(iq)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure")
|
|
|
|
}
|
|
|
|
|
2018-01-26 10:16:04 +00:00
|
|
|
if strings.Contains(string(data), "<error ") {
|
|
|
|
t.Error("empty error should not be serialized")
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:53:18 +00:00
|
|
|
parsedIQ := xmpp.IQ{}
|
2018-01-18 16:03:54 +00:00
|
|
|
if err = xml.Unmarshal(data, &parsedIQ); err != nil {
|
2018-01-15 11:28:34 +00:00
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:47:34 +00:00
|
|
|
if !xmlEqual(parsedIQ.Payload, iq.Payload) {
|
|
|
|
t.Errorf("non matching items\n%s", cmp.Diff(parsedIQ.Payload, iq.Payload))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 17:09:13 +00:00
|
|
|
func TestErrorTag(t *testing.T) {
|
2019-02-10 16:53:18 +00:00
|
|
|
xError := xmpp.Err{
|
2018-01-20 17:09:13 +00:00
|
|
|
XMLName: xml.Name{Local: "error"},
|
|
|
|
Code: 503,
|
|
|
|
Type: "cancel",
|
|
|
|
Reason: "service-unavailable",
|
|
|
|
Text: "User session not found",
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := xml.Marshal(xError)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:53:18 +00:00
|
|
|
parsedError := xmpp.Err{}
|
2018-01-20 17:09:13 +00:00
|
|
|
if err = xml.Unmarshal(data, &parsedError); err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !xmlEqual(parsedError, xError) {
|
|
|
|
t.Errorf("non matching items\n%s", cmp.Diff(parsedError, xError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:24:34 +00:00
|
|
|
func TestDiscoItems(t *testing.T) {
|
2019-02-10 16:53:18 +00:00
|
|
|
iq := xmpp.NewIQ("get", "romeo@montague.net/orchard", "catalog.shakespeare.lit", "items3", "en")
|
|
|
|
payload := xmpp.DiscoItems{
|
2018-01-26 08:24:34 +00:00
|
|
|
Node: "music",
|
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
|
|
|
|
|
|
|
data, err := xml.Marshal(iq)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure")
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:53:18 +00:00
|
|
|
parsedIQ := xmpp.IQ{}
|
2018-01-26 08:24:34 +00:00
|
|
|
if err = xml.Unmarshal(data, &parsedIQ); err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !xmlEqual(parsedIQ.Payload, iq.Payload) {
|
|
|
|
t.Errorf("non matching items\n%s", cmp.Diff(parsedIQ.Payload, iq.Payload))
|
|
|
|
}
|
|
|
|
}
|