xmpp_test.go
changeset 98 c9cc4eda6dce
parent 72 53f15893a1a7
child 114 a058e33c1666
equal deleted inserted replaced
88:d2ec96c80efe 98:c9cc4eda6dce
     4 
     4 
     5 package xmpp
     5 package xmpp
     6 
     6 
     7 import (
     7 import (
     8 	"bytes"
     8 	"bytes"
       
     9 	"encoding/xml"
     9 	"reflect"
    10 	"reflect"
    10 	"strings"
    11 	"strings"
    11 	"sync"
    12 	"sync"
    12 	"testing"
    13 	"testing"
    13 	"xml"
       
    14 )
    14 )
    15 
    15 
    16 func TestReadError(t *testing.T) {
    16 func TestReadError(t *testing.T) {
    17 	r := strings.NewReader(`<stream:error><bad-foo/></stream:error>`)
    17 	r := strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
       
    18 		`</stream:error>`)
    18 	ch := make(chan interface{})
    19 	ch := make(chan interface{})
    19 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    20 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    20 	x := <-ch
    21 	x := <-ch
    21 	se, ok := x.(*streamError)
    22 	se, ok := x.(*streamError)
    22 	if !ok {
    23 	if !ok {
    23 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    24 		t.Fatalf("not StreamError: %T", x)
    24 	}
    25 	}
    25 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    26 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    26 	assertEquals(t, "", se.Any.XMLName.Space)
    27 	assertEquals(t, "blah", se.Any.XMLName.Space)
    27 	if se.Text != nil {
    28 	if se.Text != nil {
    28 		t.Errorf("text not nil: %v", se.Text)
    29 		t.Errorf("text not nil: %v", se.Text)
    29 	}
    30 	}
    30 
    31 
    31 	r = strings.NewReader(`<stream:error><bad-foo/>` +
    32 	r = strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
    32 		`<text xml:lang="en" xmlns="` + NsStreams +
    33 		`<text xml:lang="en" xmlns="` + NsStreams +
    33 		`">Error text</text></stream:error>`)
    34 		`">Error text</text></stream:error>`)
    34 	ch = make(chan interface{})
    35 	ch = make(chan interface{})
    35 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    36 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    36 	x = <-ch
    37 	x = <-ch
    37 	se, ok = x.(*streamError)
    38 	se, ok = x.(*streamError)
    38 	if !ok {
    39 	if !ok {
    39 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    40 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    40 	}
    41 	}
    41 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    42 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    42 	assertEquals(t, "", se.Any.XMLName.Space)
    43 	assertEquals(t, "blah", se.Any.XMLName.Space)
    43 	assertEquals(t, "Error text", se.Text.Text)
    44 	assertEquals(t, "Error text", se.Text.Text)
    44 	assertEquals(t, "en", se.Text.Lang)
    45 	assertEquals(t, "en", se.Text.Lang)
    45 }
    46 }
    46 
    47 
    47 func TestReadStream(t *testing.T) {
    48 func TestReadStream(t *testing.T) {
    48 	r := strings.NewReader(`<stream:stream to="foo.com" ` +
    49 	r := strings.NewReader(`<stream:stream to="foo.com" ` +
    49 		`from="bar.org" id="42"` +
    50 		`from="bar.org" id="42"` +
    50 		`xmlns="jabber:client" xmlns:stream="` + NsStream +
    51 		`xmlns="` + NsClient + `" xmlns:stream="` + NsStream +
    51 		`" version="1.0">`)
    52 		`" version="1.0">`)
    52 	ch := make(chan interface{})
    53 	ch := make(chan interface{})
    53 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    54 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    54 	x := <-ch
    55 	x := <-ch
    55 	ss, ok := x.(*stream)
    56 	ss, ok := x.(*stream)
    92 }
    93 }
    93 
    94 
    94 func TestWriteStream(t *testing.T) {
    95 func TestWriteStream(t *testing.T) {
    95 	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: "en", Version: "1.0"}
    96 	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: "en", Version: "1.0"}
    96 	str := testWrite(ss)
    97 	str := testWrite(ss)
    97 	exp := `<stream:stream xmlns="jabber:client"` +
    98 	exp := `<stream:stream xmlns="` + NsClient +
    98 		` xmlns:stream="` + NsStream + `" to="foo.org"` +
    99 		`" xmlns:stream="` + NsStream + `" to="foo.org"` +
    99 		` from="bar.com" id="42" xml:lang="en" version="1.0">`
   100 		` from="bar.com" id="42" xml:lang="en" version="1.0">`
   100 	assertEquals(t, exp, str)
   101 	assertEquals(t, exp, str)
   101 }
   102 }