parent
836e723273
commit
7a4364be95
@ -0,0 +1,42 @@
|
||||
package xmpp_test
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
func TestDecodeRequest(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.ReceiptRequest:
|
||||
if ext.XMLName.Local != "request" {
|
||||
t.Errorf("unexpected extension: %s:%s", ext.XMLName.Space, ext.XMLName.Local)
|
||||
}
|
||||
default:
|
||||
t.Errorf("could not find receipts extension")
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package xmpp // import "gosrc.io/xmpp"
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegistry_RegisterMsgExt(t *testing.T) {
|
||||
// Setup registry
|
||||
typeRegistry := newRegistry()
|
||||
|
||||
// Register an element
|
||||
name := xml.Name{Space: "urn:xmpp:receipts", Local: "request"}
|
||||
typeRegistry.MapExtension(PKTMessage, name, ReceiptRequest{})
|
||||
|
||||
// Match that element
|
||||
receipt := typeRegistry.GetMsgExtension(name)
|
||||
if receipt == nil {
|
||||
t.Error("cannot read element type from registry")
|
||||
return
|
||||
}
|
||||
|
||||
switch r := receipt.(type) {
|
||||
case *ReceiptRequest:
|
||||
default:
|
||||
t.Errorf("Registry did not return expected type ReceiptRequest: %v", reflect.TypeOf(r))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRegistryGet(b *testing.B) {
|
||||
// Setup registry
|
||||
typeRegistry := newRegistry()
|
||||
|
||||
// Register an element
|
||||
name := xml.Name{Space: "urn:xmpp:receipts", Local: "request"}
|
||||
typeRegistry.MapExtension(PKTMessage, name, ReceiptRequest{})
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Match that element
|
||||
receipt := typeRegistry.GetExtensionType(PKTMessage, name)
|
||||
if receipt == nil {
|
||||
b.Error("cannot read element type from registry")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue