Fix index out of range runtime error when provided string is empty
This commit is contained in:
parent
9a1ba21162
commit
a2cbdfa976
3
utils.go
3
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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user