From 9abd5be0aa1bfad19eae59b1a48d218d6bf9fd5f Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Wed, 6 Nov 2019 01:27:37 +0200 Subject: [PATCH 1/2] Support partial JIDs in Bare/Full methods --- jid.go | 14 ++++++++++++-- jid_test.go | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/jid.go b/jid.go index cee292a..f44019b 100644 --- a/jid.go +++ b/jid.go @@ -51,11 +51,21 @@ func NewJid(sjid string) (*Jid, error) { } func (j *Jid) Full() string { - return j.Node + "@" + j.Domain + "/" + j.Resource + if j.Resource == "" { + return j.Bare() + } else if j.Node == "" { + return j.Node + "/" + j.Resource + } else { + return j.Node + "@" + j.Domain + "/" + j.Resource + } } func (j *Jid) Bare() string { - return j.Node + "@" + j.Domain + if j.Node == "" { + return j.Domain + } else { + return j.Node + "@" + j.Domain + } } // ============================================================================ diff --git a/jid_test.go b/jid_test.go index c6fee03..45483dd 100644 --- a/jid_test.go +++ b/jid_test.go @@ -61,26 +61,41 @@ func TestIncorrectJids(t *testing.T) { } func TestFull(t *testing.T) { - jid := "test@domain.com/my resource" - parsedJid, err := NewJid(jid) - if err != nil { - t.Errorf("could not parse jid: %v", err) + fullJids := []string{ + "test@domain.com/my resource", + "test@domain.com", + "domain.com", } - fullJid := parsedJid.Full() - if fullJid != jid { - t.Errorf("incorrect full jid: %s", fullJid) + for _, sjid := range fullJids { + parsedJid, err := NewJid(sjid) + if err != nil { + t.Errorf("could not parse jid: %v", err) + } + fullJid := parsedJid.Full() + if fullJid != sjid { + t.Errorf("incorrect full jid: %s", fullJid) + } } } func TestBare(t *testing.T) { - jid := "test@domain.com" - fullJid := jid + "/my resource" - parsedJid, err := NewJid(fullJid) - if err != nil { - t.Errorf("could not parse jid: %v", err) + tests := []struct { + jidstr string + expected string + }{ + {jidstr: "test@domain.com", expected: "test@domain.com"}, + {jidstr: "test@domain.com/resource", expected: "test@domain.com"}, + {jidstr: "domain.com", expected: "domain.com"}, } - bareJid := parsedJid.Bare() - if bareJid != jid { - t.Errorf("incorrect bare jid: %s", bareJid) + + for _, tt := range tests { + parsedJid, err := NewJid(tt.jidstr) + if err != nil { + t.Errorf("could not parse jid: %v", err) + } + bareJid := parsedJid.Bare() + if bareJid != tt.expected { + t.Errorf("incorrect bare jid: %s", bareJid) + } } } From 69619ebc6d7a458d98d0573c629525e2c44a7cd0 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Fri, 8 Jul 2022 14:40:59 -0400 Subject: [PATCH 2/2] Support for XEP-0128. --- stanza/iq_disco.go | 1 + stanza/iq_disco_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/stanza/iq_disco.go b/stanza/iq_disco.go index 8e50f90..369a7ad 100644 --- a/stanza/iq_disco.go +++ b/stanza/iq_disco.go @@ -21,6 +21,7 @@ type DiscoInfo struct { Identity []Identity `xml:"identity"` Features []Feature `xml:"feature"` ResultSet *ResultSet `xml:"set,omitempty"` + Form *Form `xml:"x,omitempty"` } // Namespace lets DiscoInfo implement the IQPayload interface diff --git a/stanza/iq_disco_test.go b/stanza/iq_disco_test.go index 87b7001..e18f518 100644 --- a/stanza/iq_disco_test.go +++ b/stanza/iq_disco_test.go @@ -16,6 +16,7 @@ func TestDiscoInfo_Builder(t *testing.T) { disco := iq.DiscoInfo() disco.AddIdentity("Test Component", "gateway", "service") disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1") + disco.Form = stanza.NewForm([]*stanza.Field{}, "result") parsedIQ, err := checkMarshalling(t, iq) if err != nil { @@ -48,6 +49,15 @@ func TestDiscoInfo_Builder(t *testing.T) { t.Errorf("Incorrect identity name: %#v", pp.Identity[0].Name) } } + + // Check form + if pp.Form == nil { + t.Errorf("Form is nil") + } else { + if len(pp.Form.Fields) != 0 { + t.Errorf("Form fields length mismatch: %#v", pp.Form.Fields) + } + } } // Implements XEP-0030 example 17