From 9a1ba21162e712758e5e4f9b57461c1b7b7d89dc Mon Sep 17 00:00:00 2001 From: Marcel Blijleven Date: Fri, 17 Sep 2021 13:23:09 +0200 Subject: [PATCH 1/2] Fix typo in description --- utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.go b/utils.go index e6caf50..f8fd664 100644 --- a/utils.go +++ b/utils.go @@ -32,7 +32,7 @@ func newPathErrorErr(op string, path string, err error) error { } } -// PathEscape escapes all segemnts of a given path +// PathEscape escapes all segments of a given path func PathEscape(path string) string { s := strings.Split(path, "/") for i, e := range s { From a2cbdfa976a76fbb2b4576a1a9d64ca415ecad85 Mon Sep 17 00:00:00 2001 From: Marcel Blijleven Date: Fri, 17 Sep 2021 13:24:28 +0200 Subject: [PATCH 2/2] Fix index out of range runtime error when provided string is empty --- utils.go | 3 ++- utils_test.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/utils.go b/utils.go index f8fd664..5b96468 100644 --- a/utils.go +++ b/utils.go @@ -51,9 +51,10 @@ func FixSlash(s string) string { // FixSlashes appends and prepends a / if they are missing func FixSlashes(s string) string { - if s[0] != '/' { + if !strings.HasPrefix(s, "/") { s = "/" + s } + return FixSlash(s) } diff --git a/utils_test.go b/utils_test.go index 9751c9c..04410fd 100644 --- a/utils_test.go +++ b/utils_test.go @@ -43,3 +43,25 @@ func TestEscapeURL(t *testing.T) { t.Error("expected: " + ex + " got: " + u.String()) } } + +func TestFixSlashes(t *testing.T) { + expected := "/" + + if got := FixSlashes(""); got != expected { + t.Errorf("expected: %q, got: %q", expected, got) + } + + expected = "/path/" + + if got := FixSlashes("path"); got != expected { + t.Errorf("expected: %q, got: %q", expected, got) + } + + if got := FixSlashes("/path"); got != expected { + t.Errorf("expected: %q, got: %q", expected, got) + } + + if got := FixSlashes("path/"); got != expected { + t.Errorf("expected: %q, got: %q", expected, got) + } +}