Skip to content

Commit

Permalink
fix: Trim only trailing slashes in URLs (#278)
Browse files Browse the repository at this point in the history
Updated the clerk.JoinPath method to trim slashes only at the end of
URLs.
  • Loading branch information
gkats authored Mar 28, 2024
1 parent 39301db commit bc76488
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 1 addition & 2 deletions clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ func (b *defaultBackend) newRequest(ctx context.Context, apiReq *APIRequest) (*h
req.Header.Add("Clerk-API-Version", clerkAPIVersion)
req.Header.Add("X-Clerk-SDK", fmt.Sprintf("go/%s", sdkVersion))
b.CustomRequestHeaders.apply(req)
req = req.WithContext(ctx)

return req, nil
}
Expand Down Expand Up @@ -492,7 +491,7 @@ func JoinPath(base string, elem ...string) (string, error) {
// multiple backslashes in a row with one backslash, preserve the
// protocol's two backslashes.
// e.g. http://foo.com//bar/ will become http://foo.com/bar
res := extraBackslashesRE.ReplaceAllString(strings.Trim(sb.String(), "/"), "$1/")
res := extraBackslashesRE.ReplaceAllString(strings.TrimRight(sb.String(), "/"), "$1/")

// Make sure we have a valid URL.
u, err := url.Parse(res)
Expand Down
29 changes: 29 additions & 0 deletions clerk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,32 @@ func TestBackendCall_Multipart(t *testing.T) {
err := GetBackend().Call(ctx, req, &testResource{})
require.NoError(t, err)
}

func TestJoinPath(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
base string
paths []string
want string
}{
{base: "clerk.com", paths: []string{"baz", "1"}, want: "clerk.com/baz/1"},
{base: "https://clerk.com", paths: []string{"baz", "1"}, want: "https://clerk.com/baz/1"},
{base: "http://clerk.com", paths: []string{"baz", "1"}, want: "http://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/"}, 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/"}, 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"},
} {
got, err := JoinPath(tc.base, tc.paths...)
require.NoError(t, err)
require.Equal(t, tc.want, got)

got, err = JoinPath(tc.base+"/", tc.paths...)
require.NoError(t, err)
require.Equal(t, tc.want, got)
}

_, err := JoinPath("https://clerk.com", "*%{wontwork$")
require.Error(t, err)
}

0 comments on commit bc76488

Please sign in to comment.