Skip to content
Myr

Stop useless panicking in context and render (#2150)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Vladislav Dmitriyev · committed by GitHub · parent 153b229 · Browse files · 4 files+16 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

4 files+16 added−19 removed

  • other:+16 added−19 removed

4 files+16 added−19 removed

modified:render/json.go+2 added-5 removed
51 unmodified lines
52 jsonASCIIContentType = []string{"application/json"}53)5455// Render (JSON) writes data with custom ContentType.56func (r JSON) Render(w http.ResponseWriter) (err error) {57 if err = WriteJSON(w, r.Data); err != nil {58 panic(err)59 }60 return56func (r JSON) Render(w http.ResponseWriter) error {57 return WriteJSON(w, r.Data)58}5960// WriteContentType (JSON) writes JSON ContentType.61func (r JSON) WriteContentType(w http.ResponseWriter) {
129 unmodified lines
modified:render/render_test.go+2 added-2 removed
38 unmodified lines
39 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())40 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))41}4243func TestRenderJSONPanics(t *testing.T) {43func TestRenderJSONError(t *testing.T) {44 w := httptest.NewRecorder()45 data := make(chan int)4647 // json: unsupported type: chan int48 assert.Panics(t, func() { assert.NoError(t, (JSON{data}).Render(w)) })48 assert.Error(t, (JSON{data}).Render(w))49}5051func TestRenderIndentedJSON(t *testing.T) {52 w := httptest.NewRecorder()
482 unmodified lines
modified:context_test.go+9 added-11 removed
30 unmodified lines
31)3233var _ context.Context = (*Context)(nil)3435var errTestRender = errors.New("TestRender")3637// Unit tests TODO38// func (c *Context) File(filepath string) {39// func (c *Context) Negotiate(code int, config Negotiate) {40// BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {
603 unmodified lines
644 assert.False(t, false, bodyAllowedForStatus(http.StatusNotModified))645 assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))646}647646type TestPanicRender struct{}648type TestRender struct{}649648func (*TestPanicRender) Render(http.ResponseWriter) error {649 return errors.New("TestPanicRender")650func (*TestRender) Render(http.ResponseWriter) error {651 return errTestRender652}653652func (*TestPanicRender) WriteContentType(http.ResponseWriter) {}654func (*TestRender) WriteContentType(http.ResponseWriter) {}655654func TestContextRenderPanicIfErr(t *testing.T) {655 defer func() {656 r := recover()657 assert.Equal(t, "TestPanicRender", fmt.Sprint(r))658 }()656func TestContextRenderIfErr(t *testing.T) {657 w := httptest.NewRecorder()658 c, _ := CreateTestContext(w)659662 c.Render(http.StatusOK, &TestPanicRender{})660 c.Render(http.StatusOK, &TestRender{})661664 assert.Fail(t, "Panic not detected")662 assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors)663}664665// Tests that the response is serialized as JSON666// and Content-Type is set to application/json
1710 unmodified lines
modified:context.go+3 added-1 removed
922 unmodified lines
923 return924 }925926 if err := r.Render(c.Writer); err != nil {927 panic(err)927 // Pushing error to c.Errors928 _ = c.Error(err)929 c.Abort()930 }931}932933// HTML renders the HTTP template specified by its file name.
287 unmodified lines