package stanza_test import ( "encoding/xml" "strings" "testing" "github.com/google/go-cmp/cmp" ) func TestUnmarshalIqs(t *testing.T) { //var cs1 = new(iot.ControlSet) var tests = []struct { iqString string parsedIQ IQ }{ {"", IQ{XMLName: xml.Name{Local: "iq"}, Attrs: Attrs{Type: IQTypeSet, To: "test@localhost", Id: "1"}}}, //{"", IQ{XMLName: xml.Name{Space: "jabber:client", Local: "iq"}, PacketAttrs: PacketAttrs{To: "test@localhost", From: "server", Type: "set", Id: "2"}, Payload: cs1}}, } for _, test := range tests { parsedIQ := IQ{} err := xml.Unmarshal([]byte(test.iqString), &parsedIQ) if err != nil { t.Errorf("Unmarshal(%s) returned error", test.iqString) } if !xmlEqual(parsedIQ, test.parsedIQ) { t.Errorf("non matching items\n%s", cmp.Diff(parsedIQ, test.parsedIQ)) } } } func TestGenerateIq(t *testing.T) { iq := NewIQ(Attrs{Type: IQTypeResult, From: "admin@localhost", To: "test@localhost", Id: "1"}) payload := DiscoInfo{ Identity: Identity{ Name: "Test Gateway", Category: "gateway", Type: "mqtt", }, Features: []Feature{ {Var: NSDiscoInfo}, {Var: NSDiscoItems}, }, } iq.Payload = &payload data, err := xml.Marshal(iq) if err != nil { t.Errorf("cannot marshal xml structure") } if strings.Contains(string(data), " Not subscribed ` parsedIQ := IQ{} err := xml.Unmarshal([]byte(iq), &parsedIQ) if err != nil { t.Errorf("Unmarshal error: %s", iq) return } if parsedIQ.Error.Reason != "subscription-required" { t.Errorf("incorrect error value: '%s'", parsedIQ.Error.Reason) } } func TestUnknownPayload(t *testing.T) { iq := ` ` parsedIQ := IQ{} err := xml.Unmarshal([]byte(iq), &parsedIQ) if err != nil { t.Errorf("Unmarshal error: %#v (%s)", err, iq) return } if parsedIQ.Any.XMLName.Space != "unknown:ns" { t.Errorf("could not extract namespace: '%s'", parsedIQ.Any.XMLName.Space) } }