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
Provenance
- Commit
5c6a15f- Tree
df99b13- Author
Langning Zhang <zln1905391059@163.com>- Committer
GitHub <noreply@github.com>- Parent
bc141b4
4 files changed+97 added−0 removed
modified:render/html.go+6 added
3 unmodified lines4 5 package render6 7 import (8+ "errors"9 "html/template"10 "net/http"11 12 "github.com/gin-gonic/gin/internal/fs"37 unmodified lines50 }51 52 var htmlContentType = []string{"text/html; charset=utf-8"}53 54+var errHTMLRendererNotConfigured = errors.New("html renderer is not configured")55+56 // Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.57 func (r HTMLProduction) Instance(name string, data any) Render {58 return HTML{59 Template: r.Template,30 unmodified lines90 91 // Render (HTML) executes template and writes its result with custom ContentType for response.92 func (r HTML) Render(w http.ResponseWriter) error {93 r.WriteContentType(w)94+ if r.Template == nil {95+ return errHTMLRendererNotConfigured96+ }97 98 if r.Name == "" {99 return r.Template.Execute(w, r.Data)100 }7 unmodified linesmodified:render/render_test.go+10 added
628 unmodified lines629 assert.Equal(t, "Hello alexandernyquist", w.Body.String())630 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))631 }632 633+func TestRenderHTMLWithoutTemplate(t *testing.T) {634+ w := httptest.NewRecorder()635+636+ err := (HTML{}).Render(w)637+638+ 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+}642+643 func TestRenderHTMLTemplateEmptyName(t *testing.T) {644 w := httptest.NewRecorder()645 templ := template.Must(template.New("").Parse(`Hello {{.name}}`))646 171 unmodified linesmodified:context_test.go+76 added
1343 unmodified lines1344 assert.Equal(t, "Hello alexandernyquist", w.Body.String())1345 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))1346 }1347 1348+func 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+ }1384+1385+ 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 := false1392+1393+ 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+ }1408+1409+ req := httptest.NewRequest(tt.method, "/", nil)1410+ require.NotPanics(t, func() {1411+ router.ServeHTTP(w, req)1412+ })1413+1414+ 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+}1423+1424 func TestContextRenderHTML2(t *testing.T) {1425 w := httptest.NewRecorder()1426 c, router := CreateTestContext(w)1427 2606 unmodified linesmodified:context.go+5 added
1217 unmodified lines1218 // 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/1221 func (c *Context) HTML(code int, name string, obj any) {1222+ if c.engine.HTMLRender == nil {1223+ c.Render(code, render.HTML{})1224+ return1225+ }1226+1227 instance := c.engine.HTMLRender.Instance(name, obj)1228 c.Render(code, instance)1229 }1230 314 unmodified lines