|
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 "bytes" |
|
9 "reflect" |
|
10 "strings" |
|
11 "sync" |
|
12 "testing" |
|
13 "xml" |
|
14 ) |
|
15 |
|
16 func TestReadError(t *testing.T) { |
|
17 r := strings.NewReader(`<stream:error><bad-foo/></stream:error>`) |
|
18 ch := make(chan interface{}) |
|
19 go readXml(r, ch, false) |
|
20 x := <- ch |
|
21 se, ok := x.(*StreamError) |
|
22 if !ok { |
|
23 t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) |
|
24 } |
|
25 assertEquals(t, "bad-foo", se.Any.XMLName.Local) |
|
26 assertEquals(t, "", se.Any.XMLName.Space) |
|
27 if se.Text != nil { |
|
28 t.Errorf("text not nil: %v", se.Text) |
|
29 } |
|
30 |
|
31 r = strings.NewReader(`<stream:error><bad-foo/>` + |
|
32 `<text xml:lang="en" xmlns="` + nsStreams + |
|
33 `">Error text</text></stream:error>`) |
|
34 ch = make(chan interface{}) |
|
35 go readXml(r, ch, false) |
|
36 x = <- ch |
|
37 se, ok = x.(*StreamError) |
|
38 if !ok { |
|
39 t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) |
|
40 } |
|
41 assertEquals(t, "bad-foo", se.Any.XMLName.Local) |
|
42 assertEquals(t, "", se.Any.XMLName.Space) |
|
43 assertEquals(t, "Error text", se.Text.Text) |
|
44 assertEquals(t, "en", se.Text.Lang) |
|
45 } |
|
46 |
|
47 func TestReadStream(t *testing.T) { |
|
48 r := strings.NewReader(`<stream:stream to="foo.com" ` + |
|
49 `from="bar.org" id="42"` + |
|
50 `xmlns="jabber:client" xmlns:stream="` + nsStream + |
|
51 `" version="1.0">`) |
|
52 ch := make(chan interface{}) |
|
53 go readXml(r, ch, false) |
|
54 x := <- ch |
|
55 ss, ok := x.(*Stream) |
|
56 if !ok { |
|
57 t.Fatalf("not Stream: %v", reflect.TypeOf(x)) |
|
58 } |
|
59 assertEquals(t, "foo.com", ss.To) |
|
60 assertEquals(t, "bar.org", ss.From) |
|
61 assertEquals(t, "42", ss.Id) |
|
62 assertEquals(t, "1.0", ss.Version) |
|
63 } |
|
64 |
|
65 func testWrite(obj interface{}) string { |
|
66 w := bytes.NewBuffer(nil) |
|
67 ch := make(chan interface{}) |
|
68 var wg sync.WaitGroup |
|
69 wg.Add(1) |
|
70 go func() { |
|
71 defer wg.Done() |
|
72 writeXml(w, ch, true) |
|
73 }() |
|
74 ch <- obj |
|
75 close(ch) |
|
76 wg.Wait() |
|
77 return w.String() |
|
78 } |
|
79 |
|
80 func TestWriteError(t *testing.T) { |
|
81 se := &StreamError{Any: definedCondition{XMLName: |
|
82 xml.Name{Local: "blah"}}} |
|
83 str := testWrite(se) |
|
84 exp := `<stream:error><blah></blah></stream:error>` |
|
85 assertEquals(t, exp, str) |
|
86 |
|
87 se = &StreamError{Any: definedCondition{XMLName: |
|
88 xml.Name{Space: nsStreams, Local: "foo"}}, |
|
89 Text: &errText{Lang: "ru", Text: "Пошёл ты"}} |
|
90 str = testWrite(se) |
|
91 exp = `<stream:error><foo xmlns="` + nsStreams + |
|
92 `"></foo><text xmlns="` + nsStreams + |
|
93 `" xml:lang="ru">Пошёл ты</text></stream:error>` |
|
94 assertEquals(t, exp, str) |
|
95 } |
|
96 |
|
97 func TestWriteStream(t *testing.T) { |
|
98 ss := &Stream{To: "foo.org", From: "bar.com", Id: "42", Lang: |
|
99 "en", Version: "1.0"} |
|
100 str := testWrite(ss) |
|
101 exp := `<stream:stream xmlns="jabber:client"` + |
|
102 ` xmlns:stream="` + nsStream + `" to="foo.org"` + |
|
103 ` from="bar.com" id="42" xml:lang="en" version="1.0">` |
|
104 assertEquals(t, exp, str) |
|
105 } |