Add tests for iq result routes
This commit is contained in:
parent
6a25856e85
commit
070934743f
|
@ -2,7 +2,9 @@ package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
|
@ -11,6 +13,58 @@ import (
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Test route & matchers
|
// Test route & matchers
|
||||||
|
|
||||||
|
func TestIqResultRoutes(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
router := NewRouter()
|
||||||
|
|
||||||
|
if router.iqResultRoutes == nil {
|
||||||
|
t.Fatal("NewRouter does not initialize isResultRoutes")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check other IQ does not matcah
|
||||||
|
conn := NewSenderMock()
|
||||||
|
iq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, Id: "4321"})
|
||||||
|
router.NewIqResultRoute(context.Background(), "1234").HandlerFunc(func(s Sender, p stanza.Packet) {
|
||||||
|
_ = s.SendRaw(successFlag)
|
||||||
|
})
|
||||||
|
if conn.String() == successFlag {
|
||||||
|
t.Fatal("IQ result with wrong ID was matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the IQ handler was called
|
||||||
|
conn = NewSenderMock()
|
||||||
|
iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, Id: "1234"})
|
||||||
|
router.route(conn, iq)
|
||||||
|
if conn.String() != successFlag {
|
||||||
|
t.Fatal("IQ result was not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
// The match must only happen once, so we if receive the same package again it
|
||||||
|
// must not be matched.
|
||||||
|
conn = NewSenderMock()
|
||||||
|
router.route(conn, iq)
|
||||||
|
if conn.String() == successFlag {
|
||||||
|
t.Fatal("IQ result was matched twice")
|
||||||
|
}
|
||||||
|
|
||||||
|
// After cancelling a route it should no longer match
|
||||||
|
conn = NewSenderMock()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
iq = stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, Id: "1234"})
|
||||||
|
router.NewIqResultRoute(ctx, "1234").HandlerFunc(func(s Sender, p stanza.Packet) {
|
||||||
|
_ = s.SendRaw(successFlag)
|
||||||
|
}).TimeoutHandlerFunc(func(err error) {
|
||||||
|
conn.SendRaw(cancelledFlag)
|
||||||
|
})
|
||||||
|
cancel()
|
||||||
|
// Yield the processor so the cancellation goroutine is triggered
|
||||||
|
runtime.Gosched()
|
||||||
|
router.route(conn, iq)
|
||||||
|
if conn.String() != cancelledFlag {
|
||||||
|
t.Fatal("IQ result route was matched after cancellation")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNameMatcher(t *testing.T) {
|
func TestNameMatcher(t *testing.T) {
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
router.HandleFunc("message", func(s Sender, p stanza.Packet) {
|
router.HandleFunc("message", func(s Sender, p stanza.Packet) {
|
||||||
|
@ -211,7 +265,8 @@ func TestCatchallMatcher(t *testing.T) {
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// SenderMock
|
// SenderMock
|
||||||
|
|
||||||
var successFlag = "matched"
|
const successFlag = "matched"
|
||||||
|
const cancelledFlag = "cancelled"
|
||||||
|
|
||||||
type SenderMock struct {
|
type SenderMock struct {
|
||||||
buffer *bytes.Buffer
|
buffer *bytes.Buffer
|
||||||
|
|
Loading…
Reference in a new issue