From 40264da9ba7c9dae5d3b6a5688c67eb982533fc6 Mon Sep 17 00:00:00 2001 From: Ricardo Katz Date: Thu, 3 Aug 2023 18:15:14 -0300 Subject: [PATCH] Trim leading slash on soap path --- vim25/soap/client.go | 1 + vim25/soap/client_test.go | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/vim25/soap/client.go b/vim25/soap/client.go index 390a54703..4e747e9f8 100644 --- a/vim25/soap/client.go +++ b/vim25/soap/client.go @@ -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 diff --git a/vim25/soap/client_test.go b/vim25/soap/client_test.go index 2f69af2b4..9ccc4758b 100644 --- a/vim25/soap/client_test.go +++ b/vim25/soap/client_test.go @@ -22,6 +22,7 @@ import ( "net/http" "net/url" "os" + "reflect" "testing" ) @@ -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) + } + }) + } +}