From e6a645dee0e3247cf7a445b77cfff12bcf52711f Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Mon, 15 Feb 2016 18:33:51 +0100 Subject: [PATCH] Fix missing type on IQ --- xmpp/iq.go | 5 +++++ xmpp/iq_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 xmpp/iq_test.go diff --git a/xmpp/iq.go b/xmpp/iq.go index a0c5cf2..e0f3177 100644 --- a/xmpp/iq.go +++ b/xmpp/iq.go @@ -29,6 +29,9 @@ func (iq *ClientIQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { if attr.Name.Local == "id" { iq.Id = attr.Value } + if attr.Name.Local == "type" { + iq.Type = attr.Value + } if attr.Name.Local == "to" { iq.To = attr.Value } @@ -75,6 +78,8 @@ func (iq *ClientIQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } } +// XMPPFormat returns the string representation of the XMPP packet. +// TODO: Should I simply rely on xml.Marshal ? func (iq *ClientIQ) XMPPFormat() string { if iq.Payload != nil { var payload []byte diff --git a/xmpp/iq_test.go b/xmpp/iq_test.go new file mode 100644 index 0000000..6468911 --- /dev/null +++ b/xmpp/iq_test.go @@ -0,0 +1,27 @@ +package xmpp + +import ( + "encoding/xml" + "reflect" + "testing" +) + +func TestUnmarshalIqs(t *testing.T) { + var tests = []struct { + iqString string + parsedIQ ClientIQ + }{ + {"", ClientIQ{XMLName: xml.Name{Space: "", Local: "iq"}, Packet: Packet{To: "test@localhost", Type: "set", Id: "1"}}}, + } + + for _, test := range tests { + var parsedIQ = new(ClientIQ) + err := xml.Unmarshal([]byte(test.iqString), parsedIQ) + if err != nil { + t.Errorf("Unmarshal(%s) returned error", test.iqString) + } + if !reflect.DeepEqual(parsedIQ, &test.parsedIQ) { + t.Errorf("Unmarshal(%s) expecting result %+v = %+v", test.iqString, parsedIQ, &test.parsedIQ) + } + } +}