Strip / from IRI path if last character

This commit is contained in:
Marius Orcsik 2021-01-24 15:45:46 +01:00
parent db9606f596
commit 6ff39cfc8d
No known key found for this signature in database
GPG key ID: 7970BDC7D4CB2674
2 changed files with 52 additions and 5 deletions

View file

@ -115,6 +115,10 @@ func (t CollectionTypes) Split(i pub.IRI) (pub.IRI, CollectionType) {
// IRIf formats an IRI from an existing IRI and the collection type
func IRIf(i pub.IRI, t CollectionType) pub.IRI {
onePastLast := len(i)
if i[onePastLast-1] == '/' {
i = i[:onePastLast-1]
}
return pub.IRI(fmt.Sprintf("%s/%s", i, t))
}

View file

@ -1,6 +1,9 @@
package handlers
import "testing"
import (
"github.com/go-ap/activitypub"
"testing"
)
func TestPathTyper_Type(t *testing.T) {
t.Skipf("TODO")
@ -22,10 +25,6 @@ func TestValidCollectionIRI(t *testing.T) {
t.Skipf("TODO")
}
func TestIRIf(t *testing.T) {
t.Skipf("TODO")
}
func TestSplit(t *testing.T) {
t.Skipf("TODO")
}
@ -45,3 +44,47 @@ func TestCollectionType_OfActor(t *testing.T) {
func TestCollectionTypes_Contains(t *testing.T) {
t.Skipf("TODO")
}
func TestIRIf(t *testing.T) {
type args struct {
i activitypub.IRI
t CollectionType
}
tests := []struct {
name string
args args
want activitypub.IRI
}{
{
name: "plain concat",
args: args{
i: "https://example.com",
t: "inbox",
},
want: "https://example.com/inbox",
},
{
name: "strip root from iri",
args: args{
i: "https://example.com/",
t: "inbox",
},
want: "https://example.com/inbox",
},
{
name: "invalid iri",
args: args{
i: "example.com",
t: "test",
},
want: "example.com/test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IRIf(tt.args.i, tt.args.t); got != tt.want {
t.Errorf("IRIf() = %v, want %v", got, tt.want)
}
})
}
}