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 2225b95e3e
3 changed files with 53 additions and 23 deletions

View File

@ -1,18 +1,36 @@
package http
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"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 {
NestedTest *request `json:"nested_test"`
Name string `json:"name"`
Field1 string `json:"field1"`
ClientID string
Field2 string
Field3 int64
NestedTest *request `json:"nested_test,omitempty"`
Name string `json:"name,omitempty"`
Field1 string `json:"field1,omitempty"`
ClientID string `json:",omitempty"`
Field2 string `json:",omitempty"`
Field3 int64 `json:",omitempty"`
}
func TestNestedPath(t *testing.T) {
@ -25,10 +43,14 @@ func TestNestedPath(t *testing.T) {
if err != nil {
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)
}
_ = 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) {

25
util.go
View File

@ -195,24 +195,23 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
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 {
if err = rutil.ZeroFieldByPath(nmsg, k); err != nil {
return "", nil, err
}
}
if val.Type().Kind() == reflect.Slice {
for idx := 0; idx < val.Len(); idx++ {
values.Add(t.name, getParam(val.Index(idx)))
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 {
for idx := 0; idx < val.Len(); idx++ {
values.Add(t.name, getParam(val.Index(idx)))
}
} else if !rutil.IsEmpty(val) {
values.Add(t.name, getParam(val))
}
} else if !rutil.IsEmpty(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())
}
// rutil.ZeroEmpty(tnmsg.Interface())
if rutil.IsZero(nmsg) && !isEmptyStruct(nmsg) {
return b.String(), nil, nil
}

View File

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