xmpp/roster_test.go
author Chris Jones <chris@cjones.org>
Wed, 16 Oct 2013 20:05:02 -0600
changeset 167 7ce61108ed86
parent 142 0ff033eed887
child 180 3010996c1928
permissions -rw-r--r--
Expose the Status.Fatal function.

package xmpp

import (
	"encoding/xml"
	"reflect"
	"testing"
)

// This is mostly just tests of the roster data structures.

func TestRosterIqMarshal(t *testing.T) {
	iq := &Iq{Header: Header{From: "from", Lang: "en",
		Nested: []interface{}{RosterQuery{}}}}
	exp := `<iq from="from" xml:lang="en"><query xmlns="` +
		NsRoster + `"></query></iq>`
	assertMarshal(t, exp, iq)
}

func TestRosterIqUnmarshal(t *testing.T) {
	str := `<iq from="from" xml:lang="en"><query xmlns="` +
		NsRoster + `"><item jid="a@b.c"/></query></iq>`
	iq := Iq{}
	xml.Unmarshal([]byte(str), &iq)
	m := make(map[xml.Name]reflect.Type)
	name := xml.Name{Space: NsRoster, Local: "query"}
	m[name] = reflect.TypeOf(RosterQuery{})
	err := parseExtended(&iq.Header, m)
	if err != nil {
		t.Fatalf("parseExtended: %v", err)
	}
	assertEquals(t, "iq", iq.XMLName.Local)
	assertEquals(t, "from", iq.From)
	assertEquals(t, "en", iq.Lang)
	nested := iq.Nested
	if nested == nil {
		t.Fatalf("nested nil")
	}
	if len(nested) != 1 {
		t.Fatalf("wrong size nested(%d): %v", len(nested),
			nested)
	}
	var rq *RosterQuery
	rq, ok := nested[0].(*RosterQuery)
	if !ok {
		t.Fatalf("nested not RosterQuery: %v",
			reflect.TypeOf(nested))
	}
	if len(rq.Item) != 1 {
		t.Fatalf("Wrong # items: %v", rq.Item)
	}
	item := rq.Item[0]
	assertEquals(t, "a@b.c", item.Jid)
}