mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2025-11-23 18:13:42 -08:00
Always add an id to IQ queries
This commit is contained in:
committed by
Mickaël Rémond
parent
0227596f90
commit
21f6a549db
@@ -2,6 +2,8 @@ package stanza
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -31,8 +33,12 @@ type IQPayload interface {
|
||||
}
|
||||
|
||||
func NewIQ(a Attrs) IQ {
|
||||
// TODO generate IQ ID if not set
|
||||
// TODO ensure that type is set, as it is required
|
||||
if a.Id == "" {
|
||||
if id, err := uuid.NewRandom(); err == nil {
|
||||
a.Id = id.String()
|
||||
}
|
||||
}
|
||||
return IQ{
|
||||
XMLName: xml.Name{Local: "iq"},
|
||||
Attrs: a,
|
||||
|
||||
@@ -34,6 +34,24 @@ func TestUnmarshalIqs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateIqId(t *testing.T) {
|
||||
t.Parallel()
|
||||
iq := stanza.NewIQ(stanza.Attrs{Id: "1"})
|
||||
if iq.Id != "1" {
|
||||
t.Errorf("NewIQ replaced id with %s", iq.Id)
|
||||
}
|
||||
|
||||
iq = stanza.NewIQ(stanza.Attrs{})
|
||||
if iq.Id != "1" {
|
||||
t.Error("NewIQ did not generate an Id")
|
||||
}
|
||||
|
||||
otherIq := stanza.NewIQ(stanza.Attrs{})
|
||||
if iq.Id == otherIq.Id {
|
||||
t.Errorf("NewIQ generated two identical ids: %s", iq.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateIq(t *testing.T) {
|
||||
iq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, From: "admin@localhost", To: "test@localhost", Id: "1"})
|
||||
payload := stanza.DiscoInfo{
|
||||
|
||||
Reference in New Issue
Block a user