From 28fb5bf61b746e7806f0b9cb446ce7403bb2d71c Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Wed, 24 Jan 2018 09:38:02 +0100 Subject: [PATCH] Add basic test for messages --- message.go | 13 +++++++++++++ message_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 message_test.go diff --git a/message.go b/message.go index 92e7446..24c2d10 100644 --- a/message.go +++ b/message.go @@ -21,6 +21,19 @@ func (Message) Name() string { 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{} var message messageDecoder diff --git a/message_test.go b/message_test.go new file mode 100644 index 0000000..f32a9f7 --- /dev/null +++ b/message_test.go @@ -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)) + } +}