xmpp_test.go
changeset 72 53f15893a1a7
parent 56 74686b8c9146
child 74 e619e18dcec3
child 98 c9cc4eda6dce
equal deleted inserted replaced
71:578c2a83dc18 72:53f15893a1a7
    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/></stream:error>`)
    18 	ch := make(chan interface{})
    18 	ch := make(chan interface{})
    19 	go readXml(r, ch, make(map[string] func(*xml.Name) interface{}))
    19 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    20 	x := <- ch
    20 	x := <-ch
    21 	se, ok := x.(*streamError)
    21 	se, ok := x.(*streamError)
    22 	if !ok {
    22 	if !ok {
    23 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    23 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    24 	}
    24 	}
    25 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    25 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    30 
    30 
    31 	r = strings.NewReader(`<stream:error><bad-foo/>` +
    31 	r = strings.NewReader(`<stream:error><bad-foo/>` +
    32 		`<text xml:lang="en" xmlns="` + NsStreams +
    32 		`<text xml:lang="en" xmlns="` + NsStreams +
    33 		`">Error text</text></stream:error>`)
    33 		`">Error text</text></stream:error>`)
    34 	ch = make(chan interface{})
    34 	ch = make(chan interface{})
    35 	go readXml(r, ch, make(map[string] func(*xml.Name) interface{}))
    35 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    36 	x = <- ch
    36 	x = <-ch
    37 	se, ok = x.(*streamError)
    37 	se, ok = x.(*streamError)
    38 	if !ok {
    38 	if !ok {
    39 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    39 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
    40 	}
    40 	}
    41 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    41 	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
    48 	r := strings.NewReader(`<stream:stream to="foo.com" ` +
    48 	r := strings.NewReader(`<stream:stream to="foo.com" ` +
    49 		`from="bar.org" id="42"` +
    49 		`from="bar.org" id="42"` +
    50 		`xmlns="jabber:client" xmlns:stream="` + NsStream +
    50 		`xmlns="jabber:client" xmlns:stream="` + NsStream +
    51 		`" version="1.0">`)
    51 		`" version="1.0">`)
    52 	ch := make(chan interface{})
    52 	ch := make(chan interface{})
    53 	go readXml(r, ch, make(map[string] func(*xml.Name) interface{}))
    53 	go readXml(r, ch, make(map[string]func(*xml.Name) interface{}))
    54 	x := <- ch
    54 	x := <-ch
    55 	ss, ok := x.(*stream)
    55 	ss, ok := x.(*stream)
    56 	if !ok {
    56 	if !ok {
    57 		t.Fatalf("not stream: %v", reflect.TypeOf(x))
    57 		t.Fatalf("not stream: %v", reflect.TypeOf(x))
    58 	}
    58 	}
    59 	assertEquals(t, "foo.com", ss.To)
    59 	assertEquals(t, "foo.com", ss.To)
    76 	wg.Wait()
    76 	wg.Wait()
    77 	return w.String()
    77 	return w.String()
    78 }
    78 }
    79 
    79 
    80 func TestWriteError(t *testing.T) {
    80 func TestWriteError(t *testing.T) {
    81 	se := &streamError{Any: Generic{XMLName: xml.Name{Local:
    81 	se := &streamError{Any: Generic{XMLName: xml.Name{Local: "blah"}}}
    82 				"blah"}}}
       
    83 	str := testWrite(se)
    82 	str := testWrite(se)
    84 	exp := `<stream:error><blah></blah></stream:error>`
    83 	exp := `<stream:error><blah></blah></stream:error>`
    85 	assertEquals(t, exp, str)
    84 	assertEquals(t, exp, str)
    86 
    85 
    87 	se = &streamError{Any: Generic{XMLName: xml.Name{Space:
    86 	se = &streamError{Any: Generic{XMLName: xml.Name{Space: NsStreams, Local: "foo"}}, Text: &errText{Lang: "ru", Text: "Пошёл ты"}}
    88 				NsStreams, Local: "foo"}}, Text:
       
    89 		&errText{Lang: "ru", Text: "Пошёл ты"}}
       
    90 	str = testWrite(se)
    87 	str = testWrite(se)
    91 	exp = `<stream:error><foo xmlns="` + NsStreams +
    88 	exp = `<stream:error><foo xmlns="` + NsStreams +
    92 		`"></foo><text xmlns="` + NsStreams +
    89 		`"></foo><text xmlns="` + NsStreams +
    93 		`" xml:lang="ru">Пошёл ты</text></stream:error>`
    90 		`" xml:lang="ru">Пошёл ты</text></stream:error>`
    94 	assertEquals(t, exp, str)
    91 	assertEquals(t, exp, str)
    95 }
    92 }
    96 
    93 
    97 func TestWriteStream(t *testing.T) {
    94 func TestWriteStream(t *testing.T) {
    98 	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang:
    95 	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: "en", Version: "1.0"}
    99 		"en", Version: "1.0"}
       
   100 	str := testWrite(ss)
    96 	str := testWrite(ss)
   101 	exp := `<stream:stream xmlns="jabber:client"` +
    97 	exp := `<stream:stream xmlns="jabber:client"` +
   102 		` xmlns:stream="` + NsStream + `" to="foo.org"` +
    98 		` xmlns:stream="` + NsStream + `" to="foo.org"` +
   103 		` from="bar.com" id="42" xml:lang="en" version="1.0">`
    99 		` from="bar.com" id="42" xml:lang="en" version="1.0">`
   104 	assertEquals(t, exp, str)
   100 	assertEquals(t, exp, str)