Skip to content

Commit

Permalink
add WithOption test
Browse files Browse the repository at this point in the history
  • Loading branch information
GladysKellogg committed Aug 29, 2019
1 parent 489f3ff commit 866efe7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
8 changes: 4 additions & 4 deletions gindump.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func Dump(cb func(dumpStr string)) gin.HandlerFunc {
}

func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders bool, showCookies bool, cb func(dumpStr string)) gin.HandlerFunc {
headerHiddenFields := []string{}
bodyHiddenFields := []string{}
headerHiddenFields := make([]string,0)
bodyHiddenFields := make([]string,0)

if !showCookies {
headerHiddenFields = append(headerHiddenFields, "cookie")
Expand Down Expand Up @@ -64,7 +64,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo
goto DumpRes
}

s, err := FormatJsonBytes(bts, bodyHiddenFields)
s, err := BeautifyJsonBytes(bts, bodyHiddenFields)
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse req body err \n" + err.Error()))
goto DumpRes
Expand Down Expand Up @@ -127,7 +127,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo
switch ct {
case gin.MIMEJSON:

s, err := FormatJsonBytes(bw.bodyCache.Bytes(), bodyHiddenFields)
s, err := BeautifyJsonBytes(bw.bodyCache.Bytes(), bodyHiddenFields)
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse bodyCache err \n" + err.Error()))
goto End
Expand Down
34 changes: 34 additions & 0 deletions gindump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ func TestMIMEJSON(t *testing.T) {
performRequest(router, "POST", gin.MIMEJSON, "/dump", body)

}
func TestMIMEJSONWithOption(t *testing.T) {
router := gin.New()
router.Use(DumpWithOptions(true,false,true,true,false,func(dumpStr string) {
fmt.Println(dumpStr)
}))

router.POST("/dump", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ok": true,
"data": "gin-dump",
})
})

type params struct {
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
}

var httpdata = params{
StartTime: "2019-03-03",
EndTime: "2019-03-03",
}
b, err := json.Marshal(httpdata)
if err != nil {
fmt.Println("json format error:", err)
return
}

body := bytes.NewBuffer(b)
performRequest(router, "POST", gin.MIMEJSON, "/dump", body)

}



func TestMIMEPOSTFORM(t *testing.T) {
router := gin.New()
Expand Down
8 changes: 5 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ var StringMaxLength = 0
var Newline = "\n"
var Indent = 4

func FormatJsonBytes(data []byte, hiddenFields []string) ([]byte, error) {


func BeautifyJsonBytes(data []byte, hiddenFields []string) ([]byte, error) {
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
return nil, err
Expand All @@ -24,13 +26,13 @@ func FormatJsonBytes(data []byte, hiddenFields []string) ([]byte, error) {
return []byte(format(v, 1)), nil
}

//support
//transfer v to json bytes
func FormatToJson(v interface{}, hiddenFields []string) ([]byte, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
return FormatJsonBytes(data, hiddenFields)
return BeautifyJsonBytes(data, hiddenFields)
}

func format(v interface{}, depth int) string {
Expand Down

0 comments on commit 866efe7

Please sign in to comment.