diff --git a/vim25/soap/client.go b/vim25/soap/client.go index 7b4b26ab6..b7ff99f5a 100644 --- a/vim25/soap/client.go +++ b/vim25/soap/client.go @@ -214,9 +214,8 @@ func (c *Client) NewServiceClient(path string, namespace string) *Client { return c.newServiceClientWithTransport(path, namespace, c.t) } -// SessionCookie returns a SessionCookie with value of the vmware_soap_session http.Cookie. -func (c *Client) SessionCookie() *HeaderElement { - for _, cookie := range c.Jar.Cookies(c.URL()) { +func sessionCookie(jar http.CookieJar, u *url.URL) *HeaderElement { + for _, cookie := range jar.Cookies(u) { if cookie.Name == SessionCookieName { return &HeaderElement{Value: cookie.Value} } @@ -224,6 +223,22 @@ func (c *Client) SessionCookie() *HeaderElement { return nil } +// SessionCookie returns a SessionCookie with value of the vmware_soap_session http.Cookie. +func (c *Client) SessionCookie() *HeaderElement { + u := c.URL() + + if cookie := sessionCookie(c.Jar, u); cookie != nil { + return cookie + } + + // Default "/sdk" Path would match above, + // but saw a case of Path == "sdk", where above returns nil. + // The jar entry Path is normally "/", so fallback to that. + u.Path = "/" + + return sessionCookie(c.Jar, u) +} + func (c *Client) newServiceClientWithTransport(path string, namespace string, t *http.Transport) *Client { vc := c.URL() u, err := url.Parse(path) diff --git a/vim25/soap/client_test.go b/vim25/soap/client_test.go index 9ccc4758b..3519058a7 100644 --- a/vim25/soap/client_test.go +++ b/vim25/soap/client_test.go @@ -229,3 +229,29 @@ func TestParseURL(t *testing.T) { }) } } + +func TestSessionCookie(t *testing.T) { + u := &url.URL{ + Scheme: "http", + Host: "localhost:1080", + Path: "sdk", // see comment in Client.SessionCookie + } + + c := NewClient(u, true) + + cookie := &http.Cookie{ + Name: SessionCookieName, + Value: "ANY", + Path: "/", + Domain: "localhost", + HttpOnly: true, + Secure: false, + } + + c.Jar.SetCookies(u, []*http.Cookie{cookie}) + + val := c.SessionCookie() + if val == nil { + t.Fatal("no session cookie") + } +}