From a71acffa5bc8b17a20828e657656dece9f0ea84a Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Sat, 28 Jun 2025 20:25:28 +0800 Subject: [PATCH] fix: enhance URL building logic in DriverSession with additional test cases --- uixt/driver_session.go | 27 +++++++++++++++++++++------ uixt/driver_session_test.go | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/uixt/driver_session.go b/uixt/driver_session.go index b929a060..82eb801c 100644 --- a/uixt/driver_session.go +++ b/uixt/driver_session.go @@ -104,6 +104,7 @@ func (s *DriverSession) History() []*DriverRequests { } func (s *DriverSession) buildURL(urlStr string) (string, error) { + // Handle empty URL or root path if urlStr == "" || urlStr == "/" { if s.baseUrl == "" { return "", fmt.Errorf("base URL is empty") @@ -111,7 +112,7 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) { return s.baseUrl, nil } - // Handle full URLs + // Handle full URLs (absolute URLs) if strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://") { u, err := url.Parse(urlStr) if err != nil { @@ -120,10 +121,12 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) { return u.String(), nil } - // handle relative path using ResolveReference + // Validate base URL if s.baseUrl == "" { return "", fmt.Errorf("base URL is empty") } + + // Parse both base URL and relative URL upfront baseURL, err := url.Parse(s.baseUrl) if err != nil { return "", fmt.Errorf("failed to parse base URL: %w", err) @@ -134,8 +137,18 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) { return "", fmt.Errorf("failed to parse relative URL: %w", err) } - finalURL := baseURL.ResolveReference(relativeURL) - return finalURL.String(), nil + // Special handling: when relative path starts with "/" and base URL has a non-root path, + // we want to append to base path instead of replacing it + if strings.HasPrefix(urlStr, "/") && baseURL.Path != "" && baseURL.Path != "/" { + finalURL := *baseURL + finalURL.Path = strings.TrimSuffix(baseURL.Path, "/") + relativeURL.Path + finalURL.RawQuery = relativeURL.RawQuery + finalURL.Fragment = relativeURL.Fragment + return finalURL.String(), nil + } + + // Use standard URL resolution for all other cases + return baseURL.ResolveReference(relativeURL).String(), nil } func (s *DriverSession) GET(urlStr string) (rawResp DriverRawResponse, err error) { @@ -157,7 +170,8 @@ func (s *DriverSession) DELETE(urlStr string) (rawResp DriverRawResponse, err er } func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte) ( - rawResp DriverRawResponse, err error) { + rawResp DriverRawResponse, err error, +) { var lastError error for attempt := 1; attempt <= s.maxRetry; attempt++ { @@ -197,7 +211,8 @@ func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody [ } func (s *DriverSession) Request(method string, urlStr string, rawBody []byte) ( - rawResp DriverRawResponse, err error) { + rawResp DriverRawResponse, err error, +) { // build final URL rawURL, err := s.buildURL(urlStr) if err != nil { diff --git a/uixt/driver_session_test.go b/uixt/driver_session_test.go index 84b296f8..83f2f48b 100644 --- a/uixt/driver_session_test.go +++ b/uixt/driver_session_test.go @@ -94,6 +94,26 @@ func TestDriverSession_buildURL(t *testing.T) { urlStr: "users", want: "http://localhost:8080/api/users", }, + // Reproduction case: base URL with path + absolute path (leading slash) + { + name: "baseUrl with path + absolute path should preserve base path", + baseURL: "http://forward-to-38153:6790/wd/hub", + urlStr: "/session", + want: "http://forward-to-38153:6790/wd/hub/session", + }, + // Additional test cases for comprehensive coverage + { + name: "baseUrl with path + absolute path with query params", + baseURL: "http://localhost:8080/api/v1", + urlStr: "/session?timeout=30", + want: "http://localhost:8080/api/v1/session?timeout=30", + }, + { + name: "baseUrl with path + absolute path with query params and fragment", + baseURL: "http://localhost:8080/wd/hub", + urlStr: "/session/123?param=value#fragment", + want: "http://localhost:8080/wd/hub/session/123?param=value#fragment", + }, } for _, tt := range tests {