~samwhited/xmpp

ref: 8748f2b28992fe92b1684ad680058a63f1934fcd xmpp/stream/stream_test.go -rw-r--r-- 1.8 KiB
8748f2b2Sam Whited Add stream tests and namespace validation 5 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
// 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 stream

import (
	"encoding/xml"
	"testing"
)

var (
	validAttrs = []xml.Attr{
		{xml.Name{"", "id"}, "1234"},
		{xml.Name{"", "version"}, "1.0"},
		{xml.Name{"", "to"}, "shakespeare.lit"},
		{xml.Name{"", "from"}, "prospero@shakespeare.lit"},
		{xml.Name{"xmlns", "stream"}, "http://etherx.jabber.org/streams"},
		{xml.Name{"xml", "lang"}, "en"},
		{xml.Name{"", "xmlns"}, "jabber:client"},
	}
	validName = xml.Name{"stream", "stream"}
)

// FromStartElement should validate attributes.
func TestStreamFromStartElement(t *testing.T) {
	var data = []struct {
		start       xml.StartElement
		shouldError bool
	}{
		{xml.StartElement{validName, validAttrs}, false},
		{xml.StartElement{xml.Name{"stream", "wrong"}, validAttrs}, true},
		{xml.StartElement{xml.Name{"wrong", "stream"}, validAttrs}, true},
		{xml.StartElement{validName, []xml.Attr{
			{xml.Name{"", "id"}, "1234"},
			{xml.Name{"", "version"}, "1.0"},
			{xml.Name{"", "to"}, "shakespeare.lit"},
			{xml.Name{"", "from"}, "prospero@shakespeare.lit"},
			{xml.Name{"xmlns", "stream"}, "http://etherx.jabber.org/streams"},
			{xml.Name{"xml", "lang"}, "en"},
			{xml.Name{"", "xmlns"}, "jabber:wrong"},
		}}, true},
		{xml.StartElement{validName, []xml.Attr{
			{xml.Name{"", "id"}, "1234"},
			{xml.Name{"", "version"}, "1.0"},
			{xml.Name{"", "to"}, "shakespeare.lit"},
			{xml.Name{"", "from"}, "prospero@shakespeare.lit"},
			{xml.Name{"xmlns", "stream"}, "urn:jabber:wrong"},
			{xml.Name{"xml", "lang"}, "en"},
			{xml.Name{"", "xmlns"}, "jabber:client"},
		}}, true},
	}

	for _, d := range data {
		if _, err := FromStartElement(d.start); (err != nil) != d.shouldError {
			t.Log(err)
			t.Fail()
		}
	}
}