Fix tests after refactor

This commit is contained in:
Mickael Remond 2019-06-26 17:28:54 +02:00
parent 428787d7ab
commit 5ed66de79e
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
6 changed files with 53 additions and 44 deletions

View File

@ -6,21 +6,22 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"gosrc.io/xmpp/stanza"
) )
func TestUnmarshalIqs(t *testing.T) { func TestUnmarshalIqs(t *testing.T) {
//var cs1 = new(iot.ControlSet) //var cs1 = new(iot.ControlSet)
var tests = []struct { var tests = []struct {
iqString string iqString string
parsedIQ IQ parsedIQ stanza.IQ
}{ }{
{"<iq id=\"1\" type=\"set\" to=\"test@localhost\"/>", {"<iq id=\"1\" type=\"set\" to=\"test@localhost\"/>",
IQ{XMLName: xml.Name{Local: "iq"}, Attrs: Attrs{Type: IQTypeSet, To: "test@localhost", Id: "1"}}}, stanza.IQ{XMLName: xml.Name{Local: "iq"}, Attrs: stanza.Attrs{Type: stanza.IQTypeSet, To: "test@localhost", Id: "1"}}},
//{"<iq xmlns=\"jabber:client\" id=\"2\" type=\"set\" to=\"test@localhost\" from=\"server\"><set xmlns=\"urn:xmpp:iot:control\"/></iq>", IQ{XMLName: xml.Name{Space: "jabber:client", Local: "iq"}, PacketAttrs: PacketAttrs{To: "test@localhost", From: "server", Type: "set", Id: "2"}, Payload: cs1}}, //{"<iq xmlns=\"jabber:client\" id=\"2\" type=\"set\" to=\"test@localhost\" from=\"server\"><set xmlns=\"urn:xmpp:iot:control\"/></iq>", IQ{XMLName: xml.Name{Space: "jabber:client", Local: "iq"}, PacketAttrs: PacketAttrs{To: "test@localhost", From: "server", Type: "set", Id: "2"}, Payload: cs1}},
} }
for _, test := range tests { for _, test := range tests {
parsedIQ := IQ{} parsedIQ := stanza.IQ{}
err := xml.Unmarshal([]byte(test.iqString), &parsedIQ) err := xml.Unmarshal([]byte(test.iqString), &parsedIQ)
if err != nil { if err != nil {
t.Errorf("Unmarshal(%s) returned error", test.iqString) t.Errorf("Unmarshal(%s) returned error", test.iqString)
@ -34,16 +35,16 @@ func TestUnmarshalIqs(t *testing.T) {
} }
func TestGenerateIq(t *testing.T) { func TestGenerateIq(t *testing.T) {
iq := NewIQ(Attrs{Type: IQTypeResult, From: "admin@localhost", To: "test@localhost", Id: "1"}) iq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, From: "admin@localhost", To: "test@localhost", Id: "1"})
payload := DiscoInfo{ payload := stanza.DiscoInfo{
Identity: Identity{ Identity: stanza.Identity{
Name: "Test Gateway", Name: "Test Gateway",
Category: "gateway", Category: "gateway",
Type: "mqtt", Type: "mqtt",
}, },
Features: []Feature{ Features: []stanza.Feature{
{Var: NSDiscoInfo}, {Var: stanza.NSDiscoInfo},
{Var: NSDiscoItems}, {Var: stanza.NSDiscoItems},
}, },
} }
iq.Payload = &payload iq.Payload = &payload
@ -57,7 +58,7 @@ func TestGenerateIq(t *testing.T) {
t.Error("empty error should not be serialized") t.Error("empty error should not be serialized")
} }
parsedIQ := IQ{} parsedIQ := stanza.IQ{}
if err = xml.Unmarshal(data, &parsedIQ); err != nil { if err = xml.Unmarshal(data, &parsedIQ); err != nil {
t.Errorf("Unmarshal(%s) returned error", data) t.Errorf("Unmarshal(%s) returned error", data)
} }
@ -68,7 +69,7 @@ func TestGenerateIq(t *testing.T) {
} }
func TestErrorTag(t *testing.T) { func TestErrorTag(t *testing.T) {
xError := Err{ xError := stanza.Err{
XMLName: xml.Name{Local: "error"}, XMLName: xml.Name{Local: "error"},
Code: 503, Code: 503,
Type: "cancel", Type: "cancel",
@ -81,7 +82,7 @@ func TestErrorTag(t *testing.T) {
t.Errorf("cannot marshal xml structure: %s", err) t.Errorf("cannot marshal xml structure: %s", err)
} }
parsedError := Err{} parsedError := stanza.Err{}
if err = xml.Unmarshal(data, &parsedError); err != nil { if err = xml.Unmarshal(data, &parsedError); err != nil {
t.Errorf("Unmarshal(%s) returned error", data) t.Errorf("Unmarshal(%s) returned error", data)
} }
@ -92,8 +93,8 @@ func TestErrorTag(t *testing.T) {
} }
func TestDiscoItems(t *testing.T) { func TestDiscoItems(t *testing.T) {
iq := NewIQ(Attrs{Type: IQTypeGet, From: "romeo@montague.net/orchard", To: "catalog.shakespeare.lit", Id: "items3"}) iq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeGet, From: "romeo@montague.net/orchard", To: "catalog.shakespeare.lit", Id: "items3"})
payload := DiscoItems{ payload := stanza.DiscoItems{
Node: "music", Node: "music",
} }
iq.Payload = &payload iq.Payload = &payload
@ -103,7 +104,7 @@ func TestDiscoItems(t *testing.T) {
t.Errorf("cannot marshal xml structure") t.Errorf("cannot marshal xml structure")
} }
parsedIQ := IQ{} parsedIQ := stanza.IQ{}
if err = xml.Unmarshal(data, &parsedIQ); err != nil { if err = xml.Unmarshal(data, &parsedIQ); err != nil {
t.Errorf("Unmarshal(%s) returned error", data) t.Errorf("Unmarshal(%s) returned error", data)
} }
@ -116,7 +117,7 @@ func TestDiscoItems(t *testing.T) {
func TestUnmarshalPayload(t *testing.T) { func TestUnmarshalPayload(t *testing.T) {
query := "<iq to='service.localhost' type='get' id='1'><query xmlns='jabber:iq:version'/></iq>" query := "<iq to='service.localhost' type='get' id='1'><query xmlns='jabber:iq:version'/></iq>"
parsedIQ := IQ{} parsedIQ := stanza.IQ{}
err := xml.Unmarshal([]byte(query), &parsedIQ) err := xml.Unmarshal([]byte(query), &parsedIQ)
if err != nil { if err != nil {
t.Errorf("Unmarshal(%s) returned error", query) t.Errorf("Unmarshal(%s) returned error", query)
@ -141,7 +142,7 @@ func TestPayloadWithError(t *testing.T) {
</error> </error>
</iq>` </iq>`
parsedIQ := IQ{} parsedIQ := stanza.IQ{}
err := xml.Unmarshal([]byte(iq), &parsedIQ) err := xml.Unmarshal([]byte(iq), &parsedIQ)
if err != nil { if err != nil {
t.Errorf("Unmarshal error: %s", iq) t.Errorf("Unmarshal error: %s", iq)
@ -157,7 +158,7 @@ func TestUnknownPayload(t *testing.T) {
iq := `<iq type="get" to="service.localhost" id="1" > iq := `<iq type="get" to="service.localhost" id="1" >
<query xmlns="unknown:ns"/> <query xmlns="unknown:ns"/>
</iq>` </iq>`
parsedIQ := IQ{} parsedIQ := stanza.IQ{}
err := xml.Unmarshal([]byte(iq), &parsedIQ) err := xml.Unmarshal([]byte(iq), &parsedIQ)
if err != nil { if err != nil {
t.Errorf("Unmarshal error: %#v (%s)", err, iq) t.Errorf("Unmarshal error: %#v (%s)", err, iq)

View File

@ -5,10 +5,11 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"gosrc.io/xmpp/stanza"
) )
func TestGenerateMessage(t *testing.T) { func TestGenerateMessage(t *testing.T) {
message := NewMessage(Attrs{Type: MessageTypeChat, From: "admin@localhost", To: "test@localhost", Id: "1"}) message := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: "admin@localhost", To: "test@localhost", Id: "1"})
message.Body = "Hi" message.Body = "Hi"
message.Subject = "Msg Subject" message.Subject = "Msg Subject"
@ -17,7 +18,7 @@ func TestGenerateMessage(t *testing.T) {
t.Errorf("cannot marshal xml structure") t.Errorf("cannot marshal xml structure")
} }
parsedMessage := Message{} parsedMessage := stanza.Message{}
if err = xml.Unmarshal(data, &parsedMessage); err != nil { if err = xml.Unmarshal(data, &parsedMessage); err != nil {
t.Errorf("Unmarshal(%s) returned error", data) t.Errorf("Unmarshal(%s) returned error", data)
} }
@ -37,7 +38,7 @@ func TestDecodeError(t *testing.T) {
</error> </error>
</message>` </message>`
parsedMessage := Message{} parsedMessage := stanza.Message{}
if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil { if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil {
t.Errorf("message error stanza unmarshall error: %v", err) t.Errorf("message error stanza unmarshall error: %v", err)
return return
@ -49,15 +50,15 @@ func TestDecodeError(t *testing.T) {
func TestGetOOB(t *testing.T) { func TestGetOOB(t *testing.T) {
image := "https://localhost/image.png" image := "https://localhost/image.png"
msg := NewMessage(Attrs{To: "test@localhost"}) msg := stanza.NewMessage(stanza.Attrs{To: "test@localhost"})
ext := OOB{ ext := stanza.OOB{
XMLName: xml.Name{Space: "jabber:x:oob", Local: "x"}, XMLName: xml.Name{Space: "jabber:x:oob", Local: "x"},
URL: image, URL: image,
} }
msg.Extensions = append(msg.Extensions, &ext) msg.Extensions = append(msg.Extensions, &ext)
// OOB can properly be found // OOB can properly be found
var oob OOB var oob stanza.OOB
// Try to find and // Try to find and
if ok := msg.Get(&oob); !ok { if ok := msg.Get(&oob); !ok {
t.Error("could not find oob extension") t.Error("could not find oob extension")
@ -68,7 +69,7 @@ func TestGetOOB(t *testing.T) {
} }
// Markable is not found // Markable is not found
var m Markable var m stanza.Markable
if ok := msg.Get(&m); ok { if ok := msg.Get(&m); ok {
t.Error("we should not have found markable extension") t.Error("we should not have found markable extension")
} }

View File

@ -3,16 +3,18 @@ package stanza_test
import ( import (
"encoding/xml" "encoding/xml"
"testing" "testing"
"gosrc.io/xmpp/stanza"
) )
func TestHTMLGen(t *testing.T) { func TestHTMLGen(t *testing.T) {
htmlBody := "<p>Hello <b>World</b></p>" htmlBody := "<p>Hello <b>World</b></p>"
msg := NewMessage(Attrs{To: "test@localhost"}) msg := stanza.NewMessage(stanza.Attrs{To: "test@localhost"})
msg.Body = "Hello World" msg.Body = "Hello World"
body := HTMLBody{ body := stanza.HTMLBody{
InnerXML: htmlBody, InnerXML: htmlBody,
} }
html := HTML{Body: body} html := stanza.HTML{Body: body}
msg.Extensions = append(msg.Extensions, html) msg.Extensions = append(msg.Extensions, html)
result := msg.XMPPFormat() result := msg.XMPPFormat()
@ -21,7 +23,7 @@ func TestHTMLGen(t *testing.T) {
t.Errorf("incorrect serialize message:\n%s", result) t.Errorf("incorrect serialize message:\n%s", result)
} }
parsedMessage := Message{} parsedMessage := stanza.Message{}
if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil { if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil {
t.Errorf("message HTML unmarshall error: %v", err) t.Errorf("message HTML unmarshall error: %v", err)
return return
@ -31,7 +33,7 @@ func TestHTMLGen(t *testing.T) {
t.Errorf("incorrect parsed body: '%s'", parsedMessage.Body) t.Errorf("incorrect parsed body: '%s'", parsedMessage.Body)
} }
var h HTML var h stanza.HTML
if ok := parsedMessage.Get(&h); !ok { if ok := parsedMessage.Get(&h); !ok {
t.Error("could not extract HTML body") t.Error("could not extract HTML body")
} }

View File

@ -3,6 +3,8 @@ package stanza_test
import ( import (
"encoding/xml" "encoding/xml"
"testing" "testing"
"gosrc.io/xmpp/stanza"
) )
func TestDecodeRequest(t *testing.T) { func TestDecodeRequest(t *testing.T) {
@ -13,7 +15,7 @@ func TestDecodeRequest(t *testing.T) {
<body>My lord, dispatch; read o'er these articles.</body> <body>My lord, dispatch; read o'er these articles.</body>
<request xmlns='urn:xmpp:receipts'/> <request xmlns='urn:xmpp:receipts'/>
</message>` </message>`
parsedMessage := Message{} parsedMessage := stanza.Message{}
if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil { if err := xml.Unmarshal([]byte(str), &parsedMessage); err != nil {
t.Errorf("message receipt unmarshall error: %v", err) t.Errorf("message receipt unmarshall error: %v", err)
return return
@ -29,7 +31,7 @@ func TestDecodeRequest(t *testing.T) {
} }
switch ext := parsedMessage.Extensions[0].(type) { switch ext := parsedMessage.Extensions[0].(type) {
case *ReceiptRequest: case *stanza.ReceiptRequest:
if ext.XMLName.Local != "request" { if ext.XMLName.Local != "request" {
t.Errorf("unexpected extension: %s:%s", ext.XMLName.Space, ext.XMLName.Local) t.Errorf("unexpected extension: %s:%s", ext.XMLName.Space, ext.XMLName.Local)
} }

View File

@ -3,6 +3,8 @@ package stanza_test
import ( import (
"encoding/xml" "encoding/xml"
"testing" "testing"
"gosrc.io/xmpp/stanza"
) )
// https://xmpp.org/extensions/xep-0045.html#example-27 // https://xmpp.org/extensions/xep-0045.html#example-27
@ -16,12 +18,12 @@ func TestMucPassword(t *testing.T) {
</x> </x>
</presence>` </presence>`
var parsedPresence Presence var parsedPresence stanza.Presence
if err := xml.Unmarshal([]byte(str), &parsedPresence); err != nil { if err := xml.Unmarshal([]byte(str), &parsedPresence); err != nil {
t.Errorf("Unmarshal(%s) returned error", str) t.Errorf("Unmarshal(%s) returned error", str)
} }
var muc MucPresence var muc stanza.MucPresence
if ok := parsedPresence.Get(&muc); !ok { if ok := parsedPresence.Get(&muc); !ok {
t.Error("muc presence extension was not found") t.Error("muc presence extension was not found")
} }
@ -42,12 +44,12 @@ func TestMucHistory(t *testing.T) {
</x> </x>
</presence>` </presence>`
var parsedPresence Presence var parsedPresence stanza.Presence
if err := xml.Unmarshal([]byte(str), &parsedPresence); err != nil { if err := xml.Unmarshal([]byte(str), &parsedPresence); err != nil {
t.Errorf("Unmarshal(%s) returned error", str) t.Errorf("Unmarshal(%s) returned error", str)
} }
var muc MucPresence var muc stanza.MucPresence
if ok := parsedPresence.Get(&muc); !ok { if ok := parsedPresence.Get(&muc); !ok {
t.Error("muc presence extension was not found") t.Error("muc presence extension was not found")
} }

View File

@ -5,18 +5,19 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"gosrc.io/xmpp/stanza"
) )
func TestGeneratePresence(t *testing.T) { func TestGeneratePresence(t *testing.T) {
presence := NewPresence(Attrs{From: "admin@localhost", To: "test@localhost", Id: "1"}) presence := stanza.NewPresence(stanza.Attrs{From: "admin@localhost", To: "test@localhost", Id: "1"})
presence.Show = PresenceShowChat presence.Show = stanza.PresenceShowChat
data, err := xml.Marshal(presence) data, err := xml.Marshal(presence)
if err != nil { if err != nil {
t.Errorf("cannot marshal xml structure") t.Errorf("cannot marshal xml structure")
} }
var parsedPresence Presence var parsedPresence stanza.Presence
if err = xml.Unmarshal(data, &parsedPresence); err != nil { if err = xml.Unmarshal(data, &parsedPresence); err != nil {
t.Errorf("Unmarshal(%s) returned error", data) t.Errorf("Unmarshal(%s) returned error", data)
} }
@ -30,13 +31,13 @@ func TestPresenceSubElt(t *testing.T) {
// Test structure to ensure that show, status and priority are correctly defined as presence // Test structure to ensure that show, status and priority are correctly defined as presence
// package sub-elements // package sub-elements
type pres struct { type pres struct {
Show PresenceShow `xml:"show"` Show stanza.PresenceShow `xml:"show"`
Status string `xml:"status"` Status string `xml:"status"`
Priority int8 `xml:"priority"` Priority int8 `xml:"priority"`
} }
presence := NewPresence(Attrs{From: "admin@localhost", To: "test@localhost", Id: "1"}) presence := stanza.NewPresence(stanza.Attrs{From: "admin@localhost", To: "test@localhost", Id: "1"})
presence.Show = PresenceShowXA presence.Show = stanza.PresenceShowXA
presence.Status = "Coding" presence.Status = "Coding"
presence.Priority = 10 presence.Priority = 10