Browse Source

Support for XEP-0334

160-regression
CORNIERE Rémi 2 years ago
parent
commit
6a3ee5b0a5
3 changed files with 109 additions and 1 deletions
  1. +1
    -1
      go.sum
  2. +36
    -0
      stanza/msg_hint.go
  3. +72
    -0
      stanza/msg_hint_test.go

+ 1
- 1
go.sum View File

@ -201,7 +201,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=


+ 36
- 0
stanza/msg_hint.go View File

@ -0,0 +1,36 @@
package stanza
import "encoding/xml"
/*
Support for:
- XEP-0334: Message Processing Hints: https://xmpp.org/extensions/xep-0334.html
Pointers should be used to keep consistent with unmarshal. Eg :
msg.Extensions = append(msg.Extensions, &stanza.HintNoCopy{}, &stanza.HintStore{})
*/
type HintNoPermanentStore struct {
MsgExtension
XMLName xml.Name `xml:"urn:xmpp:hints no-permanent-store"`
}
type HintNoStore struct {
MsgExtension
XMLName xml.Name `xml:"urn:xmpp:hints no-store"`
}
type HintNoCopy struct {
MsgExtension
XMLName xml.Name `xml:"urn:xmpp:hints no-copy"`
}
type HintStore struct {
MsgExtension
XMLName xml.Name `xml:"urn:xmpp:hints store"`
}
func init() {
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-permanent-store"}, HintNoPermanentStore{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-store"}, HintNoStore{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-copy"}, HintNoCopy{})
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "store"}, HintStore{})
}

+ 72
- 0
stanza/msg_hint_test.go View File

@ -0,0 +1,72 @@
package stanza_test
import (
"encoding/xml"
"gosrc.io/xmpp/stanza"
"reflect"
"strings"
"testing"
)
const msg_const = `
<message
from="romeo@montague.lit/laptop"
to="juliet@capulet.lit/laptop">
<body>V unir avtugf pybnx gb uvqr zr sebz gurve fvtug</body>
<no-copy xmlns="urn:xmpp:hints"></no-copy>
<no-permanent-store xmlns="urn:xmpp:hints"></no-permanent-store>
<no-store xmlns="urn:xmpp:hints"></no-store>
<store xmlns="urn:xmpp:hints"></store>
</message>`
func TestSerializationHint(t *testing.T) {
msg := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"})
msg.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug"
msg.Extensions = append(msg.Extensions, stanza.HintNoCopy{}, stanza.HintNoPermanentStore{}, stanza.HintNoStore{}, stanza.HintStore{})
data, _ := xml.Marshal(msg)
if strings.ReplaceAll(strings.Join(strings.Fields(msg_const), ""), "\n", "") != strings.Join(strings.Fields(string(data)), "") {
t.Fatalf("marshalled message does not match expected message")
}
}
func TestUnmarshalHints(t *testing.T) {
// Init message as in the const value
msgConst := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"})
msgConst.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug"
msgConst.Extensions = append(msgConst.Extensions, &stanza.HintNoCopy{}, &stanza.HintNoPermanentStore{}, &stanza.HintNoStore{}, &stanza.HintStore{})
// Compare message with the const value
msg := stanza.Message{}
err := xml.Unmarshal([]byte(msg_const), &msg)
if err != nil {
t.Fatal(err)
}
if msgConst.XMLName.Local != msg.XMLName.Local {
t.Fatalf("message tags do not match. Expected: %s, Actual: %s", msgConst.XMLName.Local, msg.XMLName.Local)
}
if msgConst.Body != msg.Body {
t.Fatalf("message bodies do not match. Expected: %s, Actual: %s", msgConst.Body, msg.Body)
}
if !reflect.DeepEqual(msgConst.Attrs, msg.Attrs) {
t.Fatalf("attributes do not match")
}
if !reflect.DeepEqual(msgConst.Error, msg.Error) {
t.Fatalf("attributes do not match")
}
var found bool
for _, ext := range msgConst.Extensions {
for _, strExt := range msg.Extensions {
if reflect.TypeOf(ext) == reflect.TypeOf(strExt) {
found = true
break
}
}
if !found {
t.Fatalf("extensions do not match")
}
found = false
}
}

Loading…
Cancel
Save