xmpp/roster_test.go
author Chris Jones <chris@cjones.org>
Sat, 09 Nov 2013 12:37:49 -0700
changeset 181 750bc33ccdda
parent 180 3010996c1928
permissions -rw-r--r--
Forgot to change the type of RosterItem.Jid to JID.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
package xmpp
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
import (
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
     4
	"encoding/xml"
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
     5
	"reflect"
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
	"testing"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
// This is mostly just tests of the roster data structures.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
func TestRosterIqMarshal(t *testing.T) {
111
36287f2cf06e Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents: 110
diff changeset
    12
	iq := &Iq{Header: Header{From: "from", Lang: "en",
110
7696e6a01709 Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents: 98
diff changeset
    13
		Nested: []interface{}{RosterQuery{}}}}
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
	exp := `<iq from="from" xml:lang="en"><query xmlns="` +
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    15
		NsRoster + `"></query></iq>`
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
	assertMarshal(t, exp, iq)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    17
}
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    18
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    19
func TestRosterIqUnmarshal(t *testing.T) {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    20
	str := `<iq from="from" xml:lang="en"><query xmlns="` +
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    21
		NsRoster + `"><item jid="a@b.c"/></query></iq>`
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    22
	iq := Iq{}
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    23
	xml.Unmarshal([]byte(str), &iq)
128
8342afcffc92 Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents: 126
diff changeset
    24
	m := make(map[xml.Name]reflect.Type)
8342afcffc92 Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents: 126
diff changeset
    25
	name := xml.Name{Space: NsRoster, Local: "query"}
8342afcffc92 Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents: 126
diff changeset
    26
	m[name] = reflect.TypeOf(RosterQuery{})
111
36287f2cf06e Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents: 110
diff changeset
    27
	err := parseExtended(&iq.Header, m)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    28
	if err != nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    29
		t.Fatalf("parseExtended: %v", err)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    30
	}
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    31
	assertEquals(t, "iq", iq.XMLName.Local)
180
3010996c1928 Changed the type of structure fields which hold JIDs from string to JID.
Chris Jones <chris@cjones.org>
parents: 142
diff changeset
    32
	assertEquals(t, "from", string(iq.From))
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    33
	assertEquals(t, "en", iq.Lang)
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    34
	nested := iq.Nested
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    35
	if nested == nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    36
		t.Fatalf("nested nil")
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    37
	}
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    38
	if len(nested) != 1 {
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    39
		t.Fatalf("wrong size nested(%d): %v", len(nested),
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    40
			nested)
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    41
	}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    42
	var rq *RosterQuery
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    43
	rq, ok := nested[0].(*RosterQuery)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    44
	if !ok {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    45
		t.Fatalf("nested not RosterQuery: %v",
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    46
			reflect.TypeOf(nested))
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    47
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    48
	if len(rq.Item) != 1 {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    49
		t.Fatalf("Wrong # items: %v", rq.Item)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    50
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    51
	item := rq.Item[0]
181
750bc33ccdda Forgot to change the type of RosterItem.Jid to JID.
Chris Jones <chris@cjones.org>
parents: 180
diff changeset
    52
	assertEquals(t, "a@b.c", string(item.Jid))
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    53
}