Skip to content

Commit

Permalink
Allowing the base url to be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
izaaklauer committed May 15, 2024
1 parent 2d117c9 commit ae6fdde
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 7 additions & 6 deletions clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ func NewClock() Clock {
return &defaultClock{}
}

// Regular expression that matches multiple backslashes in a row.
var extraBackslashesRE = regexp.MustCompile("([^:])//+")
// Regular expression that matches multiple forward slashes in a row.
var extraForwardslashesRE = regexp.MustCompile("(^/+|([^:])//+)")

// JoinPath returns a URL string with the provided path elements joined
// with the base path.
Expand All @@ -487,11 +487,12 @@ func JoinPath(base string, elem ...string) (string, error) {
sb.WriteString("/")
sb.WriteString(el)
}
// Trim leading and trailing backslashes, replace all occurrences of
// multiple backslashes in a row with one backslash, preserve the
// protocol's two backslashes.
// Trim leading and trailing forward slashes, replace all occurrences of
// multiple forward slashes in a row with one forward slash, preserve the
// protocol's two forward slashes.
// e.g. http://foo.com//bar/ will become http://foo.com/bar
res := extraBackslashesRE.ReplaceAllString(strings.TrimRight(sb.String(), "/"), "$1/")
trimRightForwardSlashes := strings.TrimRight(sb.String(), "/")
res := extraForwardslashesRE.ReplaceAllString(trimRightForwardSlashes, "$2/")

// Make sure we have a valid URL.
u, err := url.Parse(res)
Expand Down
5 changes: 5 additions & 0 deletions clerk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ func TestJoinPath(t *testing.T) {
{base: "https://clerk.com", paths: []string{"//baz/", "/1/"}, want: "https://clerk.com/baz/1"},
{base: "https://clerk.com", paths: []string{"//baz/", "///1/"}, want: "https://clerk.com/baz/1"},
{base: "https://clerk.com", paths: []string{"/baz/", "/1?foo=bar&baz=bar/"}, want: "https://clerk.com/baz/1?foo=bar&baz=bar"},

// Tests an empty basepath, which should only be necessary when attempting
// to intercept the sdk's calls to the clerk API.
{base: "", paths: []string{"/baz"}, want: "/baz"},
{base: "/", paths: []string{"/baz"}, want: "/baz"},
} {
got, err := JoinPath(tc.base, tc.paths...)
require.NoError(t, err)
Expand Down

0 comments on commit ae6fdde

Please sign in to comment.