Repository: vmware/govmomi
Pull request #3201 (2023-08-03 21:18:50)

Trim leading slash on soap path
## Description

When passing an endpoint as `https://some.vcenter.tld/` to govc (or soap client), it will consider the leading / as part of the path and not add the `/sdk`.

This leads to errors when consuming vCenter API.

While passing the leading "/" may be desired, I'm not sure if there's a better way to turn this error more intuitive.

This PR also adds some unit tests on ParseURL function

Closes: #(issue-number)

## Type of change

Please mark options that are relevant:

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to
  not work as expected)
- [ ] This change requires a documentation update
- [ ] Build related change

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. If applicable, please also list any relevant
details for your test configuration.

- Added unit tests
- Tested soap.ParseURL with and without the change, and passing the URL as `https://some.vcenter.tld` with and without the leading path. Without the change, it always gets an unauthorized access

## Checklist:

- [ ] My code follows the `CONTRIBUTION` [guidelines] of this project
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

[guidelines]: https://github.com/vmware/govmomi/blob/main/CONTRIBUTING.md



Patch:
diff --git a/vim25/soap/client.go b/vim25/soap/client.go
index 390a5470..4e747e9f 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


Test patch:
diff --git a/vim25/soap/client_test.go b/vim25/soap/client_test.go
index 2f69af2b..9ccc4758 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)
+			}
+		})
+	}
+}