package xmpp_test import ( "encoding/xml" "testing" "github.com/google/go-cmp/cmp" "gosrc.io/xmpp" ) func TestGenerateMessage(t *testing.T) { message := xmpp.NewMessage("chat", "admin@localhost", "test@localhost", "1", "en") message.Body = "Hi" message.Subject = "Msg Subject" data, err := xml.Marshal(message) if err != nil { t.Errorf("cannot marshal xml structure") } parsedMessage := xmpp.Message{} if err = xml.Unmarshal(data, &parsedMessage); err != nil { t.Errorf("Unmarshal(%s) returned error", data) } if !xmlEqual(parsedMessage, message) { t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage, message)) } } func TestDecodeError(t *testing.T) { str := ` ` parsedMessage := xmpp.Message{} if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil { t.Errorf("message error stanza unmarshall error: %v", err) return } if parsedMessage.Error.Type != "cancel" { t.Errorf("incorrect error type: %s", parsedMessage.Error.Type) } } func TestDecodeXEP0184(t *testing.T) { str := ` My lord, dispatch; read o'er these articles. ` parsedMessage := xmpp.Message{} if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil { t.Errorf("message receipt unmarshall error: %v", err) return } if parsedMessage.Body != "My lord, dispatch; read o'er these articles." { t.Errorf("Unexpected body: '%s'", parsedMessage.Body) } if len(parsedMessage.Extensions) < 1 { t.Errorf("no extension found on parsed message") return } switch ext := parsedMessage.Extensions[0].(type) { case *xmpp.Receipt: if ext.XMLName.Local != "request" { t.Errorf("unexpected extension: %s:%s", ext.XMLName.Space, ext.XMLName.Local) } default: t.Errorf("could not find receipt extension") } }