Skip to content

Commit

Permalink
Trim leading slash on soap path
Browse files Browse the repository at this point in the history
  • Loading branch information
rikatz committed Aug 3, 2023
1 parent ba843e5 commit 4882f1c
Show file tree
Hide file tree
Showing 2 changed files with 136 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
135 changes: 135 additions & 0 deletions vim25/soap/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"sync"
"testing"

"github.com/vmware/govmomi/vim25/types"
)

type mockRT struct{}
Expand Down Expand Up @@ -156,3 +160,134 @@ 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)
}
})
}
}

func TestClient_ParseURL(t *testing.T) {
type fields struct {
Client http.Client
u *url.URL
k bool
d *debugContainer
t *http.Transport
hostsMu sync.Mutex
hosts map[string]string
Namespace string
Version string
Types types.Func
UserAgent string
cookie string
insecureCookies bool
useJSON bool
}
type args struct {
urlStr string
}
tests := []struct {
name string
fields fields
args args
want *url.URL
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {

Check failure on line 265 in vim25/soap/client_test.go

View workflow job for this annotation

GitHub Actions / Lint Files

copylocks: range var tt copies lock: struct{name string; fields github.com/vmware/govmomi/vim25/soap.fields; args github.com/vmware/govmomi/vim25/soap.args; want *net/url.URL; wantErr bool} contains github.com/vmware/govmomi/vim25/soap.fields contains sync.Mutex (govet)
t.Run(tt.name, func(t *testing.T) {
c := &Client{
Client: tt.fields.Client,
u: tt.fields.u,
k: tt.fields.k,
d: tt.fields.d,
t: tt.fields.t,
hostsMu: tt.fields.hostsMu,

Check failure on line 273 in vim25/soap/client_test.go

View workflow job for this annotation

GitHub Actions / Lint Files

copylocks: literal copies lock value from tt.fields.hostsMu: sync.Mutex (govet)
hosts: tt.fields.hosts,
Namespace: tt.fields.Namespace,
Version: tt.fields.Version,
Types: tt.fields.Types,
UserAgent: tt.fields.UserAgent,
cookie: tt.fields.cookie,
insecureCookies: tt.fields.insecureCookies,
useJSON: tt.fields.useJSON,
}
got, err := c.ParseURL(tt.args.urlStr)
if (err != nil) != tt.wantErr {
t.Errorf("Client.ParseURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Client.ParseURL() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 4882f1c

Please sign in to comment.