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