Support partial JIDs in Bare/Full methods

This commit is contained in:
bodqhrohro 2019-11-06 01:27:37 +02:00 committed by Mickaël Rémond
parent ac5b066815
commit 5f99e1cd06
2 changed files with 42 additions and 17 deletions

View File

@ -51,12 +51,22 @@ func NewJid(sjid string) (*Jid, error) {
} }
func (j *Jid) Full() string { func (j *Jid) Full() string {
if j.Resource == "" {
return j.Bare()
} else if j.Node == "" {
return j.Node + "/" + j.Resource
} else {
return j.Node + "@" + j.Domain + "/" + j.Resource return j.Node + "@" + j.Domain + "/" + j.Resource
} }
}
func (j *Jid) Bare() string { func (j *Jid) Bare() string {
if j.Node == "" {
return j.Domain
} else {
return j.Node + "@" + j.Domain return j.Node + "@" + j.Domain
} }
}
// ============================================================================ // ============================================================================
// Helpers, for parsing / validation // Helpers, for parsing / validation

View File

@ -61,26 +61,41 @@ func TestIncorrectJids(t *testing.T) {
} }
func TestFull(t *testing.T) { func TestFull(t *testing.T) {
jid := "test@domain.com/my resource" fullJids := []string{
parsedJid, err := NewJid(jid) "test@domain.com/my resource",
"test@domain.com",
"domain.com",
}
for _, sjid := range fullJids {
parsedJid, err := NewJid(sjid)
if err != nil { if err != nil {
t.Errorf("could not parse jid: %v", err) t.Errorf("could not parse jid: %v", err)
} }
fullJid := parsedJid.Full() fullJid := parsedJid.Full()
if fullJid != jid { if fullJid != sjid {
t.Errorf("incorrect full jid: %s", fullJid) t.Errorf("incorrect full jid: %s", fullJid)
} }
} }
}
func TestBare(t *testing.T) { func TestBare(t *testing.T) {
jid := "test@domain.com" tests := []struct {
fullJid := jid + "/my resource" jidstr string
parsedJid, err := NewJid(fullJid) 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"},
}
for _, tt := range tests {
parsedJid, err := NewJid(tt.jidstr)
if err != nil { if err != nil {
t.Errorf("could not parse jid: %v", err) t.Errorf("could not parse jid: %v", err)
} }
bareJid := parsedJid.Bare() bareJid := parsedJid.Bare()
if bareJid != jid { if bareJid != tt.expected {
t.Errorf("incorrect bare jid: %s", bareJid) t.Errorf("incorrect bare jid: %s", bareJid)
} }
} }
}