roster_test.go
changeset 38 2839fece923e
parent 36 9fe022261dcc
child 39 4a06f7ccfa84
equal deleted inserted replaced
37:fbda8e925fdf 38:2839fece923e
     3 // license that can be found in the LICENSE file.
     3 // license that can be found in the LICENSE file.
     4 
     4 
     5 package xmpp
     5 package xmpp
     6 
     6 
     7 import (
     7 import (
       
     8 	"reflect"
       
     9 	"strings"
     8 	"testing"
    10 	"testing"
     9 	"xml"
    11 	"xml"
    10 )
    12 )
    11 
    13 
    12 // This is mostly just tests of the roster data structures.
    14 // This is mostly just tests of the roster data structures.
    13 
    15 
    14 func TestRosterIqMarshal(t *testing.T) {
    16 func TestRosterIqMarshal(t *testing.T) {
    15 	iq := &RosterIq{Iq: Iq{From: "from", Lang: "en"}, Query:
    17 	iq := &Iq{From: "from", Lang: "en", Nested:
    16 		RosterQuery{XMLName: xml.Name{Space: NsRoster, Local:
    18 		RosterQuery{XMLName: xml.Name{Space: NsRoster, Local:
    17 				"query"}, Item: []RosterItem{}}}
    19 				"query"}, Item: []RosterItem{}}}
    18 	var s Stanza = iq
       
    19 	if _, ok := s.(ExtendedStanza) ; !ok {
       
    20 		t.Errorf("Not an ExtendedStanza")
       
    21 	}
       
    22 	exp := `<iq from="from" xml:lang="en"><query xmlns="` +
    20 	exp := `<iq from="from" xml:lang="en"><query xmlns="` +
    23 		NsRoster + `"></query></iq>`
    21 		NsRoster + `"></query></iq>`
    24 	assertMarshal(t, exp, iq)
    22 	assertMarshal(t, exp, iq)
    25 }
    23 }
       
    24 
       
    25 func TestRosterIqUnmarshal(t *testing.T) {
       
    26 	str := `<iq from="from" xml:lang="en"><query xmlns="` +
       
    27 		NsRoster + `"><item jid="a@b.c"/></query></iq>`
       
    28 	r := strings.NewReader(str)
       
    29 	var st Stanza = &Iq{}
       
    30 	xml.Unmarshal(r, st)
       
    31 	err := parseExtended(st, newRosterQuery)
       
    32 	if err != nil {
       
    33 		t.Fatalf("parseExtended: %v", err)
       
    34 	}
       
    35 	assertEquals(t, "iq", st.XName())
       
    36 	assertEquals(t, "from", st.XFrom())
       
    37 	assertEquals(t, "en", st.XLang())
       
    38 	nested := st.XNested()
       
    39 	if nested == nil {
       
    40 		t.Fatalf("nested nil")
       
    41 	}
       
    42 	rq, ok := nested.(*RosterQuery)
       
    43 	if !ok {
       
    44 		t.Fatalf("nested not RosterQuery: %v",
       
    45 			reflect.TypeOf(nested))
       
    46 	}
       
    47 	if len(rq.Item) != 1 {
       
    48 		t.Fatalf("Wrong # items: %v", rq.Item)
       
    49 	}
       
    50 	item := rq.Item[0]
       
    51 	assertEquals(t, "a@b.c", item.Jid)
       
    52 }