try to fixup

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-05-20 08:55:24 +03:00
parent b76b8d3114
commit d6845b88f7
3 changed files with 53 additions and 23 deletions

View File

@ -1,18 +1,36 @@
package http package http
import ( import (
"encoding/json"
"fmt"
"net/url" "net/url"
"strings" "strings"
"testing" "testing"
) )
func TestNestedPathPost(t *testing.T) {
req := &request{Name: "first", Field1: "fieldval"}
p, m, err := newPathRequest("/api/v1/xxxx", "POST", "*", req, []string{"json", "protobuf"}, nil)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(p)
if err != nil {
t.Fatal(err)
}
if s := u.String(); s != "/api/v1/xxxx" {
t.Fatalf("nested path error %s", s)
}
_ = m
}
type request struct { type request struct {
NestedTest *request `json:"nested_test"` NestedTest *request `json:"nested_test,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Field1 string `json:"field1"` Field1 string `json:"field1,omitempty"`
ClientID string ClientID string `json:",omitempty"`
Field2 string Field2 string `json:",omitempty"`
Field3 int64 Field3 int64 `json:",omitempty"`
} }
func TestNestedPath(t *testing.T) { func TestNestedPath(t *testing.T) {
@ -25,10 +43,14 @@ func TestNestedPath(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if s := u.String(); s != "/api/v1/first/second?field1=fieldval" { if s := u.String(); s != "/api/v1/first/second" {
t.Fatalf("nested path error %s", s) t.Fatalf("nested path error %s", s)
} }
_ = m b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
fmt.Printf("m %#+v %s\n", m, b)
} }
func TestPathWithHeader(t *testing.T) { func TestPathWithHeader(t *testing.T) {

15
util.go
View File

@ -195,18 +195,16 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
cleanPath[strings.Join(clean, ".")] = true cleanPath[strings.Join(clean, ".")] = true
} }
} }
if (body == "*" || body == t.name) && method != http.MethodGet {
if tnmsg.Field(i).CanSet() {
tnmsg.Field(i).Set(val)
}
}
for k := range cleanPath { for k := range cleanPath {
if err = rutil.ZeroFieldByPath(nmsg, k); err != nil { if err = rutil.ZeroFieldByPath(nmsg, k); err != nil {
return "", nil, err return "", nil, err
} }
} }
if (body == "*" || body == t.name) && method != http.MethodGet {
if tnmsg.Field(i).CanSet() {
tnmsg.Field(i).Set(val)
}
} else if method == http.MethodGet {
if val.Type().Kind() == reflect.Slice { if val.Type().Kind() == reflect.Slice {
for idx := 0; idx < val.Len(); idx++ { for idx := 0; idx < val.Len(); idx++ {
values.Add(t.name, getParam(val.Index(idx))) values.Add(t.name, getParam(val.Index(idx)))
@ -214,6 +212,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
} else if !rutil.IsEmpty(val) { } else if !rutil.IsEmpty(val) {
values.Add(t.name, getParam(val)) values.Add(t.name, getParam(val))
} }
}
} }
} }
@ -266,6 +265,8 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
_, _ = b.WriteString(values.Encode()) _, _ = b.WriteString(values.Encode())
} }
// rutil.ZeroEmpty(tnmsg.Interface())
if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) { if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) {
return b.String(), nil, nil return b.String(), nil, nil
} }

View File

@ -1,6 +1,7 @@
package http package http
import ( import (
"net/http"
"net/url" "net/url"
"testing" "testing"
) )
@ -52,11 +53,17 @@ func TestNewPathRequest(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
switch m {
case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
break
case http.MethodGet:
vals := u.Query() vals := u.Query()
if v, ok := vals["name"]; !ok || v[0] != "test_name" { if v, ok := vals["name"]; !ok || v[0] != "test_name" {
t.Fatalf("invalid path: %v nmsg: %v", path, nmsg) t.Fatalf("%s invalid path: %v nmsg: %v", m, path, nmsg)
} }
} }
}
} }
func TestNewPathRequestWithEmptyBody(t *testing.T) { func TestNewPathRequestWithEmptyBody(t *testing.T) {