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"
|
2019-02-10 16:53:18 +00:00
|
|
|
"gosrc.io/xmpp"
|
2018-01-24 08:38:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateMessage(t *testing.T) {
|
2019-02-10 16:53:18 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:53:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2019-05-31 21:35:04 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeXEP0184(t *testing.T) {
|
|
|
|
str := `<message
|
|
|
|
from='northumberland@shakespeare.lit/westminster'
|
|
|
|
id='richard2-4.1.247'
|
|
|
|
to='kingrichard@royalty.england.lit/throne'>
|
|
|
|
<body>My lord, dispatch; read o'er these articles.</body>
|
|
|
|
<request xmlns='urn:xmpp:receipts'/>
|
|
|
|
</message>`
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|