Skip to content

Commit

Permalink
Merge pull request #20 from tonny-zhang/develop
Browse files Browse the repository at this point in the history
delete auto deal application/json
  • Loading branch information
tonny-zhang authored Sep 7, 2021
2 parents 27f9ebb + 52a969e commit e601f15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
31 changes: 4 additions & 27 deletions context_post.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cotton

import (
"encoding/json"
"io"
"mime/multipart"
"net/http"
Expand All @@ -13,34 +12,12 @@ import (
func (ctx *Context) initPostFormCache() {
if nil == ctx.postFormCache {
if nil != ctx.Request {
ct := ctx.GetRequestHeader("Content-Type")
if ct == "application/json" {
body := ctx.Request.Body
if body != nil {
values := url.Values{}
obj := make(map[string]interface{})
json.NewDecoder(body).Decode(&obj)
for k, v := range obj {
var valStr string
switch v.(type) {
case string:
valStr = v.(string)
default:
b, _ := json.Marshal(v)
valStr = string(b)
}
values[k] = []string{valStr}
}
ctx.postFormCache = values
if e := ctx.Request.ParseMultipartForm(defaultMultipartMemory); e != nil {
if e != http.ErrNotMultipart {
panic(e)
}
} else {
if e := ctx.Request.ParseMultipartForm(defaultMultipartMemory); e != nil {
if e != http.ErrNotMultipart {
panic(e)
}
}
ctx.postFormCache = ctx.Request.PostForm
}
ctx.postFormCache = ctx.Request.PostForm
} else {
ctx.postFormCache = url.Values{}
}
Expand Down
11 changes: 11 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import (
"github.com/stretchr/testify/assert"
)

var handlerEmpty = func(c *Context) {}

func TestDiffentParamNameInSamePos(t *testing.T) {
// same postion param name must be same
assert.PanicsWithError(t, "[:name] in path [/v1/abc/abc/:name/:id] conflicts with [/v1/abc/abc/:id]", func() {
router := NewRouter()
g := router.Group("/v1/abc/")
g.Post("/abc/:id", handlerEmpty)
g.Post("/abc/:name/:id", handlerEmpty)
})
}
func TestAddHandleFunc(t *testing.T) {
handler := func(c *Context) {}
assert.PanicsWithError(t, "[hello] shold absolute path", func() {
Expand Down

0 comments on commit e601f15

Please sign in to comment.