xmpp/roster_test.go
changeset 126 367e76b3028e
parent 111 36287f2cf06e
child 128 8342afcffc92
equal deleted inserted replaced
125:f464f14e39a7 126:367e76b3028e
       
     1 // Copyright 2011 The Go Authors.  All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style
       
     3 // license that can be found in the LICENSE file.
       
     4 
       
     5 package xmpp
       
     6 
       
     7 import (
       
     8 	"encoding/xml"
       
     9 	"reflect"
       
    10 	"testing"
       
    11 )
       
    12 
       
    13 // This is mostly just tests of the roster data structures.
       
    14 
       
    15 func TestRosterIqMarshal(t *testing.T) {
       
    16 	iq := &Iq{Header: Header{From: "from", Lang: "en",
       
    17 		Nested: []interface{}{RosterQuery{}}}}
       
    18 	exp := `<iq from="from" xml:lang="en"><query xmlns="` +
       
    19 		NsRoster + `"></query></iq>`
       
    20 	assertMarshal(t, exp, iq)
       
    21 }
       
    22 
       
    23 func TestRosterIqUnmarshal(t *testing.T) {
       
    24 	str := `<iq from="from" xml:lang="en"><query xmlns="` +
       
    25 		NsRoster + `"><item jid="a@b.c"/></query></iq>`
       
    26 	iq := Iq{}
       
    27 	xml.Unmarshal([]byte(str), &iq)
       
    28 	m := map[string]func(*xml.Name) interface{}{NsRoster: newRosterQuery}
       
    29 	err := parseExtended(&iq.Header, m)
       
    30 	if err != nil {
       
    31 		t.Fatalf("parseExtended: %v", err)
       
    32 	}
       
    33 	assertEquals(t, "iq", iq.XMLName.Local)
       
    34 	assertEquals(t, "from", iq.From)
       
    35 	assertEquals(t, "en", iq.Lang)
       
    36 	nested := iq.Nested
       
    37 	if nested == nil {
       
    38 		t.Fatalf("nested nil")
       
    39 	}
       
    40 	if len(nested) != 1 {
       
    41 		t.Fatalf("wrong size nested(%d): %v", len(nested),
       
    42 			nested)
       
    43 	}
       
    44 	var rq *RosterQuery
       
    45 	rq, ok := nested[0].(*RosterQuery)
       
    46 	if !ok {
       
    47 		t.Fatalf("nested not RosterQuery: %v",
       
    48 			reflect.TypeOf(nested))
       
    49 	}
       
    50 	if len(rq.Item) != 1 {
       
    51 		t.Fatalf("Wrong # items: %v", rq.Item)
       
    52 	}
       
    53 	item := rq.Item[0]
       
    54 	assertEquals(t, "a@b.c", item.Jid)
       
    55 }