Merge pull request #45 from marcelblijleven/master

Fix index out of range runtime error when empty string is provided to FixSlashes
This commit is contained in:
Christoph Polcin 2021-09-17 15:32:50 +02:00 committed by GitHub
commit a3a86976a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -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 { func PathEscape(path string) string {
s := strings.Split(path, "/") s := strings.Split(path, "/")
for i, e := range s { for i, e := range s {
@ -51,9 +51,10 @@ func FixSlash(s string) string {
// FixSlashes appends and prepends a / if they are missing // FixSlashes appends and prepends a / if they are missing
func FixSlashes(s string) string { func FixSlashes(s string) string {
if s[0] != '/' { if !strings.HasPrefix(s, "/") {
s = "/" + s s = "/" + s
} }
return FixSlash(s) return FixSlash(s)
} }

View File

@ -43,3 +43,25 @@ func TestEscapeURL(t *testing.T) {
t.Error("expected: " + ex + " got: " + u.String()) 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)
}
}