go-xmpp/message_test.go

50 lines
1.2 KiB
Go
Raw Normal View History

2019-05-16 16:03:12 +00:00
package xmpp_test
2018-01-24 08:38:02 +00:00
import (
"encoding/xml"
"testing"
"github.com/google/go-cmp/cmp"
"gosrc.io/xmpp"
2018-01-24 08:38:02 +00:00
)
func TestGenerateMessage(t *testing.T) {
message := xmpp.NewMessage("chat", "admin@localhost", "test@localhost", "1", "en")
2018-01-24 08:38:02 +00:00
message.Body = "Hi"
message.Subject = "Msg Subject"
data, err := xml.Marshal(message)
if err != nil {
t.Errorf("cannot marshal xml structure")
}
parsedMessage := xmpp.Message{}
2018-01-24 08:38:02 +00:00
if err = xml.Unmarshal(data, &parsedMessage); err != nil {
t.Errorf("Unmarshal(%s) returned error", data)
}
2018-01-25 10:00:20 +00:00
if !xmlEqual(parsedMessage, message) {
t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage, message))
2018-01-24 08:38:02 +00:00
}
}
func TestDecodeError(t *testing.T) {
str := `<message from='juliet@capulet.com'
id='msg_1'
to='romeo@montague.lit'
type='error'>
<error type='cancel'>
<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</message>`
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)
}
}