~samwhited/xmpp

ref: c71a17569b03f07e0fc407f1da2094c1796cec90 xmpp/bind_test.go -rw-r--r-- 1.9 KiB
c71a1756Sam Whited ibr2: Add API stability disclaimer to docs 4 years 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
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

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

	"mellium.im/xmpp/ns"
)

func TestBindList(t *testing.T) {
	buf := &bytes.Buffer{}
	bind := BindResource()
	e := xml.NewEncoder(buf)
	start := xml.StartElement{Name: xml.Name{Space: ns.Bind, Local: "bind"}}
	req, err := bind.List(context.Background(), e, start)
	if err != nil {
		t.Fatal(err)
	}
	if !req {
		t.Error("Bind must always be required")
	}
	if buf.String() != `<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"></bind>` {
		t.Errorf("Got unexpected value for bind listing: `%s`", buf.String())
	}
}

func TestBindParse(t *testing.T) {
	bind := BindResource()
	for _, test := range []struct {
		XML string
		err bool
	}{
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>`, false},
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind>`, false},
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>STUFF</bind>`, false},
		{`<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><test/></bind>`, false},
		{`<notbind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></notbind>`, true},
		{`<bind xmlns='notbindns'></bind>`, true},
	} {
		// Run each test twice, once without a requested resource and once for a
		// requested resource (which should be ignored, making the results
		// identical).
		d := xml.NewDecoder(strings.NewReader(test.XML))
		tok, err := d.Token()
		if err != nil {
			// We screwed up the test string…
			panic(err)
		}
		start := tok.(xml.StartElement)
		req, data, err := bind.Parse(context.Background(), d, &start)
		switch {
		case test.err && err == nil:
			t.Error("Expected error from parse")
			continue
		case !test.err && err != nil:
			t.Error(err)
			continue
		}
		if !req {
			t.Error("Expected parsed bind feature to be required")
		}
		if data != nil {
			t.Error("Expected bind data to be nil")
		}
	}
}