Skip to content

Commit

Permalink
Merge pull request #3201 from rikatz/trim-vc-path
Browse files Browse the repository at this point in the history
Trim leading slash on soap path
  • Loading branch information
rikatz authored Aug 10, 2023
2 parents e7aac6a + 40264da commit b504836
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions vim25/soap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func ParseURL(s string) (*url.URL, error) {
s = "https://" + s
}

s := strings.TrimSuffix(s, "/")
u, err = url.Parse(s)
if err != nil {
return nil, err
Expand Down
73 changes: 73 additions & 0 deletions vim25/soap/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"testing"
)

Expand Down Expand Up @@ -156,3 +157,75 @@ func setCAsOnClient(cas string) error {

return client.SetRootCAs(cas)
}

func TestParseURL(t *testing.T) {
tests := []struct {
name string
s string
want *url.URL
wantErr bool
}{
{
name: "empty URL should return null",
want: nil,
},
{
name: "just endpoint should return full URL",
s: "some.vcenter.tld",
want: &url.URL{
Scheme: "https",
Path: "/sdk",
Host: "some.vcenter.tld",
User: url.UserPassword("", ""),
},
},
{
name: "URL with / on suffix should be trimmed",
s: "https://some.vcenter.tld/",
want: &url.URL{
Scheme: "https",
Path: "/sdk",
Host: "some.vcenter.tld",
User: url.UserPassword("", ""),
},
},
{
name: "URL with user and password should be used",
s: "https://user:password@some.vcenter.tld",
want: &url.URL{
Scheme: "https",
Path: "/sdk",
Host: "some.vcenter.tld",
User: url.UserPassword("user", "password"),
},
},
{
name: "existing path should be used",
s: "https://some.vcenter.tld/othersdk",
want: &url.URL{
Scheme: "https",
Path: "/othersdk",
Host: "some.vcenter.tld",
User: url.UserPassword("", ""),
},
},
{
name: "Invalid URL should be rejected",
s: "https://user:password@some.vcenter.tld:xpto1234",
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseURL(tt.s)
if (err != nil) != tt.wantErr {
t.Errorf("ParseURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseURL() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit b504836

Please sign in to comment.