roster_test.go
author Chris Jones <chris@cjones.org>
Sun, 08 Jan 2012 13:04:50 -0700
branch20120108-close
changeset 70 bb629d22148a
parent 61 16513974d273
child 72 53f15893a1a7
permissions -rw-r--r--
Closing this branch.
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
// Copyright 2011 The Go Authors.  All rights reserved.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
package xmpp
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
import (
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
     8
	"reflect"
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
     9
	"strings"
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
	"testing"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
	"xml"
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
)
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    13
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    14
// 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
    15
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
func TestRosterIqMarshal(t *testing.T) {
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    17
	iq := &Iq{From: "from", Lang: "en", Nested:
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    18
		[]interface{}{RosterQuery{}}}
36
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
	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
    20
		NsRoster + `"></query></iq>`
9fe022261dcc Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
	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
    22
}
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    23
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    24
func TestRosterIqUnmarshal(t *testing.T) {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    25
	str := `<iq from="from" xml:lang="en"><query xmlns="` +
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    26
		NsRoster + `"><item jid="a@b.c"/></query></iq>`
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    27
	r := strings.NewReader(str)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    28
	var st Stanza = &Iq{}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    29
	xml.Unmarshal(r, st)
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    30
	m := map[string] func(*xml.Name) interface{}{NsRoster: newRosterQuery}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    31
	err := parseExtended(st, m)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    32
	if err != nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    33
		t.Fatalf("parseExtended: %v", err)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    34
	}
42
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 39
diff changeset
    35
	assertEquals(t, "iq", st.GetName())
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 39
diff changeset
    36
	assertEquals(t, "from", st.GetFrom())
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 39
diff changeset
    37
	assertEquals(t, "en", st.GetLang())
f6bb47ca12f2 Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents: 39
diff changeset
    38
	nested := st.GetNested()
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    39
	if nested == nil {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    40
		t.Fatalf("nested nil")
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    41
	}
61
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    42
	if len(nested) != 1 {
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    43
		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
    44
			nested)
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    45
	}
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    46
	var rq *RosterQuery
16513974d273 Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents: 42
diff changeset
    47
	rq, ok := nested[0].(*RosterQuery)
38
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    48
	if !ok {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    49
		t.Fatalf("nested not RosterQuery: %v",
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    50
			reflect.TypeOf(nested))
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    51
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    52
	if len(rq.Item) != 1 {
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    53
		t.Fatalf("Wrong # items: %v", rq.Item)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    54
	}
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    55
	item := rq.Item[0]
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    56
	assertEquals(t, "a@b.c", item.Jid)
2839fece923e Extended stanzas work now.
Chris Jones <chris@cjones.org>
parents: 36
diff changeset
    57
}