author | Chris Jones <chris@cjones.org> |
Sun, 08 Jan 2012 13:04:50 -0700 | |
branch | 20120108-close |
changeset 70 | bb629d22148a |
parent 61 | 16513974d273 |
child 71 | 578c2a83dc18 |
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" |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
9 |
"testing" |
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
10 |
"xml" |
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{} |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
23 |
if !jid.Set(str) { |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
24 |
t.Errorf("Set(%s) failed\n", str) |
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" |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
32 |
if !jid.Set(str) { |
6121aa2f21b1
Made JID implement flag.Value.
Chris Jones <chris@cjones.org>
parents:
2
diff
changeset
|
33 |
t.Errorf("Set(%s) failed\n", str) |
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 + |
2
4dabfef08c8c
Forgot to add the new xmpp.go from my last commit. Also added some
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
74 |
`"></ack></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
|
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) { |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
86 |
iq := &Iq{Type: "set", Id: "3", Nested: |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
87 |
[]interface{}{Generic{XMLName: xml.Name{Space: NsBind, |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
88 |
Local: "bind"}}}} |
34
7b1f924c75e2
Made the namespace constants public.
Chris Jones <chris@cjones.org>
parents:
31
diff
changeset
|
89 |
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
|
90 |
`"></bind></iq>` |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
91 |
assertMarshal(t, exp, iq) |
122ab6208c3c
Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents:
7
diff
changeset
|
92 |
} |
26 | 93 |
|
94 |
func TestParseStanza(t *testing.T) { |
|
95 |
str := `<iq to="alice" from="bob" id="1" type="A"` + |
|
96 |
` xml:lang="en"><foo>text</foo></iq>` |
|
97 |
st, err := ParseStanza(str) |
|
98 |
if err != nil { |
|
99 |
t.Fatalf("iq: %v", err) |
|
100 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
101 |
assertEquals(t, "iq", st.GetName()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
102 |
assertEquals(t, "alice", st.GetTo()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
103 |
assertEquals(t, "bob", st.GetFrom()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
104 |
assertEquals(t, "1", st.GetId()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
105 |
assertEquals(t, "A", st.GetType()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
106 |
assertEquals(t, "en", st.GetLang()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
107 |
if st.GetError() != nil { |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
108 |
t.Errorf("iq: error %v", st.GetError()) |
26 | 109 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
110 |
if st.innerxml() == "" { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
111 |
t.Errorf("iq: empty child") |
26 | 112 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
113 |
assertEquals(t, "<foo>text</foo>", st.innerxml()) |
26 | 114 |
|
115 |
str = `<message to="alice" from="bob"/>` |
|
116 |
st, err = ParseStanza(str) |
|
117 |
if err != nil { |
|
118 |
t.Fatalf("message: %v", err) |
|
119 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
120 |
assertEquals(t, "message", st.GetName()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
121 |
assertEquals(t, "alice", st.GetTo()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
122 |
assertEquals(t, "bob", st.GetFrom()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
123 |
assertEquals(t, "", st.GetId()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
124 |
assertEquals(t, "", st.GetLang()) |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
125 |
if st.GetError() != nil { |
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
126 |
t.Errorf("message: error %v", st.GetError()) |
26 | 127 |
} |
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
128 |
if st.innerxml() != "" { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
129 |
t.Errorf("message: child %v", st.innerxml()) |
26 | 130 |
} |
131 |
||
132 |
str = `<presence/>` |
|
133 |
st, err = ParseStanza(str) |
|
134 |
if err != nil { |
|
135 |
t.Fatalf("presence: %v", err) |
|
136 |
} |
|
42
f6bb47ca12f2
Renamed the somewhat obscure XTo(), etc. to GetTo(), etc.
Chris Jones <chris@cjones.org>
parents:
38
diff
changeset
|
137 |
assertEquals(t, "presence", st.GetName()) |
26 | 138 |
} |