87 `" xml:lang="pt">things happen</text></stream:error>` |
87 `" xml:lang="pt">things happen</text></stream:error>` |
88 assertMarshal(t, exp, e) |
88 assertMarshal(t, exp, e) |
89 } |
89 } |
90 |
90 |
91 func TestIqMarshal(t *testing.T) { |
91 func TestIqMarshal(t *testing.T) { |
92 iq := &Iq{Type: "set", Id: "3", Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsBind, |
92 iq := &Iq{Stanza: Stanza{Type: "set", Id: "3", |
93 Local: "bind"}}}} |
93 Nested: []interface{}{Generic{XMLName: xml.Name{Space: NsBind, |
|
94 Local: "bind"}}}}} |
94 exp := `<iq id="3" type="set"><bind xmlns="` + NsBind + |
95 exp := `<iq id="3" type="set"><bind xmlns="` + NsBind + |
95 `"></bind></iq>` |
96 `"></bind></iq>` |
96 assertMarshal(t, exp, iq) |
97 assertMarshal(t, exp, iq) |
97 } |
|
98 |
|
99 func TestParseStanza(t *testing.T) { |
|
100 str := `<iq to="alice" from="bob" id="1" type="A"` + |
|
101 ` xml:lang="en"><foo>text</foo></iq>` |
|
102 st, err := ParseStanza(str) |
|
103 if err != nil { |
|
104 t.Fatalf("iq: %v", err) |
|
105 } |
|
106 iq, ok := st.(*Iq) |
|
107 if !ok { |
|
108 t.Fatalf("not iq: %v", st) |
|
109 } |
|
110 assertEquals(t, "iq", iq.XMLName.Local) |
|
111 assertEquals(t, "alice", iq.To) |
|
112 assertEquals(t, "bob", iq.From) |
|
113 assertEquals(t, "1", iq.Id) |
|
114 assertEquals(t, "A", iq.Type) |
|
115 assertEquals(t, "en", iq.Lang) |
|
116 if iq.Error != nil { |
|
117 t.Errorf("iq: error %v", iq.Error) |
|
118 } |
|
119 if st.innerxml() == "" { |
|
120 t.Errorf("iq: empty child") |
|
121 } |
|
122 assertEquals(t, "<foo>text</foo>", st.innerxml()) |
|
123 assertEquals(t, st.innerxml(), iq.Innerxml) |
|
124 |
|
125 str = `<message to="alice" from="bob"/>` |
|
126 st, err = ParseStanza(str) |
|
127 if err != nil { |
|
128 t.Fatalf("message: %v", err) |
|
129 } |
|
130 m, ok := st.(*Message) |
|
131 if !ok { |
|
132 t.Fatalf("not message: %v", st) |
|
133 } |
|
134 assertEquals(t, "message", m.XMLName.Local) |
|
135 assertEquals(t, "alice", m.To) |
|
136 assertEquals(t, "bob", m.From) |
|
137 assertEquals(t, "", m.Id) |
|
138 assertEquals(t, "", m.Lang) |
|
139 if m.Error != nil { |
|
140 t.Errorf("message: error %v", m.Error) |
|
141 } |
|
142 if st.innerxml() != "" { |
|
143 t.Errorf("message: child %v", st.innerxml()) |
|
144 } |
|
145 |
|
146 str = `<presence/>` |
|
147 st, err = ParseStanza(str) |
|
148 if err != nil { |
|
149 t.Fatalf("presence: %v", err) |
|
150 } |
|
151 _, ok = st.(*Presence) |
|
152 if !ok { |
|
153 t.Fatalf("not presence: %v", st) |
|
154 } |
|
155 } |
98 } |
156 |
99 |
157 func TestMarshalEscaping(t *testing.T) { |
100 func TestMarshalEscaping(t *testing.T) { |
158 msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"}, |
101 msg := &Message{Body: &Generic{XMLName: xml.Name{Local: "body"}, |
159 Chardata: `&<!-- "`}} |
102 Chardata: `&<!-- "`}} |