Skip to content

Commit

Permalink
Merge pull request #14 from dacohen/force-leading-slash
Browse files Browse the repository at this point in the history
Add leading slash to URL Path in getBaseUrlString function, if one does not already exist
  • Loading branch information
karen-avetisyan-mc authored Aug 22, 2024
2 parents 6c70d2a + bb5a4c4 commit 2a6b283
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func getBaseUrlString(u *url.URL) string {
host = hostPort[0]
}
path := u.EscapedPath()
if path == "" {
path = "/"
if path == "" || path[0] != '/' {
path = "/" + path
}
return fmt.Sprintf("%v://%v%v", scheme, host, path)
}
Expand Down
9 changes: 9 additions & 0 deletions oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ func TestGetBaseUrlString_ShouldAddTrailingSlash(t *testing.T) {
}
}

func TestGetBaseUrlString_ShouldAddLeadingSlashToPath(t *testing.T) {
u, _ := url.Parse("https://api.mastercard.com")
u.Path = "test/service"
baseUrl := getBaseUrlString(u)
if "https://api.mastercard.com/test/service" != baseUrl {
t.Errorf("Something went wrong, got %v", baseUrl)
}
}

func TestGetBaseUrlString_ShouldUseLowercaseSchemesAndHosts(t *testing.T) {
u, _ := url.Parse("HTTPS://API.MASTERCARD.COM/TEST")
baseUrl := getBaseUrlString(u)
Expand Down

0 comments on commit 2a6b283

Please sign in to comment.