mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-08 07:03:40 +08:00
refactor: session request url str, fix lazy setup
This commit is contained in:
@@ -87,8 +87,8 @@ func (s *DriverSession) History() []*DriverRequests {
|
||||
return s.requests
|
||||
}
|
||||
|
||||
func (s *DriverSession) concatURL(elem ...string) (string, error) {
|
||||
if len(elem) == 0 {
|
||||
func (s *DriverSession) concatURL(urlStr string) (string, error) {
|
||||
if urlStr == "" || urlStr == "/" {
|
||||
if s.baseUrl == "" {
|
||||
return "", fmt.Errorf("base URL is empty")
|
||||
}
|
||||
@@ -96,14 +96,11 @@ func (s *DriverSession) concatURL(elem ...string) (string, error) {
|
||||
}
|
||||
|
||||
// 处理完整 URL
|
||||
if strings.HasPrefix(elem[0], "http://") || strings.HasPrefix(elem[0], "https://") {
|
||||
u, err := url.Parse(elem[0])
|
||||
if strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://") {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse URL: %w", err)
|
||||
}
|
||||
if len(elem) > 1 {
|
||||
u.Path = path.Join(u.Path, path.Join(elem[1:]...))
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
@@ -116,55 +113,25 @@ func (s *DriverSession) concatURL(elem ...string) (string, error) {
|
||||
return "", fmt.Errorf("failed to parse base URL: %w", err)
|
||||
}
|
||||
|
||||
// 保存原始查询参数
|
||||
baseQuery := u.Query()
|
||||
|
||||
// 处理路径和查询参数
|
||||
var paths []string
|
||||
for i, e := range elem {
|
||||
if i == len(elem)-1 {
|
||||
// 处理最后一个元素的查询参数
|
||||
parts := strings.SplitN(e, "?", 2)
|
||||
paths = append(paths, parts[0])
|
||||
if len(parts) > 1 {
|
||||
newQuery, err := url.ParseQuery(parts[1])
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse query params: %w", err)
|
||||
}
|
||||
// 合并查询参数
|
||||
for k, v := range newQuery {
|
||||
baseQuery[k] = v
|
||||
}
|
||||
}
|
||||
} else {
|
||||
paths = append(paths, e)
|
||||
parts := strings.SplitN(urlStr, "?", 2)
|
||||
u.Path = path.Join(u.Path, parts[0])
|
||||
if len(parts) > 1 {
|
||||
query, err := url.ParseQuery(parts[1])
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse query params: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 合并路径
|
||||
u.Path = path.Join(append([]string{u.Path}, paths...)...)
|
||||
|
||||
// 设置合并后的查询参数
|
||||
if len(baseQuery) > 0 {
|
||||
u.RawQuery = baseQuery.Encode()
|
||||
u.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func (s *DriverSession) GET(pathElem ...string) (rawResp DriverRawResponse, err error) {
|
||||
urlStr, err := s.concatURL(pathElem...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (s *DriverSession) GET(urlStr string) (rawResp DriverRawResponse, err error) {
|
||||
return s.RequestWithRetry(http.MethodGet, urlStr, nil)
|
||||
}
|
||||
|
||||
func (s *DriverSession) POST(data interface{}, pathElem ...string) (rawResp DriverRawResponse, err error) {
|
||||
urlStr, err := s.concatURL(pathElem...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (s *DriverSession) POST(data interface{}, urlStr string) (rawResp DriverRawResponse, err error) {
|
||||
var bsJSON []byte = nil
|
||||
if data != nil {
|
||||
if bsJSON, err = json.Marshal(data); err != nil {
|
||||
@@ -174,18 +141,14 @@ func (s *DriverSession) POST(data interface{}, pathElem ...string) (rawResp Driv
|
||||
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON)
|
||||
}
|
||||
|
||||
func (s *DriverSession) DELETE(pathElem ...string) (rawResp DriverRawResponse, err error) {
|
||||
urlStr, err := s.concatURL(pathElem...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (s *DriverSession) DELETE(urlStr string) (rawResp DriverRawResponse, err error) {
|
||||
return s.RequestWithRetry(http.MethodDelete, urlStr, nil)
|
||||
}
|
||||
|
||||
func (s *DriverSession) RequestWithRetry(method string, rawURL string, rawBody []byte) (
|
||||
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte) (
|
||||
rawResp DriverRawResponse, err error) {
|
||||
for count := 1; count <= s.maxRetry; count++ {
|
||||
rawResp, err = s.Request(method, rawURL, rawBody)
|
||||
rawResp, err = s.Request(method, urlStr, rawBody)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
@@ -205,8 +168,15 @@ func (s *DriverSession) RequestWithRetry(method string, rawURL string, rawBody [
|
||||
return
|
||||
}
|
||||
|
||||
func (s *DriverSession) Request(method string, rawURL string, rawBody []byte) (
|
||||
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte) (
|
||||
rawResp DriverRawResponse, err error) {
|
||||
|
||||
// concat url with base url
|
||||
rawURL, err := s.concatURL(urlStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driverResult := &DriverRequests{
|
||||
RequestMethod: method,
|
||||
RequestUrl: rawURL,
|
||||
|
||||
Reference in New Issue
Block a user