Skip to content

Commit

Permalink
add mimepostform support
Browse files Browse the repository at this point in the history
  • Loading branch information
tpkeeper committed Apr 11, 2019
1 parent 2e1f1c1 commit b5bc528
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Very helpful for debugging your applications.
## Content-type support / todo

* [x] application/json
* [x] application/x-www-form-urlencoded
* [ ] text/xml
* [ ] application/xml
* [ ] text/plain
Expand Down
59 changes: 50 additions & 9 deletions gindump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"mime"
"net/http"
"net/url"
"strings"
)

Expand All @@ -27,23 +29,43 @@ func Dump() gin.HandlerFunc {
}
rdr := ioutil.NopCloser(bytes.NewBuffer(buf))
ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
ctGet := ctx.Request.Header.Get("Content-Type")
ct, _, err := mime.ParseMediaType(ctGet)
if err != nil {
strB.WriteString(fmt.Sprintf("\ncontent_type: %s parse err \n %s", ctGet, err.Error()))
goto DumpRes
}

switch strings.Split(ctx.Request.Header.Get("Content-Type"), ";")[0] {
switch ct {
case gin.MIMEJSON:
var mapReq map[string]interface{}
bytes, err := ioutil.ReadAll(rdr)
bts, err := ioutil.ReadAll(rdr)
if err != nil {
strB.WriteString(fmt.Sprintf("\nread rdr err \n %s", err.Error()))
goto DumpRes
}
if err := json.Unmarshal(bytes, &mapReq); err != nil {
if err := json.Unmarshal(bts, &mapReq); err != nil {
strB.WriteString(fmt.Sprintf("\nparse bodyCache err \n" + err.Error()))
goto DumpRes
}

strB.WriteString("\nRequest-Body:\n")
strB.WriteString(strMap(mapReq))
case gin.MIMEPOSTForm:
bts, err := ioutil.ReadAll(rdr)
if err != nil {
strB.WriteString(fmt.Sprintf("\nread rdr err \n %s", err.Error()))
goto DumpRes
}
val, err := url.ParseQuery(string(bts))
valMap := (map[string][]string)(val)
if err != nil {
strB.WriteString(fmt.Sprintf("\nparse formdata err \n %s", err.Error()))
goto DumpRes
}
strB.WriteString("\nRequest-Body:\n")
strB.WriteString(strMap(valMap))

case gin.MIMEMultipartPOSTForm:
case gin.MIMEHTML:
default:
Expand All @@ -66,7 +88,13 @@ func Dump() gin.HandlerFunc {

//dump res body
if bodyAllowedForStatus(ctx.Writer.Status()) && bw.bodyCache.Len() > 0 {
switch strings.Split(ctx.Writer.Header().Get("Content-Type"), ";")[0] {
ctGet := ctx.Writer.Header().Get("Content-Type")
ct, _, err := mime.ParseMediaType(ctGet)
if err != nil {
strB.WriteString(fmt.Sprintf("\ncontent-type: %s parse err \n %s", ctGet, err.Error()))
goto End
}
switch ct {
case gin.MIMEJSON:
var mapRes map[string]interface{}
if err := json.Unmarshal(bw.bodyCache.Bytes(), &mapRes); err != nil {
Expand Down Expand Up @@ -97,13 +125,26 @@ func strHeader(header http.Header) string {
return strB.String()
}

func strMap(m map[string]interface{}) string {
func strMap(m interface{}) string {
var strB strings.Builder
strB.WriteString(" {\n")
for key, value := range m {
strB.WriteString(fmt.Sprintf(" %s : %s\n", key, value))
switch m.(type) {
case map[string]interface{}:
mInter := m.(map[string]interface{})
strB.WriteString(" {\n")
for key, value := range mInter {
strB.WriteString(fmt.Sprintf(" %s : %s\n", key, value))
}
strB.WriteString(" }\n")
break
case map[string][]string:
mSl := m.(map[string][]string)
strB.WriteString(" {\n")
for key, value := range mSl {
strB.WriteString(fmt.Sprintf(" %s : %s\n", key, value))
}
strB.WriteString(" }\n")
break
}
strB.WriteString(" }\n")
return strB.String()
}

Expand Down
31 changes: 28 additions & 3 deletions gindump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)

func init() {
gin.SetMode(gin.TestMode)
}

func performRequest(r http.Handler, method, path string,body io.Reader) *httptest.ResponseRecorder {
func performRequest(r http.Handler, method,contentType string ,path string,body io.Reader) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, body)
req.Header.Set("Content-Type",contentType)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}

func TestMIMJSON(t *testing.T) {
func TestMIMEJSON(t *testing.T) {
router := gin.New()
router.Use(Dump())

Expand Down Expand Up @@ -50,6 +53,28 @@ func TestMIMJSON(t *testing.T) {
}

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

}

func TestMIMEPOSTFORM(t *testing.T) {
router := gin.New()
router.Use(Dump())

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

form := make(url.Values)
form.Set("foo", "bar")
form.Add("foo", "bar2")
form.Set("bar", "baz")


body:=strings.NewReader(form.Encode())
performRequest(router, "POST",gin.MIMEPOSTForm ,"/dump",body)

}

0 comments on commit b5bc528

Please sign in to comment.