Skip to content
Myr

fix(context): handle missing HTML renderer (#4805)

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

* fix(context): handle missing HTML renderer * test(render): cover missing HTML renderer error ---------

authored by Langning Zhang · committed by GitHub · parent bc141b4 · Browse files · 4 files+97 added−0 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+97 added−0 removed

  • other:+97 added−0 removed

4 files+97 added−0 removed

modified:render/html.go+6 added
3 unmodified lines
45package render67import (8 "errors"9 "html/template"10 "net/http"1112 "github.com/gin-gonic/gin/internal/fs"
37 unmodified lines
50}5152var htmlContentType = []string{"text/html; charset=utf-8"}5354var errHTMLRendererNotConfigured = errors.New("html renderer is not configured")5556// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.57func (r HTMLProduction) Instance(name string, data any) Render {58 return HTML{59 Template: r.Template,
30 unmodified lines
9091// Render (HTML) executes template and writes its result with custom ContentType for response.92func (r HTML) Render(w http.ResponseWriter) error {93 r.WriteContentType(w)94 if r.Template == nil {95 return errHTMLRendererNotConfigured96 }9798 if r.Name == "" {99 return r.Template.Execute(w, r.Data)100 }
7 unmodified lines
modified:render/render_test.go+10 added
628 unmodified lines
629 assert.Equal(t, "Hello alexandernyquist", w.Body.String())630 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))631}632633func TestRenderHTMLWithoutTemplate(t *testing.T) {634 w := httptest.NewRecorder()635636 err := (HTML{}).Render(w)637638 require.EqualError(t, err, "html renderer is not configured")639 assert.Empty(t, w.Body.String())640 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))641}642643func TestRenderHTMLTemplateEmptyName(t *testing.T) {644 w := httptest.NewRecorder()645 templ := template.Must(template.New("").Parse(`Hello {{.name}}`))646
171 unmodified lines
modified:context_test.go+76 added
1343 unmodified lines
1344 assert.Equal(t, "Hello alexandernyquist", w.Body.String())1345 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))1346}13471348func TestContextRenderHTMLWithoutRenderer(t *testing.T) {1349 tests := []struct {1350 name string1351 method string1352 status int1353 wantErrors []string1354 wantAborted bool1355 wantNextCalled bool1356 }{1357 {1358 name: "body allowed",1359 method: http.MethodGet,1360 status: http.StatusAccepted,1361 wantErrors: []string{"html renderer is not configured"},1362 wantAborted: true,1363 },1364 {1365 name: "no content",1366 method: http.MethodGet,1367 status: http.StatusNoContent,1368 wantNextCalled: true,1369 },1370 {1371 name: "not modified",1372 method: http.MethodGet,1373 status: http.StatusNotModified,1374 wantNextCalled: true,1375 },1376 {1377 name: "head",1378 method: http.MethodHead,1379 status: http.StatusOK,1380 wantErrors: []string{"html renderer is not configured"},1381 wantAborted: true,1382 },1383 }13841385 for _, tt := range tests {1386 t.Run(tt.name, func(t *testing.T) {1387 w := httptest.NewRecorder()1388 router := New()1389 var contextErrors []string1390 var contextAborted bool1391 nextHandlerCalled := false13921393 handlers := []HandlerFunc{1394 func(c *Context) {1395 c.HTML(tt.status, "index.tmpl", H{"title": "Main website"})1396 contextErrors = c.Errors.Errors()1397 contextAborted = c.IsAborted()1398 },1399 func(_ *Context) {1400 nextHandlerCalled = true1401 },1402 }1403 if tt.method == http.MethodHead {1404 router.HEAD("/", handlers...)1405 } else {1406 router.GET("/", handlers...)1407 }14081409 req := httptest.NewRequest(tt.method, "/", nil)1410 require.NotPanics(t, func() {1411 router.ServeHTTP(w, req)1412 })14131414 assert.Equal(t, tt.status, w.Code)1415 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))1416 assert.Equal(t, tt.wantErrors, contextErrors)1417 assert.Equal(t, tt.wantAborted, contextAborted)1418 assert.Equal(t, tt.wantNextCalled, nextHandlerCalled)1419 assert.Empty(t, w.Body.String())1420 })1421 }1422}14231424func TestContextRenderHTML2(t *testing.T) {1425 w := httptest.NewRecorder()1426 c, router := CreateTestContext(w)1427
2606 unmodified lines
modified:context.go+5 added
1217 unmodified lines
1218// HTML renders the HTTP template specified by its file name.1219// It also updates the HTTP code and sets the Content-Type as "text/html".1220// See http://golang.org/doc/articles/wiki/1221func (c *Context) HTML(code int, name string, obj any) {1222 if c.engine.HTMLRender == nil {1223 c.Render(code, render.HTML{})1224 return1225 }12261227 instance := c.engine.HTMLRender.Instance(name, obj)1228 c.Render(code, instance)1229}1230
314 unmodified lines