From bf02e3bacfa9175e363d6bb0fa46eb2b7e92b2a5 Mon Sep 17 00:00:00 2001 From: Jack Henschel Date: Wed, 3 Feb 2021 09:22:05 +0200 Subject: [PATCH] fix: Ensure trailing slash in CustomEndpoint is handled gracefully --- simples3.go | 5 +++++ simples3_test.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/simples3.go b/simples3.go index 11ff045..b875fe3 100644 --- a/simples3.go +++ b/simples3.go @@ -189,6 +189,11 @@ func (s3 *S3) SetEndpoint(uri string) *S3 { if !strings.HasPrefix(uri, "http") { uri = "https://" + uri } + + // make sure there is no trailing slash + if uri[len(uri)-1] == '/' { + uri = uri[:len(uri)-1] + } s3.Endpoint = uri } return s3 diff --git a/simples3_test.go b/simples3_test.go index f54fc22..2b99e71 100644 --- a/simples3_test.go +++ b/simples3_test.go @@ -351,6 +351,12 @@ func TestCustomEndpoint(t *testing.T) { if s3.getURL("bucket3") != "https://example.com/bucket3" { t.Errorf("S3.SetEndpoint() got = %v", s3.Endpoint) } + + // try with trailing slash + s3.SetEndpoint("https://example.com/foobar/") + if s3.getURL("bucket4") != "https://example.com/foobar/bucket4" { + t.Errorf("S3.SetEndpoint() got = %v", s3.Endpoint) + } } func TestGetURL(t *testing.T) {