mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 16:29:37 +08:00
fix: 解决单侧语法错误问题
This commit is contained in:
@@ -6,105 +6,52 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDriverSession_concatURL(t *testing.T) {
|
func TestDriverSession_buildURL(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
baseUrl string
|
|
||||||
urlStr string
|
urlStr string
|
||||||
want string
|
want string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
errMsg string
|
errMsg string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty url with empty base url",
|
name: "empty url",
|
||||||
baseUrl: "",
|
|
||||||
urlStr: "",
|
urlStr: "",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
errMsg: "base URL is empty",
|
errMsg: "URL cannot be empty",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty url with valid base url",
|
name: "absolute http url",
|
||||||
baseUrl: "http://localhost:8080",
|
urlStr: "http://example.com/api",
|
||||||
urlStr: "",
|
want: "http://example.com/api",
|
||||||
want: "http://localhost:8080",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "root path with empty base url",
|
name: "absolute https url",
|
||||||
baseUrl: "",
|
urlStr: "https://example.com/api",
|
||||||
urlStr: "/",
|
want: "https://example.com/api",
|
||||||
wantErr: true,
|
|
||||||
errMsg: "base URL is empty",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "root path with valid base url",
|
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "/",
|
|
||||||
want: "http://localhost:8080",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "absolute http url",
|
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "http://example.com/api",
|
|
||||||
want: "http://example.com/api",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "absolute https url",
|
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "https://example.com/api",
|
|
||||||
want: "https://example.com/api",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid absolute url",
|
name: "invalid absolute url",
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "http://[invalid-url",
|
urlStr: "http://[invalid-url",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
errMsg: "failed to parse URL",
|
errMsg: "failed to parse URL",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "relative path with empty base url",
|
name: "relative path",
|
||||||
baseUrl: "",
|
urlStr: "/api/users",
|
||||||
urlStr: "api/users",
|
want: "/api/users",
|
||||||
wantErr: true,
|
|
||||||
errMsg: "base URL is empty",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "relative path with invalid base url",
|
name: "relative path with query params",
|
||||||
baseUrl: "http://[invalid-url",
|
urlStr: "/api/users?id=1&name=test",
|
||||||
urlStr: "api/users",
|
want: "/api/users?id=1&name=test",
|
||||||
wantErr: true,
|
|
||||||
errMsg: "failed to parse base URL",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "relative path with valid base url",
|
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "api/users",
|
|
||||||
want: "http://localhost:8080/api/users",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "relative path with query params",
|
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "api/users?id=1&name=test",
|
|
||||||
want: "http://localhost:8080/api/users?id=1&name=test",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "base url with query params",
|
|
||||||
baseUrl: "http://localhost:8080?token=123",
|
|
||||||
urlStr: "api/users?id=1",
|
|
||||||
want: "http://localhost:8080/api/users?id=1",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid query params",
|
|
||||||
baseUrl: "http://localhost:8080",
|
|
||||||
urlStr: "api/users?id=%invalid",
|
|
||||||
wantErr: true,
|
|
||||||
errMsg: "failed to parse query params",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
s := &DriverSession{baseUrl: tt.baseUrl}
|
s := NewDriverSession()
|
||||||
got, err := s.concatURL(tt.urlStr)
|
got, err := s.buildURL(tt.urlStr)
|
||||||
|
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
@@ -118,25 +65,92 @@ func TestDriverSession_concatURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDriverSession_WithBaseURL(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
baseURL string
|
||||||
|
path string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "base url with root path",
|
||||||
|
baseURL: "http://localhost:8080",
|
||||||
|
path: "/",
|
||||||
|
want: "http://localhost:8080/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "base url with api path",
|
||||||
|
baseURL: "http://localhost:8080",
|
||||||
|
path: "/api/users",
|
||||||
|
want: "http://localhost:8080/api/users",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "base url with path and query params",
|
||||||
|
baseURL: "http://localhost:8080",
|
||||||
|
path: "/api/users?id=1&name=test",
|
||||||
|
want: "http://localhost:8080/api/users?id=1&name=test",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "base url with trailing slash and path",
|
||||||
|
baseURL: "http://localhost:8080/",
|
||||||
|
path: "api/users",
|
||||||
|
want: "http://localhost:8080/api/users",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
s := NewDriverSession()
|
||||||
|
|
||||||
|
// Test that the URL concatenation works as expected
|
||||||
|
fullURL := tt.baseURL + tt.path
|
||||||
|
assert.Equal(t, tt.want, fullURL)
|
||||||
|
|
||||||
|
// Test that buildURL handles the resulting full URL correctly
|
||||||
|
if fullURL != "" {
|
||||||
|
got, err := s.buildURL(fullURL)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDriverSession(t *testing.T) {
|
func TestDriverSession(t *testing.T) {
|
||||||
session := NewDriverSession()
|
session := NewDriverSession()
|
||||||
|
|
||||||
|
// Test backward compatibility with SetBaseURL (should not error)
|
||||||
session.SetBaseURL("https://postman-echo.com")
|
session.SetBaseURL("https://postman-echo.com")
|
||||||
resp, err := session.GET("/get")
|
|
||||||
|
// Test GET with full URL
|
||||||
|
resp, err := session.GET("https://postman-echo.com/get")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
t.Log(resp)
|
t.Log(resp)
|
||||||
|
|
||||||
resp, err = session.GET("/get?a=1&b=2")
|
// Test GET with full URL and query params
|
||||||
|
resp, err = session.GET("https://postman-echo.com/get?a=1&b=2")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
t.Log(resp)
|
||||||
|
|
||||||
|
// Test GETWithBaseURL
|
||||||
|
baseURL := "https://postman-echo.com"
|
||||||
|
resp, err = session.GETWithBaseURL(baseURL, "/get")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
t.Log(resp)
|
t.Log(resp)
|
||||||
|
|
||||||
driverRequests := session.History()
|
driverRequests := session.History()
|
||||||
assert.Equal(t, 2, len(driverRequests))
|
assert.Equal(t, 3, len(driverRequests))
|
||||||
|
|
||||||
session.Reset()
|
session.Reset()
|
||||||
driverRequests = session.History()
|
driverRequests = session.History()
|
||||||
assert.Equal(t, 0, len(driverRequests))
|
assert.Equal(t, 0, len(driverRequests))
|
||||||
|
|
||||||
resp, err = session.GET("https://postman-echo.com/get")
|
// Test POST with base URL and path
|
||||||
|
resp, err = session.POSTWithBaseURL(nil, baseURL, "/post")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
t.Log(resp)
|
t.Log(resp)
|
||||||
|
|
||||||
|
// Verify one request was made after reset
|
||||||
|
driverRequests = session.History()
|
||||||
|
assert.Equal(t, 1, len(driverRequests))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user