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>
master
Kevin Wan 3 years ago committed by GitHub
parent 263e426ae1
commit cd289465fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -871,3 +871,50 @@ func TestUnmarshalReaderError(t *testing.T) {
assert.NotNil(t, err)
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():
tw.mu.Lock()
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) {
w.WriteHeader(statusClientClosedRequest)
} else {

@ -25,8 +25,6 @@ var (
pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues(),
mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
emptyMap = map[string]interface{}{}
)
// 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.
func ParseJsonBody(r *http.Request, v interface{}) error {
var reader io.Reader
if withJsonBody(r) {
reader = io.LimitReader(r.Body, maxBodyLen)
reader := io.LimitReader(r.Body, maxBodyLen)
return mapping.UnmarshalJsonReader(reader, v)
}
return mapping.UnmarshalJsonMap(emptyMap, v)
return mapping.UnmarshalJsonMap(nil, v)
}
// ParsePath parses the symbols reside in url path.

@ -196,9 +196,7 @@ Content-Disposition: form-data; name="age"
}
func TestParseJsonBody(t *testing.T) {
t.Run("has body", func(t *testing.T) {
var v struct {
Name string `json:"name"`
Age int `json:"age"`
@ -211,11 +209,9 @@ func TestParseJsonBody(t *testing.T) {
assert.Nil(t, Parse(r, &v))
assert.Equal(t, "kevin", v.Name)
assert.Equal(t, 18, v.Age)
})
t.Run("hasn't body", func(t *testing.T) {
var v struct {
Name string `json:"name,optional"`
Age int `json:"age,optional"`
@ -225,9 +221,7 @@ func TestParseJsonBody(t *testing.T) {
assert.Nil(t, Parse(r, &v))
assert.Equal(t, "", v.Name)
assert.Equal(t, 0, v.Age)
})
}
func TestParseRequired(t *testing.T) {

@ -119,18 +119,10 @@ func VerifySignature(r *http.Request, securityHeader *ContentSecurityHeader, tol
}, "\n")
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 {
return httpx.CodeSignaturePass
}
logx.Infof("signature different, expect: %s, actual: %s",
securityHeader.Signature, actualSignature)

Loading…
Cancel
Save