parent
005c8823d9
commit
3b66e31888
31
stanza/error_test.go
Normal file
31
stanza/error_test.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestErr_UnmarshalXML(t *testing.T) {
|
||||
packet := `
|
||||
<iq from='pubsub.example.com'
|
||||
id='kj4vz31m'
|
||||
to='romeo@example.net/foo'
|
||||
type='error'>
|
||||
<error type='wait'>
|
||||
<resource-constraint
|
||||
xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
||||
<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>System overloaded, please retry</text>
|
||||
</error>
|
||||
</iq>`
|
||||
|
||||
parsedIQ := IQ{}
|
||||
data := []byte(packet)
|
||||
if err := xml.Unmarshal(data, &parsedIQ); err != nil {
|
||||
t.Errorf("Unmarshal(%s) returned error", data)
|
||||
}
|
||||
|
||||
xmppError := parsedIQ.Error
|
||||
if xmppError.Text != "System overloaded, please retry" {
|
||||
t.Errorf("Could not extract error text: '%s'", xmppError.Text)
|
||||
}
|
||||
}
|
|
@ -21,6 +21,6 @@ func TestControlSet(t *testing.T) {
|
|||
}
|
||||
|
||||
if cs, ok := parsedIQ.Payload.(*ControlSet); !ok {
|
||||
t.Errorf("Paylod is not an iot control set: %v", cs)
|
||||
t.Errorf("Payload is not an iot control set: %v", cs)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ type IQ struct { // Info/Query
|
|||
// request."
|
||||
Payload IQPayload `xml:",omitempty"`
|
||||
Error Err `xml:"error,omitempty"`
|
||||
// Any is used to decode unknown payload as a generique structure
|
||||
// Any is used to decode unknown payload as a generic structure
|
||||
Any *Node `xml:",any"`
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import "encoding/xml"
|
|||
type Node struct {
|
||||
XMLName xml.Name
|
||||
Attrs []xml.Attr `xml:"-"`
|
||||
Content string `xml:",innerxml"`
|
||||
Content string `xml:",cdata"`
|
||||
Nodes []Node `xml:",any"`
|
||||
}
|
||||
|
||||
|
@ -47,5 +47,8 @@ func (n Node) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
|
|||
|
||||
err = e.EncodeToken(start)
|
||||
e.EncodeElement(n.Nodes, xml.StartElement{Name: n.XMLName})
|
||||
if n.Content != "" {
|
||||
e.EncodeToken(xml.CharData(n.Content))
|
||||
}
|
||||
return e.EncodeToken(xml.EndElement{Name: start.Name})
|
||||
}
|
||||
|
|
30
stanza/node_test.go
Normal file
30
stanza/node_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNode_Marshal(t *testing.T) {
|
||||
jsonData := []byte("{\"key\":\"value\"}")
|
||||
|
||||
iqResp := NewIQ(Attrs{Type: "result", From: "admin@localhost", To: "test@localhost", Id: "1"})
|
||||
iqResp.Any = &Node{
|
||||
XMLName: xml.Name{Space: "myNS", Local: "space"},
|
||||
Content: string(jsonData),
|
||||
}
|
||||
|
||||
bytes, err := xml.Marshal(iqResp)
|
||||
if err != nil {
|
||||
t.Errorf("Could not marshal XML: %v", err)
|
||||
}
|
||||
|
||||
parsedIQ := IQ{}
|
||||
if err := xml.Unmarshal(bytes, &parsedIQ); err != nil {
|
||||
t.Errorf("Unmarshal returned error: %v", err)
|
||||
}
|
||||
|
||||
if parsedIQ.Any.Content != string(jsonData) {
|
||||
t.Errorf("Cannot find generic any payload in parsedIQ: '%s'", parsedIQ.Any.Content)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue