~samwhited/xmpp

ref: dcc6772f31033948a00a7dfb9232a120120be5b7 xmpp/carbons/handler_test.go -rw-r--r-- 1.6 KiB
dcc6772fSam Whited all: add strict namespace checking to mux 26 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package carbons_test

import (
	"context"
	"encoding/xml"
	"strings"
	"testing"

	"mellium.im/xmpp/carbons"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

func TestHandler(t *testing.T) {
	wait := make(chan struct{})
	h := carbons.Handler{
		F: func(m stanza.Message, sent bool, inner xml.TokenReader) error {
			defer close(wait)
			if sent {
				t.Error("expected received not to set sent")
			}
			tok, err := inner.Token()
			if err != nil {
				return err
			}
			if start, ok := tok.(xml.StartElement); !ok || start.Name.Local != "message" {
				t.Errorf("inner payload not handled correctly, got %T token: %[1]v", tok)
			}
			return nil
		},
	}
	m := mux.New(ns.Client, carbons.Handle(h))
	s := xmpptest.NewClientServer(xmpptest.ClientHandler(m))
	defer s.Close()
	const recv = `<message xmlns='jabber:client'
         from='romeo@montague.example'
         to='romeo@montague.example/home'
         type='chat'><received xmlns='urn:xmpp:carbons:2'><forwarded xmlns='urn:xmpp:forward:0'><message xmlns='jabber:client' from='juliet@capulet.example/balcony' to='romeo@montague.example/garden' type='chat'><body>What man art thou that, thus bescreen'd in night, so stumblest on my counsel?</body><thread>0e3141cd80894871a68e6fe6b1ec56fa</thread></message></forwarded></received></message>`
	d := xml.NewDecoder(strings.NewReader(recv))
	err := s.Server.Send(context.Background(), d)
	if err != nil {
		t.Fatalf("error sending carbon: %v", err)
	}
	<-wait
}