author | Chris Jones <chris@cjones.org> |
Wed, 16 Oct 2013 20:29:29 -0600 | |
changeset 168 | f4ccc2f29777 |
parent 142 | 0ff033eed887 |
child 180 | 3010996c1928 |
permissions | -rw-r--r-- |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
1 |
package xmpp |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
2 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
3 |
import ( |
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
4 |
"encoding/xml" |
38 | 5 |
"reflect" |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
6 |
"testing" |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
7 |
) |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
8 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
9 |
// This is mostly just tests of the roster data structures. |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
10 |
|
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
11 |
func TestRosterIqMarshal(t *testing.T) { |
111
36287f2cf06e
Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
110
diff
changeset
|
12 |
iq := &Iq{Header: Header{From: "from", Lang: "en", |
110
7696e6a01709
Instead of making Stanza an interface that Iq, Message, and Presence implement, change it to an embedded struct.
Chris Jones <chris@cjones.org>
parents:
98
diff
changeset
|
13 |
Nested: []interface{}{RosterQuery{}}}} |
36
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
14 |
exp := `<iq from="from" xml:lang="en"><query xmlns="` + |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
15 |
NsRoster + `"></query></iq>` |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
16 |
assertMarshal(t, exp, iq) |
9fe022261dcc
Added a capability to use extensions. There are still some bugs with
Chris Jones <chris@cjones.org>
parents:
diff
changeset
|
17 |
} |
38 | 18 |
|
19 |
func TestRosterIqUnmarshal(t *testing.T) { |
|
20 |
str := `<iq from="from" xml:lang="en"><query xmlns="` + |
|
21 |
NsRoster + `"><item jid="a@b.c"/></query></iq>` |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
22 |
iq := Iq{} |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
23 |
xml.Unmarshal([]byte(str), &iq) |
128
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
24 |
m := make(map[xml.Name]reflect.Type) |
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
25 |
name := xml.Name{Space: NsRoster, Local: "query"} |
8342afcffc92
Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents:
126
diff
changeset
|
26 |
m[name] = reflect.TypeOf(RosterQuery{}) |
111
36287f2cf06e
Step 1 of moving to interface Stanza and embedded struct Header.
Chris Jones <chris@cjones.org>
parents:
110
diff
changeset
|
27 |
err := parseExtended(&iq.Header, m) |
38 | 28 |
if err != nil { |
29 |
t.Fatalf("parseExtended: %v", err) |
|
30 |
} |
|
98
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
31 |
assertEquals(t, "iq", iq.XMLName.Local) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
32 |
assertEquals(t, "from", iq.From) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
33 |
assertEquals(t, "en", iq.Lang) |
c9cc4eda6dce
Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents:
72
diff
changeset
|
34 |
nested := iq.Nested |
38 | 35 |
if nested == nil { |
36 |
t.Fatalf("nested nil") |
|
37 |
} |
|
61
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
38 |
if len(nested) != 1 { |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
39 |
t.Fatalf("wrong size nested(%d): %v", len(nested), |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
40 |
nested) |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
41 |
} |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
42 |
var rq *RosterQuery |
16513974d273
Stanzas can now contain multiple nested (extended) elements.
Chris Jones <chris@cjones.org>
parents:
42
diff
changeset
|
43 |
rq, ok := nested[0].(*RosterQuery) |
38 | 44 |
if !ok { |
45 |
t.Fatalf("nested not RosterQuery: %v", |
|
46 |
reflect.TypeOf(nested)) |
|
47 |
} |
|
48 |
if len(rq.Item) != 1 { |
|
49 |
t.Fatalf("Wrong # items: %v", rq.Item) |
|
50 |
} |
|
51 |
item := rq.Item[0] |
|
52 |
assertEquals(t, "a@b.c", item.Jid) |
|
53 |
} |