[#275] added support to customize the default user email templates from the Admin UI
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NormalizeUrl removes duplicated slashes from a url path.
|
||||
func NormalizeUrl(originalUrl string) (string, error) {
|
||||
u, err := url.Parse(originalUrl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hasSlash := strings.HasSuffix(u.Path, "/")
|
||||
|
||||
// clean up path by removing duplicated /
|
||||
u.Path = path.Clean(u.Path)
|
||||
u.RawPath = path.Clean(u.RawPath)
|
||||
|
||||
// restore original trailing slash
|
||||
if hasSlash && !strings.HasSuffix(u.Path, "/") {
|
||||
u.Path += "/"
|
||||
u.RawPath += "/"
|
||||
}
|
||||
|
||||
return u.String(), nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package rest_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/rest"
|
||||
)
|
||||
|
||||
func TestNormalizeUrl(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
url string
|
||||
expectError bool
|
||||
expectUrl string
|
||||
}{
|
||||
{":/", true, ""},
|
||||
{"./", false, "./"},
|
||||
{"../../test////", false, "../../test/"},
|
||||
{"/a/b/c", false, "/a/b/c"},
|
||||
{"a/////b//c/", false, "a/b/c/"},
|
||||
{"/a/////b//c", false, "/a/b/c"},
|
||||
{"///a/b/c", false, "/a/b/c"},
|
||||
{"//a/b/c", false, "//a/b/c"}, // preserve "auto-schema"
|
||||
{"http://a/b/c", false, "http://a/b/c"},
|
||||
{"a//bc?test=1//dd", false, "a/bc?test=1//dd"}, // only the path is normalized
|
||||
{"a//bc?test=1#12///3", false, "a/bc?test=1#12///3"}, // only the path is normalized
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
result, err := rest.NormalizeUrl(s.url)
|
||||
|
||||
hasErr := err != nil
|
||||
if hasErr != s.expectError {
|
||||
t.Errorf("(%d) Expected hasErr %v, got %v", i, s.expectError, hasErr)
|
||||
}
|
||||
|
||||
if result != s.expectUrl {
|
||||
t.Errorf("(%d) Expected url %q, got %q", i, s.expectUrl, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user