~samwhited/xmpp

ref: 8a516e9e064619dcb3d84499b66e1562546be918 xmpp/receipts/receipts_test.go -rw-r--r-- 2.6 KiB
8a516e9eSam Whited .builds: validate license headers 10 months 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2020 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 receipts_test

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

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

func TestClosedDoesNotPanic(t *testing.T) {
	h := &receipts.Handler{}

	bw := &bytes.Buffer{}
	s := xmpptest.NewSession(0, bw)
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	err := h.SendMessageElement(ctx, s, nil, stanza.Message{
		ID: "123",
	})
	if err != context.Canceled {
		t.Fatalf("unexpected error: %v", err)
	}

	msg := stanza.Message{
		XMLName: xml.Name{Space: ns.Client, Local: "message"},
		Type:    stanza.ChatMessage,
	}
	r := msg.Wrap(xmlstream.Wrap(nil, xml.StartElement{
		Name: xml.Name{Local: "received", Space: receipts.NS},
		Attr: []xml.Attr{{Name: xml.Name{Local: "id"}, Value: "123"}},
	}))

	bw = &bytes.Buffer{}
	e := xml.NewEncoder(bw)
	// If the has not been removed from handling when the context is canceled,
	// this will panic (effectively failing the test).
	err = h.HandleMessage(msg, struct {
		xml.TokenReader
		xmlstream.Encoder
	}{
		TokenReader: r,
		Encoder:     e,
	})
	if err != nil {
		t.Fatalf("error handling response: %v", err)
	}
}

// TODO: find a way to test that SendMessageElement actually matches up the
// response correctly (ie. don't timeout, use the test server).
func TestRoundTrip(t *testing.T) {
	h := &receipts.Handler{}

	var req bytes.Buffer
	s := xmpptest.NewSession(0, &req)

	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	err := h.SendMessageElement(ctx, s, nil, stanza.Message{
		ID:   "123",
		Type: stanza.NormalMessage,
	})
	if err != context.Canceled {
		t.Fatalf("unexpected error: %v", err)
	}

	d := xml.NewDecoder(strings.NewReader(req.String()))
	d.DefaultSpace = ns.Server
	tok, _ := d.Token()
	start := tok.(xml.StartElement)
	var b strings.Builder
	e := xml.NewEncoder(&b)

	m := mux.New(receipts.Handle(h))
	err = m.HandleXMPP(struct {
		xml.TokenReader
		xmlstream.Encoder
	}{
		TokenReader: d,
		Encoder:     e,
	}, &start)
	if err != nil {
		t.Errorf("unexpected error in handler: %v", err)
	}
	err = e.Flush()
	if err != nil {
		t.Errorf("unexpected error flushing encoder: %v", err)
	}

	out := b.String()
	const expected = `<message xmlns="jabber:server" type="normal"><received xmlns="urn:xmpp:receipts" id="123"></received></message>`
	if out != expected {
		t.Errorf("got=%s, want=%s", out, expected)
	}
}