mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 07:57:27 +08:00
fix: enhance URL building logic in DriverSession with additional test cases
This commit is contained in:
+21
-6
@@ -104,6 +104,7 @@ func (s *DriverSession) History() []*DriverRequests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *DriverSession) buildURL(urlStr string) (string, error) {
|
func (s *DriverSession) buildURL(urlStr string) (string, error) {
|
||||||
|
// Handle empty URL or root path
|
||||||
if urlStr == "" || urlStr == "/" {
|
if urlStr == "" || urlStr == "/" {
|
||||||
if s.baseUrl == "" {
|
if s.baseUrl == "" {
|
||||||
return "", fmt.Errorf("base URL is empty")
|
return "", fmt.Errorf("base URL is empty")
|
||||||
@@ -111,7 +112,7 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) {
|
|||||||
return s.baseUrl, nil
|
return s.baseUrl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle full URLs
|
// Handle full URLs (absolute URLs)
|
||||||
if strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://") {
|
if strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://") {
|
||||||
u, err := url.Parse(urlStr)
|
u, err := url.Parse(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -120,10 +121,12 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) {
|
|||||||
return u.String(), nil
|
return u.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle relative path using ResolveReference
|
// Validate base URL
|
||||||
if s.baseUrl == "" {
|
if s.baseUrl == "" {
|
||||||
return "", fmt.Errorf("base URL is empty")
|
return "", fmt.Errorf("base URL is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse both base URL and relative URL upfront
|
||||||
baseURL, err := url.Parse(s.baseUrl)
|
baseURL, err := url.Parse(s.baseUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to parse base URL: %w", err)
|
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)
|
return "", fmt.Errorf("failed to parse relative URL: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
finalURL := baseURL.ResolveReference(relativeURL)
|
// Special handling: when relative path starts with "/" and base URL has a non-root path,
|
||||||
return finalURL.String(), nil
|
// 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) {
|
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) (
|
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte) (
|
||||||
rawResp DriverRawResponse, err error) {
|
rawResp DriverRawResponse, err error,
|
||||||
|
) {
|
||||||
var lastError error
|
var lastError error
|
||||||
|
|
||||||
for attempt := 1; attempt <= s.maxRetry; attempt++ {
|
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) (
|
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte) (
|
||||||
rawResp DriverRawResponse, err error) {
|
rawResp DriverRawResponse, err error,
|
||||||
|
) {
|
||||||
// build final URL
|
// build final URL
|
||||||
rawURL, err := s.buildURL(urlStr)
|
rawURL, err := s.buildURL(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -94,6 +94,26 @@ func TestDriverSession_buildURL(t *testing.T) {
|
|||||||
urlStr: "users",
|
urlStr: "users",
|
||||||
want: "http://localhost:8080/api/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 {
|
for _, tt := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user