author | Chris Jones <christian.jones@sri.com> |
Sun, 25 Dec 2011 18:46:13 -0700 | |
changeset 6 | 8e425e340ca1 |
child 9 | 4fe926b03827 |
permissions | -rw-r--r-- |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
1 |
// Copyright 2011 The Go Authors. All rights reserved. |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
2 |
// Use of this source code is governed by a BSD-style |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
3 |
// license that can be found in the LICENSE file. |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
4 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
5 |
package xmpp |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
6 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
7 |
import ( |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
8 |
"bytes" |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
9 |
"reflect" |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
10 |
"strings" |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
11 |
"sync" |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
12 |
"testing" |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
13 |
"xml" |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
14 |
) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
15 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
16 |
func TestReadError(t *testing.T) { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
17 |
r := strings.NewReader(`<stream:error><bad-foo/></stream:error>`) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
18 |
ch := make(chan interface{}) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
19 |
go readXml(r, ch, false) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
20 |
x := <- ch |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
21 |
se, ok := x.(*StreamError) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
22 |
if !ok { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
23 |
t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
24 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
25 |
assertEquals(t, "bad-foo", se.Any.XMLName.Local) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
26 |
assertEquals(t, "", se.Any.XMLName.Space) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
27 |
if se.Text != nil { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
28 |
t.Errorf("text not nil: %v", se.Text) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
29 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
30 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
31 |
r = strings.NewReader(`<stream:error><bad-foo/>` + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
32 |
`<text xml:lang="en" xmlns="` + nsStreams + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
33 |
`">Error text</text></stream:error>`) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
34 |
ch = make(chan interface{}) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
35 |
go readXml(r, ch, false) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
36 |
x = <- ch |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
37 |
se, ok = x.(*StreamError) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
38 |
if !ok { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
39 |
t.Fatalf("not StreamError: %v", reflect.TypeOf(x)) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
40 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
41 |
assertEquals(t, "bad-foo", se.Any.XMLName.Local) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
42 |
assertEquals(t, "", se.Any.XMLName.Space) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
43 |
assertEquals(t, "Error text", se.Text.Text) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
44 |
assertEquals(t, "en", se.Text.Lang) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
45 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
46 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
47 |
func TestReadStream(t *testing.T) { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
48 |
r := strings.NewReader(`<stream:stream to="foo.com" ` + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
49 |
`from="bar.org" id="42"` + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
50 |
`xmlns="jabber:client" xmlns:stream="` + nsStream + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
51 |
`" version="1.0">`) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
52 |
ch := make(chan interface{}) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
53 |
go readXml(r, ch, false) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
54 |
x := <- ch |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
55 |
ss, ok := x.(*Stream) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
56 |
if !ok { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
57 |
t.Fatalf("not Stream: %v", reflect.TypeOf(x)) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
58 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
59 |
assertEquals(t, "foo.com", ss.To) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
60 |
assertEquals(t, "bar.org", ss.From) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
61 |
assertEquals(t, "42", ss.Id) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
62 |
assertEquals(t, "1.0", ss.Version) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
63 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
64 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
65 |
func testWrite(obj interface{}) string { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
66 |
w := bytes.NewBuffer(nil) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
67 |
ch := make(chan interface{}) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
68 |
var wg sync.WaitGroup |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
69 |
wg.Add(1) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
70 |
go func() { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
71 |
defer wg.Done() |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
72 |
writeXml(w, ch, true) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
73 |
}() |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
74 |
ch <- obj |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
75 |
close(ch) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
76 |
wg.Wait() |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
77 |
return w.String() |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
78 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
79 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
80 |
func TestWriteError(t *testing.T) { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
81 |
se := &StreamError{Any: definedCondition{XMLName: |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
82 |
xml.Name{Local: "blah"}}} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
83 |
str := testWrite(se) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
84 |
exp := `<stream:error><blah></blah></stream:error>` |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
85 |
assertEquals(t, exp, str) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
86 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
87 |
se = &StreamError{Any: definedCondition{XMLName: |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
88 |
xml.Name{Space: nsStreams, Local: "foo"}}, |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
89 |
Text: &errText{Lang: "ru", Text: "Пошёл ты"}} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
90 |
str = testWrite(se) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
91 |
exp = `<stream:error><foo xmlns="` + nsStreams + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
92 |
`"></foo><text xmlns="` + nsStreams + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
93 |
`" xml:lang="ru">Пошёл ты</text></stream:error>` |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
94 |
assertEquals(t, exp, str) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
95 |
} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
96 |
|
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
97 |
func TestWriteStream(t *testing.T) { |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
98 |
ss := &Stream{To: "foo.org", From: "bar.com", Id: "42", Lang: |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
99 |
"en", Version: "1.0"} |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
100 |
str := testWrite(ss) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
101 |
exp := `<stream:stream xmlns="jabber:client"` + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
102 |
` xmlns:stream="` + nsStream + `" to="foo.org"` + |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
103 |
` from="bar.com" id="42" xml:lang="en" version="1.0">` |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
104 |
assertEquals(t, exp, str) |
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
105 |
} |