~samwhited/xmpp

ref: 7fa903e220ac86501595262dd3d89c3b51f9be7d xmpp/disco/handler_test.go -rw-r--r-- 2.4 KiB
7fa903e2Sam Whited all: be more strict about stanza namespaces 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
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
// 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 disco_test

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

	"mellium.im/xmlstream"
	"mellium.im/xmpp/disco"
	"mellium.im/xmpp/disco/items"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

func TestFeaturesRoundTrip(t *testing.T) {
	m := mux.New(disco.Handle())
	cs := xmpptest.NewClientServer(
		xmpptest.ServerHandler(m),
	)

	info, err := disco.GetInfoIQ(context.Background(), "", stanza.IQ{ID: "123"}, cs.Client)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(info.Features) != 1 || info.Features[0].Var != disco.NSInfo {
		t.Errorf("wrong features: want=%s, got=%v", disco.NSInfo, info.Features)
	}

	info, err = disco.GetInfoIQ(context.Background(), "node", stanza.IQ{ID: "123"}, cs.Client)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(info.Features) != 0 {
		t.Errorf("got unexpected features %v", info.Features)
	}
}

type itemHandler struct{}

func (itemHandler) HandleXMPP(xmlstream.TokenReadEncoder, *xml.StartElement) error {
	panic("should not be called")
}

func (itemHandler) ForItems(node string, f func(items.Item) error) error {
	if node != "" {
		return nil
	}

	return f(items.Item{
		Name: disco.NSItems,
	})
}

func TestItemsRoundTrip(t *testing.T) {
	m := mux.New(
		disco.Handle(),
		mux.Handle(xml.Name{}, itemHandler{}),
	)
	cs := xmpptest.NewClientServer(
		xmpptest.ServerHandler(m),
	)

	iter := disco.FetchItemsIQ(context.Background(), "", stanza.IQ{ID: "123"}, cs.Client)
	allItems := []items.Item{}
	for iter.Next() {
		allItems = append(allItems, iter.Item())
	}
	if err := iter.Err(); err != nil {
		t.Fatalf("error iterating over items: %v", err)
	}
	if len(allItems) != 1 || allItems[0].Name != disco.NSItems {
		t.Errorf("wrong items: want=%s, got=%v", disco.NSItems, allItems)
	}
	err := iter.Close()
	if err != nil {
		t.Fatalf("error closing iterator: %v", err)
	}

	iter = disco.FetchItemsIQ(context.Background(), "node", stanza.IQ{ID: "123"}, cs.Client)
	for iter.Next() {
		t.Fatalf("error, got item %v but did not expect any", iter.Item())
	}
	if err := iter.Err(); err != nil {
		t.Fatalf("error iterating over empty items: %v", err)
	}
	err = iter.Close()
	if err != nil {
		t.Fatalf("error closing empty iter: %v", err)
	}
}