chore: coding style and comments (#1361)
* chore: coding style and comments * chore: optimize `ParseJsonBody` (#1353) * chore: optimize `ParseJsonBody` * chore: optimize `ParseJsonBody` * fix: fix a test * chore: optimize `ParseJsonBody` * fix a test * chore: add comment * chore: refactor Co-authored-by: chenquan <chenquan.dev@foxmail.com>
This commit is contained in:
@@ -871,3 +871,50 @@ func TestUnmarshalReaderError(t *testing.T) {
|
|||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
assert.True(t, strings.Contains(err.Error(), payload))
|
assert.True(t, strings.Contains(err.Error(), payload))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalMap(t *testing.T) {
|
||||||
|
t.Run("nil map and valid", func(t *testing.T) {
|
||||||
|
var m map[string]interface{}
|
||||||
|
var v struct {
|
||||||
|
Any string `json:",optional"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := UnmarshalJsonMap(m, &v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, len(v.Any) == 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty map but not valid", func(t *testing.T) {
|
||||||
|
m := map[string]interface{}{}
|
||||||
|
var v struct {
|
||||||
|
Any string
|
||||||
|
}
|
||||||
|
|
||||||
|
err := UnmarshalJsonMap(m, &v)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty map and valid", func(t *testing.T) {
|
||||||
|
m := map[string]interface{}{}
|
||||||
|
var v struct {
|
||||||
|
Any string `json:",optional"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := UnmarshalJsonMap(m, &v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, len(v.Any) == 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("valid map", func(t *testing.T) {
|
||||||
|
m := map[string]interface{}{
|
||||||
|
"Any": "foo",
|
||||||
|
}
|
||||||
|
var v struct {
|
||||||
|
Any string
|
||||||
|
}
|
||||||
|
|
||||||
|
err := UnmarshalJsonMap(m, &v)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "foo", v.Any)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -89,6 +89,8 @@ func (h *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
tw.mu.Lock()
|
tw.mu.Lock()
|
||||||
defer tw.mu.Unlock()
|
defer tw.mu.Unlock()
|
||||||
|
// there isn't any user-defined middleware before TimoutHandler,
|
||||||
|
// so we can guarantee that cancelation in biz related code won't come here.
|
||||||
if errors.Is(ctx.Err(), context.Canceled) {
|
if errors.Is(ctx.Err(), context.Canceled) {
|
||||||
w.WriteHeader(statusClientClosedRequest)
|
w.WriteHeader(statusClientClosedRequest)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ var (
|
|||||||
pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
|
pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
|
||||||
headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues(),
|
headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues(),
|
||||||
mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
|
mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
|
||||||
|
|
||||||
emptyMap = map[string]interface{}{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse parses the request.
|
// Parse parses the request.
|
||||||
@@ -107,13 +105,12 @@ func ParseHeader(headerValue string) map[string]string {
|
|||||||
|
|
||||||
// ParseJsonBody parses the post request which contains json in body.
|
// ParseJsonBody parses the post request which contains json in body.
|
||||||
func ParseJsonBody(r *http.Request, v interface{}) error {
|
func ParseJsonBody(r *http.Request, v interface{}) error {
|
||||||
var reader io.Reader
|
|
||||||
if withJsonBody(r) {
|
if withJsonBody(r) {
|
||||||
reader = io.LimitReader(r.Body, maxBodyLen)
|
reader := io.LimitReader(r.Body, maxBodyLen)
|
||||||
return mapping.UnmarshalJsonReader(reader, v)
|
return mapping.UnmarshalJsonReader(reader, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mapping.UnmarshalJsonMap(emptyMap, v)
|
return mapping.UnmarshalJsonMap(nil, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParsePath parses the symbols reside in url path.
|
// ParsePath parses the symbols reside in url path.
|
||||||
|
|||||||
@@ -196,9 +196,7 @@ Content-Disposition: form-data; name="age"
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseJsonBody(t *testing.T) {
|
func TestParseJsonBody(t *testing.T) {
|
||||||
|
|
||||||
t.Run("has body", func(t *testing.T) {
|
t.Run("has body", func(t *testing.T) {
|
||||||
|
|
||||||
var v struct {
|
var v struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Age int `json:"age"`
|
Age int `json:"age"`
|
||||||
@@ -211,11 +209,9 @@ func TestParseJsonBody(t *testing.T) {
|
|||||||
assert.Nil(t, Parse(r, &v))
|
assert.Nil(t, Parse(r, &v))
|
||||||
assert.Equal(t, "kevin", v.Name)
|
assert.Equal(t, "kevin", v.Name)
|
||||||
assert.Equal(t, 18, v.Age)
|
assert.Equal(t, 18, v.Age)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("hasn't body", func(t *testing.T) {
|
t.Run("hasn't body", func(t *testing.T) {
|
||||||
|
|
||||||
var v struct {
|
var v struct {
|
||||||
Name string `json:"name,optional"`
|
Name string `json:"name,optional"`
|
||||||
Age int `json:"age,optional"`
|
Age int `json:"age,optional"`
|
||||||
@@ -225,9 +221,7 @@ func TestParseJsonBody(t *testing.T) {
|
|||||||
assert.Nil(t, Parse(r, &v))
|
assert.Nil(t, Parse(r, &v))
|
||||||
assert.Equal(t, "", v.Name)
|
assert.Equal(t, "", v.Name)
|
||||||
assert.Equal(t, 0, v.Age)
|
assert.Equal(t, 0, v.Age)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseRequired(t *testing.T) {
|
func TestParseRequired(t *testing.T) {
|
||||||
|
|||||||
@@ -119,18 +119,10 @@ func VerifySignature(r *http.Request, securityHeader *ContentSecurityHeader, tol
|
|||||||
}, "\n")
|
}, "\n")
|
||||||
actualSignature := codec.HmacBase64(securityHeader.Key, signContent)
|
actualSignature := codec.HmacBase64(securityHeader.Key, signContent)
|
||||||
|
|
||||||
/*passed := securityHeader.Signature == actualSignature
|
|
||||||
if !passed {
|
|
||||||
logx.Infof("signature different, expect: %s, actual: %s",
|
|
||||||
securityHeader.Signature, actualSignature)
|
|
||||||
}
|
|
||||||
|
|
||||||
if passed {
|
|
||||||
return httpx.CodeSignaturePass
|
|
||||||
}*/
|
|
||||||
if securityHeader.Signature == actualSignature {
|
if securityHeader.Signature == actualSignature {
|
||||||
return httpx.CodeSignaturePass
|
return httpx.CodeSignaturePass
|
||||||
}
|
}
|
||||||
|
|
||||||
logx.Infof("signature different, expect: %s, actual: %s",
|
logx.Infof("signature different, expect: %s, actual: %s",
|
||||||
securityHeader.Signature, actualSignature)
|
securityHeader.Signature, actualSignature)
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func (e env) string() string {
|
|||||||
w.WriteString(fmt.Sprintf("%s = %q\n", k, v))
|
w.WriteString(fmt.Sprintf("%s = %q\n", k, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimSuffix(w.String(),"\n")
|
return strings.TrimSuffix(w.String(), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEnv() env {
|
func getEnv() env {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package bug
|
package bug
|
||||||
|
|
||||||
const issueTemplate=`
|
const issueTemplate = `
|
||||||
<!-- Please answer these questions before submitting your issue. Thanks! -->
|
<!-- Please answer these questions before submitting your issue. Thanks! -->
|
||||||
|
|
||||||
### What category of issue (<code>goctl</code> or <code>sdk</code>)?
|
### What category of issue (<code>goctl</code> or <code>sdk</code>)?
|
||||||
|
|||||||
Reference in New Issue
Block a user