diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-10-17 11:32:19 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-10-17 11:44:18 +0200 |
| commit | 8635f8bbf5e4383fb2db6774757157f29195aec1 (patch) | |
| tree | db1bf71bf903a920b1330cc88689778adf19725d /internal/util | |
| parent | 51b536b45fce6bf2d7ec8bbfe6ce30faec11c88e (diff) | |
Server + Util: Ensure the base URL already ends with a /
Diffstat (limited to 'internal/util')
| -rw-r--r-- | internal/util/util.go | 9 | ||||
| -rw-r--r-- | internal/util/util_test.go | 41 |
2 files changed, 31 insertions, 19 deletions
diff --git a/internal/util/util.go b/internal/util/util.go index a8abd80..a500e15 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -29,7 +29,14 @@ func EnsureValidURL(s string) (string, error) { // https://pkg.go.dev/path#Clean parsedURL.Path = path.Clean(parsedURL.Path) } - return parsedURL.String(), nil + + returnedURL := parsedURL.String() + + // Make sure the URL ends with a / + if returnedURL[len(returnedURL)-1:] != "/" { + returnedURL = returnedURL + "/" + } + return returnedURL, nil } // Creates a random byteslice of `size` diff --git a/internal/util/util_test.go b/internal/util/util_test.go index f671251..eb1a9f6 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -13,24 +13,29 @@ func Test_EnsureValidURL(t *testing.T) { t.Fatal("Got nil error, want: non-nil") } - valid, validErr := EnsureValidURL("valid.com") - if validErr != nil { - t.Fatalf("Got: %v, want: nil", validErr) - } - - afterValid := "https://valid.com" - if valid != afterValid { - t.Fatalf("Got: %v, want: %v", valid, afterValid) - } - - valid, validErr = EnsureValidURL("http://valid.com") - if validErr != nil { - t.Fatalf("Got: %v, want: nil", validErr) - } - - afterValid = "http://valid.com" - if valid != afterValid { - t.Fatalf("Got: %v, want: %v", valid, afterValid) + testCases := map[string]string{ + // Make sure we set https + "example.com/": "https://example.com/", + // Make sure we do not override the scheme if provided + "http://example.com/": "http://example.com/", + // This URL is already valid + "https://example.com/": "https://example.com/", + // Make sure to add a trailing slash (/) + "https://example.com": "https://example.com/", + // Cleanup the path 1 + "https://example.com/////": "https://example.com/", + // Cleanup the path 2 + "https://example.com/..": "https://example.com/", + } + + for k, v := range testCases { + valid, validErr := EnsureValidURL(k) + if validErr != nil { + t.Fatalf("Got: %v, want: nil", validErr) + } + if valid != v { + t.Fatalf("Got: %v, want: %v", valid, v) + } } } |
