xmpp/xmpp_test.go
changeset 183 b4bd77d58a3e
parent 163 3f891f7fe817
equal deleted inserted replaced
182:626c390682fc 183:b4bd77d58a3e
    11 
    11 
    12 func TestReadError(t *testing.T) {
    12 func TestReadError(t *testing.T) {
    13 	r := strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
    13 	r := strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
    14 		`</stream:error>`)
    14 		`</stream:error>`)
    15 	ch := make(chan interface{})
    15 	ch := make(chan interface{})
    16 	go recvXml(r, ch, make(map[xml.Name]reflect.Type))
    16 	cl := &Client{}
       
    17 	go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type))
    17 	x := <-ch
    18 	x := <-ch
    18 	se, ok := x.(*streamError)
    19 	se, ok := x.(*streamError)
    19 	if !ok {
    20 	if !ok {
    20 		t.Fatalf("not StreamError: %T", x)
    21 		t.Fatalf("not StreamError: %T", x)
    21 	}
    22 	}
    27 
    28 
    28 	r = strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
    29 	r = strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
    29 		`<text xml:lang="en" xmlns="` + NsStreams +
    30 		`<text xml:lang="en" xmlns="` + NsStreams +
    30 		`">Error text</text></stream:error>`)
    31 		`">Error text</text></stream:error>`)
    31 	ch = make(chan interface{})
    32 	ch = make(chan interface{})
    32 	go recvXml(r, ch, make(map[xml.Name]reflect.Type))
    33 	go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type))
    33 	x = <-ch
    34 	x = <-ch
    34 	se, ok = x.(*streamError)
    35 	se, ok = x.(*streamError)
    35 	if !ok {
    36 	if !ok {
    36 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    37 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    37 	}
    38 	}
    45 	r := strings.NewReader(`<stream:stream to="foo.com" ` +
    46 	r := strings.NewReader(`<stream:stream to="foo.com" ` +
    46 		`from="bar.org" id="42"` +
    47 		`from="bar.org" id="42"` +
    47 		`xmlns="` + NsClient + `" xmlns:stream="` + NsStream +
    48 		`xmlns="` + NsClient + `" xmlns:stream="` + NsStream +
    48 		`" version="1.0">`)
    49 		`" version="1.0">`)
    49 	ch := make(chan interface{})
    50 	ch := make(chan interface{})
    50 	go recvXml(r, ch, make(map[xml.Name]reflect.Type))
    51 	cl := &Client{}
       
    52 	go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type))
    51 	x := <-ch
    53 	x := <-ch
    52 	ss, ok := x.(*stream)
    54 	ss, ok := x.(*stream)
    53 	if !ok {
    55 	if !ok {
    54 		t.Fatalf("not stream: %v", reflect.TypeOf(x))
    56 		t.Fatalf("not stream: %v", reflect.TypeOf(x))
    55 	}
    57 	}
    62 func testWrite(obj interface{}) string {
    64 func testWrite(obj interface{}) string {
    63 	w := bytes.NewBuffer(nil)
    65 	w := bytes.NewBuffer(nil)
    64 	ch := make(chan interface{})
    66 	ch := make(chan interface{})
    65 	var wg sync.WaitGroup
    67 	var wg sync.WaitGroup
    66 	wg.Add(1)
    68 	wg.Add(1)
       
    69 	cl := &Client{}
    67 	go func() {
    70 	go func() {
    68 		defer wg.Done()
    71 		defer wg.Done()
    69 		sendXml(w, ch)
    72 		cl.sendXml(w, ch)
    70 	}()
    73 	}()
    71 	ch <- obj
    74 	ch <- obj
    72 	close(ch)
    75 	close(ch)
    73 	wg.Wait()
    76 	wg.Wait()
    74 	return w.String()
    77 	return w.String()