2019-06-26 08:14:52 -07:00
|
|
|
package stanza_test
|
2019-06-23 06:52:20 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTMLGen(t *testing.T) {
|
|
|
|
htmlBody := "<p>Hello <b>World</b></p>"
|
2019-06-26 08:14:52 -07:00
|
|
|
msg := NewMessage(Attrs{To: "test@localhost"})
|
2019-06-23 06:52:20 -07:00
|
|
|
msg.Body = "Hello World"
|
2019-06-26 08:14:52 -07:00
|
|
|
body := HTMLBody{
|
2019-06-23 06:52:20 -07:00
|
|
|
InnerXML: htmlBody,
|
|
|
|
}
|
2019-06-26 08:14:52 -07:00
|
|
|
html := HTML{Body: body}
|
2019-06-23 06:52:20 -07:00
|
|
|
msg.Extensions = append(msg.Extensions, html)
|
|
|
|
|
|
|
|
result := msg.XMPPFormat()
|
|
|
|
str := `<message to="test@localhost"><body>Hello World</body><html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="http://www.w3.org/1999/xhtml"><p>Hello <b>World</b></p></body></html></message>`
|
|
|
|
if result != str {
|
|
|
|
t.Errorf("incorrect serialize message:\n%s", result)
|
|
|
|
}
|
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
parsedMessage := Message{}
|
2019-06-23 06:52:20 -07:00
|
|
|
if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil {
|
|
|
|
t.Errorf("message HTML unmarshall error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedMessage.Body != msg.Body {
|
|
|
|
t.Errorf("incorrect parsed body: '%s'", parsedMessage.Body)
|
|
|
|
}
|
|
|
|
|
2019-06-26 08:14:52 -07:00
|
|
|
var h HTML
|
2019-06-23 06:52:20 -07:00
|
|
|
if ok := parsedMessage.Get(&h); !ok {
|
|
|
|
t.Error("could not extract HTML body")
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Body.InnerXML != htmlBody {
|
|
|
|
t.Errorf("could not extract html body: '%s'", h.Body.InnerXML)
|
|
|
|
}
|
|
|
|
}
|