author | Chris Jones <chris@cjones.org> |
Fri, 30 Dec 2011 17:27:14 -0700 | |
changeset 34 | 7b1f924c75e2 |
parent 31 | 1dc47df5c99f |
child 36 | 9fe022261dcc |
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{}) |
9
4fe926b03827
Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents:
6
diff
changeset
|
19 |
go readXml(r, ch) |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
20 |
x := <- ch |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
21 |
se, ok := x.(*streamError) |
6
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/>` + |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
32 |
`<text xml:lang="en" xmlns="` + NsStreams + |
6
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{}) |
9
4fe926b03827
Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents:
6
diff
changeset
|
35 |
go readXml(r, ch) |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
36 |
x = <- ch |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
37 |
se, ok = x.(*streamError) |
6
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"` + |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
50 |
`xmlns="jabber:client" xmlns:stream="` + NsStream + |
6
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{}) |
9
4fe926b03827
Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents:
6
diff
changeset
|
53 |
go readXml(r, ch) |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
54 |
x := <- ch |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
9
diff
changeset
|
55 |
ss, ok := x.(*stream) |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
56 |
if !ok { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
9
diff
changeset
|
57 |
t.Fatalf("not stream: %v", reflect.TypeOf(x)) |
6
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() |
9
4fe926b03827
Reorganize so we have a layered approach to IO with the server.
Chris Jones <chris@cjones.org>
parents:
6
diff
changeset
|
72 |
writeXml(w, ch) |
6
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) { |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
81 |
se := &streamError{Any: Generic{XMLName: xml.Name{Local: |
24
fff79efe06f6
Removed definedCondition in favor of Generic.
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
82 |
"blah"}}} |
6
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 |
|
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
87 |
se = &streamError{Any: Generic{XMLName: xml.Name{Space: |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
88 |
NsStreams, Local: "foo"}}, Text: |
24
fff79efe06f6
Removed definedCondition in favor of Generic.
Chris Jones <chris@cjones.org>
parents:
22
diff
changeset
|
89 |
&errText{Lang: "ru", Text: "Пошёл ты"}} |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
diff
changeset
|
90 |
str = testWrite(se) |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
91 |
exp = `<stream:error><foo xmlns="` + NsStreams + |
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
92 |
`"></foo><text xmlns="` + NsStreams + |
6
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) { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
9
diff
changeset
|
98 |
ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: |
6
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"` + |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
102 |
` xmlns:stream="` + NsStream + `" to="foo.org"` + |
6
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 |
} |