Add basic test for messages

This commit is contained in:
Mickael Remond 2018-01-24 09:38:02 +01:00
parent 7ae2adca9f
commit 28fb5bf61b
No known key found for this signature in database
GPG key ID: E6F6045D79965AA3
2 changed files with 41 additions and 0 deletions

View file

@ -21,6 +21,19 @@ func (Message) Name() string {
return "message" return "message"
} }
func NewMessage(msgtype, from, to, id, lang string) Message {
return Message{
XMLName: xml.Name{Local: "message"},
PacketAttrs: PacketAttrs{
Id: id,
From: from,
To: to,
Type: msgtype,
Lang: lang,
},
}
}
type messageDecoder struct{} type messageDecoder struct{}
var message messageDecoder var message messageDecoder

28
message_test.go Normal file
View file

@ -0,0 +1,28 @@
package xmpp
import (
"encoding/xml"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestGenerateMessage(t *testing.T) {
message := 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 := Message{}
if err = xml.Unmarshal(data, &parsedMessage); err != nil {
t.Errorf("Unmarshal(%s) returned error", data)
}
if !xmlEqual(parsedMessage.Body, message.Body) {
t.Errorf("non matching items\n%s", cmp.Diff(parsedMessage.Body, message.Body))
}
}