Open platform to collect and prioritize feedback
force use of application/json as content type (#241)
Guilherme Oenningb562b59eafbd
diff --git a/app/pkg/mock/server.go b/app/pkg/mock/server.goindex 1e9dbf0db..6c56a881d 100644--- a/app/pkg/mock/server.go+++ b/app/pkg/mock/server.go@@ -105,7 +105,7 @@ func (s *Server) ExecutePost(handler web.HandlerFunc, body string) (int, *httptes.context.Request.URL.Path = "/"s.context.Request.Body = ioutil.NopCloser(strings.NewReader(body))s.context.Request.ContentLength = int64(len(body))- s.context.Request.Header.Set("Content-Type", web.JSONContentType)+ s.context.Request.Header.Set("Content-Type", web.UTF8JSONContentType)if err := s.middleware(handler)(s.context); err != nil {s.context.Failure(err)diff --git a/app/pkg/web/binder.go b/app/pkg/web/binder.goindex f62ddd2fd..b4e711f64 100644--- a/app/pkg/web/binder.go+++ b/app/pkg/web/binder.go@@ -2,6 +2,7 @@ package webimport ("encoding/json"+ "errors""net/http""reflect""strconv"@@ -9,8 +10,10 @@ import ()var (- intType = reflect.TypeOf(0)- stringType = reflect.TypeOf("")+ //ErrContentTypeNotAllowed is used when POSTing a body that is not json+ ErrContentTypeNotAllowed = errors.New("Only Content-Type application/json is allowed")+ intType = reflect.TypeOf(0)+ stringType = reflect.TypeOf(""))//DefaultBinder is the default HTTP binder@@ -25,6 +28,11 @@ func NewDefaultBinder() *DefaultBinder {//Bind request data to object ifunc (b *DefaultBinder) Bind(target interface{}, c *Context) error {if c.Request.Method == http.MethodPost && c.Request.ContentLength > 0 {+ contentType := strings.Split(c.Request.Header.Get("Content-Type"), ";")+ if len(contentType) == 0 || contentType[0] != JSONContentType {+ return ErrContentTypeNotAllowed+ }+if err := json.NewDecoder(c.Request.Body).Decode(target); err != nil {return err}diff --git a/app/pkg/web/binder_test.go b/app/pkg/web/binder_test.goindex 6970c9073..9267d60ec 100644--- a/app/pkg/web/binder_test.go+++ b/app/pkg/web/binder_test.go@@ -20,7 +20,7 @@ func TestDefaultBinder_FromParams(t *testing.T) {params := make(web.StringMap, 0)params["number"] = "2"params["slug"] = "jon-snow"- ctx := newPostContext(params, `{ "name": "Jon Snow" }`)+ ctx := newPostContext(params, `{ "name": "Jon Snow" }`, "application/json")u := new(updateUser)err := binder.Bind(u, ctx)Expect(err).To(BeNil())@@ -39,7 +39,9 @@ func TestDefaultBinder_TrimSpaces(t *testing.T) {Other string}- ctx := newPostContext(make(web.StringMap, 0), `{ "name": " Jon Snow ", "email": " [email protected] ", "color": "ff00ad" }`)+ params := make(web.StringMap, 0)+ body := `{ "name": " Jon Snow ", "email": " [email protected] ", "color": "ff00ad" }`+ ctx := newPostContext(params, body, "application/json")u := new(user)err := binder.Bind(u, ctx)Expect(err).To(BeNil())@@ -60,10 +62,25 @@ func TestDefaultBinder_POST_WithoutBody(t *testing.T) {params := make(web.StringMap, 0)params["number"] = "2"params["slug"] = "jon-snow"- ctx := newPostContext(params, "")+ ctx := newPostContext(params, "", web.UTF8JSONContentType)a := new(action)err := binder.Bind(a, ctx)Expect(err).To(BeNil())Expect(a.Number).To(Equal(2))Expect(a.Slug).To(Equal("jon-snow"))}++func TestDefaultBinder_POST_NonJSON(t *testing.T) {+ RegisterTestingT(t)++ type user struct {+ Name string `json:"name"`+ }++ params := make(web.StringMap, 0)+ ctx := newPostContext(params, `name=JonSnow`, web.UTF8HTMLContentType)++ u := new(user)+ err := binder.Bind(u, ctx)+ Expect(err).To(Equal(web.ErrContentTypeNotAllowed))+}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 9b09c0cd2..665b73dba 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -29,9 +29,12 @@ type StringMap map[string]string// HTMLMimeType is the mimetype for HTML responsesvar (- PlainContentType = "text/plain; charset=utf-8"- HTMLContentType = "text/html; charset=utf-8"- JSONContentType = "application/json; charset=utf-8"+ PlainContentType = "text/plain"+ HTMLContentType = "text/html"+ JSONContentType = "application/json"+ UTF8PlainContentType = PlainContentType + "; charset=utf-8"+ UTF8HTMLContentType = HTMLContentType + "; charset=utf-8"+ UTF8JSONContentType = JSONContentType + "; charset=utf-8")// CookieAuthName is the name of the authentication cookie@@ -359,7 +362,7 @@ func (ctx *Context) Set(key string, val interface{}) {// String returns a text response with status code.func (ctx *Context) String(code int, text string) error {- return ctx.Blob(code, PlainContentType, []byte(text))+ return ctx.Blob(code, UTF8PlainContentType, []byte(text))}// JSON returns a JSON response with status code.diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.goindex 6e947691f..aa562430d 100644--- a/app/pkg/web/context_test.go+++ b/app/pkg/web/context_test.go@@ -19,10 +19,11 @@ func newGetContext(params web.StringMap) *web.Context {return &ctx}-func newPostContext(params web.StringMap, body string) *web.Context {+func newPostContext(params web.StringMap, body, contentType string) *web.Context {e := web.New(nil)res := httptest.NewRecorder()req := httptest.NewRequest("POST", "http://demo.test.fider.io:3000/some/resource", strings.NewReader(body))+ req.Header.Set("Content-Type", contentType)ctx := e.NewContext(res, req, params)return &ctx}