mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 03:23:36 +08:00
fix(context): handle missing HTML renderer (#4805)
* fix(context): handle missing HTML renderer * test(render): cover missing HTML renderer error --------- Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
bc141b4dbf
commit
5c6a15f8f9
@ -1219,6 +1219,11 @@ func (c *Context) Render(code int, r render.Render) {
|
||||
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
||||
// See http://golang.org/doc/articles/wiki/
|
||||
func (c *Context) HTML(code int, name string, obj any) {
|
||||
if c.engine.HTMLRender == nil {
|
||||
c.Render(code, render.HTML{})
|
||||
return
|
||||
}
|
||||
|
||||
instance := c.engine.HTMLRender.Instance(name, obj)
|
||||
c.Render(code, instance)
|
||||
}
|
||||
|
||||
@ -1345,6 +1345,82 @@ func TestContextRenderHTML(t *testing.T) {
|
||||
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestContextRenderHTMLWithoutRenderer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
method string
|
||||
status int
|
||||
wantErrors []string
|
||||
wantAborted bool
|
||||
wantNextCalled bool
|
||||
}{
|
||||
{
|
||||
name: "body allowed",
|
||||
method: http.MethodGet,
|
||||
status: http.StatusAccepted,
|
||||
wantErrors: []string{"html renderer is not configured"},
|
||||
wantAborted: true,
|
||||
},
|
||||
{
|
||||
name: "no content",
|
||||
method: http.MethodGet,
|
||||
status: http.StatusNoContent,
|
||||
wantNextCalled: true,
|
||||
},
|
||||
{
|
||||
name: "not modified",
|
||||
method: http.MethodGet,
|
||||
status: http.StatusNotModified,
|
||||
wantNextCalled: true,
|
||||
},
|
||||
{
|
||||
name: "head",
|
||||
method: http.MethodHead,
|
||||
status: http.StatusOK,
|
||||
wantErrors: []string{"html renderer is not configured"},
|
||||
wantAborted: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
router := New()
|
||||
var contextErrors []string
|
||||
var contextAborted bool
|
||||
nextHandlerCalled := false
|
||||
|
||||
handlers := []HandlerFunc{
|
||||
func(c *Context) {
|
||||
c.HTML(tt.status, "index.tmpl", H{"title": "Main website"})
|
||||
contextErrors = c.Errors.Errors()
|
||||
contextAborted = c.IsAborted()
|
||||
},
|
||||
func(_ *Context) {
|
||||
nextHandlerCalled = true
|
||||
},
|
||||
}
|
||||
if tt.method == http.MethodHead {
|
||||
router.HEAD("/", handlers...)
|
||||
} else {
|
||||
router.GET("/", handlers...)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(tt.method, "/", nil)
|
||||
require.NotPanics(t, func() {
|
||||
router.ServeHTTP(w, req)
|
||||
})
|
||||
|
||||
assert.Equal(t, tt.status, w.Code)
|
||||
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
assert.Equal(t, tt.wantErrors, contextErrors)
|
||||
assert.Equal(t, tt.wantAborted, contextAborted)
|
||||
assert.Equal(t, tt.wantNextCalled, nextHandlerCalled)
|
||||
assert.Empty(t, w.Body.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextRenderHTML2(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, router := CreateTestContext(w)
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
@ -50,6 +51,8 @@ type HTML struct {
|
||||
|
||||
var htmlContentType = []string{"text/html; charset=utf-8"}
|
||||
|
||||
var errHTMLRendererNotConfigured = errors.New("html renderer is not configured")
|
||||
|
||||
// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
|
||||
func (r HTMLProduction) Instance(name string, data any) Render {
|
||||
return HTML{
|
||||
@ -88,6 +91,9 @@ func (r HTMLDebug) loadTemplate() *template.Template {
|
||||
// Render (HTML) executes template and writes its result with custom ContentType for response.
|
||||
func (r HTML) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
if r.Template == nil {
|
||||
return errHTMLRendererNotConfigured
|
||||
}
|
||||
|
||||
if r.Name == "" {
|
||||
return r.Template.Execute(w, r.Data)
|
||||
|
||||
@ -630,6 +630,16 @@ func TestRenderHTMLTemplate(t *testing.T) {
|
||||
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestRenderHTMLWithoutTemplate(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
err := (HTML{}).Render(w)
|
||||
|
||||
require.EqualError(t, err, "html renderer is not configured")
|
||||
assert.Empty(t, w.Body.String())
|
||||
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestRenderHTMLTemplateEmptyName(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
templ := template.Must(template.New("").Parse(`Hello {{.name}}`))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user