author | Chris Jones <christian.jones@sri.com> |
Thu, 19 Jan 2012 22:57:36 -0700 | |
changeset 84 | 25c4296a3524 |
parent 75 | 03a923eb5c01 |
child 93 | fbd51fa6b7ea |
permissions | -rw-r--r-- |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
1 |
// Copyright 2011 The Go Authors. All rights reserved. |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
2 |
// Use of this source code is governed by a BSD-style |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
3 |
// license that can be found in the LICENSE file. |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
4 |
|
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
5 |
package xmpp |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
6 |
|
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
7 |
import ( |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
8 |
"bytes" |
74
e619e18dcec3
Ran gofix from weekly-2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
72
diff
changeset
|
9 |
"encoding/xml" |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
10 |
"testing" |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
11 |
) |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
12 |
|
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
13 |
func assertEquals(t *testing.T, expected, observed string) { |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
14 |
if expected != observed { |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
15 |
t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected, |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
16 |
observed) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
17 |
} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
18 |
} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
19 |
|
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
20 |
func TestJid(t *testing.T) { |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
21 |
str := "user@domain/res" |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
22 |
jid := &JID{} |
75
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
23 |
if err := jid.Set(str); err != nil { |
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
24 |
t.Errorf("Set(%s) failed: %s", str, err) |
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
25 |
} |
25
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
26 |
assertEquals(t, "user", jid.Node) |
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
27 |
assertEquals(t, "domain", jid.Domain) |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
28 |
assertEquals(t, "res", jid.Resource) |
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
29 |
assertEquals(t, str, jid.String()) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
30 |
|
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
31 |
str = "domain.tld" |
75
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
32 |
if err := jid.Set(str); err != nil { |
03a923eb5c01
Updated for weekly.2012-01-15.
Chris Jones <christian.jones@sri.com>
parents:
74
diff
changeset
|
33 |
t.Errorf("Set(%s) failed: %s", str, err) |
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
34 |
} |
25
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
35 |
if jid.Node != "" { |
7437d6eed227
Made JID.Node a string rather than *string. This is more appropriate
Chris Jones <chris@cjones.org>
parents:
24
diff
changeset
|
36 |
t.Errorf("Node: %v\n", jid.Node) |
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
37 |
} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
38 |
assertEquals(t, "domain.tld", jid.Domain) |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
39 |
if jid.Resource != "" { |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
40 |
t.Errorf("Resource: %v\n", jid.Resource) |
3
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
41 |
} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
42 |
assertEquals(t, str, jid.String()) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
43 |
} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
44 |
|
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
45 |
func assertMarshal(t *testing.T, expected string, marshal interface{}) { |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
46 |
buf := bytes.NewBuffer(nil) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
47 |
xml.Marshal(buf, marshal) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
48 |
observed := string(buf.Bytes()) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
49 |
assertEquals(t, expected, observed) |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
50 |
} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
51 |
|
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
52 |
func TestStreamMarshal(t *testing.T) { |
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
53 |
s := &stream{To: "bob"} |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
3
diff
changeset
|
54 |
exp := `<stream:stream xmlns="jabber:client"` + |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
55 |
` xmlns:stream="` + NsStream + `" to="bob">` |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
56 |
assertMarshal(t, exp, s) |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
57 |
|
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
58 |
s = &stream{To: "bob", From: "alice", Id: "#3", Version: "5.3"} |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
3
diff
changeset
|
59 |
exp = `<stream:stream xmlns="jabber:client"` + |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
60 |
` xmlns:stream="` + NsStream + `" to="bob" from="alice"` + |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
3
diff
changeset
|
61 |
` id="#3" version="5.3">` |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
62 |
assertMarshal(t, exp, s) |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
63 |
|
22
d6b7b4cbf50d
Made the stream type non-public.
Chris Jones <chris@cjones.org>
parents:
21
diff
changeset
|
64 |
s = &stream{Lang: "en_US"} |
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
3
diff
changeset
|
65 |
exp = `<stream:stream xmlns="jabber:client"` + |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
66 |
` xmlns:stream="` + NsStream + `" xml:lang="en_US">` |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
67 |
assertMarshal(t, exp, s) |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
68 |
} |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
69 |
|
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
70 |
func TestStreamErrorMarshal(t *testing.T) { |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
71 |
name := xml.Name{Space: NsStreams, Local: "ack"} |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
72 |
e := &streamError{Any: Generic{XMLName: name}} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
73 |
exp := `<stream:error><ack xmlns="` + NsStreams + |
72 | 74 |
`"></ack></stream:error>` |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
75 |
assertMarshal(t, exp, e) |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
76 |
|
6
8e425e340ca1
Implemented writing to the remote. Now we have bidirectional communication.
Chris Jones <christian.jones@sri.com>
parents:
3
diff
changeset
|
77 |
txt := errText{Lang: "pt", Text: "things happen"} |
31
1dc47df5c99f
Made streamError non-public, and made a first attempt at a stream
Chris Jones <chris@cjones.org>
parents:
26
diff
changeset
|
78 |
e = &streamError{Any: Generic{XMLName: name}, Text: &txt} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
79 |
exp = `<stream:error><ack xmlns="` + NsStreams + |
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
80 |
`"></ack><text xmlns="` + NsStreams + |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
81 |
`" xml:lang="pt">things happen</text></stream:error>` |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
82 |
assertMarshal(t, exp, e) |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
83 |
} |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
84 |
|
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
85 |
func TestIqMarshal(t *testing.T) { |
72 | 86 |
iq := &Iq{Type: "set", Id: "3", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsBind, |
87 |
Local: "bind"}}}} |
|
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
88 |
exp := `<iq id="3" type="set"><bind xmlns="` + NsBind + |
12
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
89 |
`"></bind></iq>` |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
90 |
assertMarshal(t, exp, iq) |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
91 |
} |
26 | 92 |
|
93 |
func TestParseStanza(t *testing.T) { |
|
94 |
str := `<iq to="alice" from="bob" id="1" type="A"` + |
|
95 |
` xml:lang="en"><foo>text</foo></iq>` |
|
96 |
st, err := ParseStanza(str) |
|
97 |
if err != nil { |
|
98 |
t.Fatalf("iq: %v", err) |
|
99 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
100 |
assertEquals(t, "iq", st.GetName()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
101 |
assertEquals(t, "alice", st.GetTo()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
102 |
assertEquals(t, "bob", st.GetFrom()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
103 |
assertEquals(t, "1", st.GetId()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
104 |
assertEquals(t, "A", st.GetType()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
105 |
assertEquals(t, "en", st.GetLang()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
106 |
if st.GetError() != nil { |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
107 |
t.Errorf("iq: error %v", st.GetError()) |
26 | 108 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
109 |
if st.innerxml() == "" { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
110 |
t.Errorf("iq: empty child") |
26 | 111 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
112 |
assertEquals(t, "<foo>text</foo>", st.innerxml()) |
26 | 113 |
|
114 |
str = `<message to="alice" from="bob"/>` |
|
115 |
st, err = ParseStanza(str) |
|
116 |
if err != nil { |
|
117 |
t.Fatalf("message: %v", err) |
|
118 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
119 |
assertEquals(t, "message", st.GetName()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
120 |
assertEquals(t, "alice", st.GetTo()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
121 |
assertEquals(t, "bob", st.GetFrom()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
122 |
assertEquals(t, "", st.GetId()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
123 |
assertEquals(t, "", st.GetLang()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
124 |
if st.GetError() != nil { |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
125 |
t.Errorf("message: error %v", st.GetError()) |
26 | 126 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
127 |
if st.innerxml() != "" { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
128 |
t.Errorf("message: child %v", st.innerxml()) |
26 | 129 |
} |
130 |
||
131 |
str = `<presence/>` |
|
132 |
st, err = ParseStanza(str) |
|
133 |
if err != nil { |
|
134 |
t.Fatalf("presence: %v", err) |
|
135 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
136 |
assertEquals(t, "presence", st.GetName()) |
26 | 137 |
} |
71
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
138 |
|
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
139 |
func TestMarshalEscaping(t *testing.T) { |
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
140 |
msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"}, |
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
141 |
Chardata: `&<!-- "`}} |
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
142 |
exp := `<message><body>&<!-- "</body></message>` |
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
143 |
assertMarshal(t, exp, msg) |
578c2a83dc18
Added a quick test of XML escaping.
Chris Jones <chris@cjones.org>
parents:
61
diff
changeset
|
144 |
} |