@@ -138,6 +138,24 @@ func TestContextRenderTemplate(t *testing.T) {
138138 }
139139}
140140
141+ func TestContextRenderTemplateError (t * testing.T ) {
142+ // we test that when template rendering fails, no response is sent to the client yet, so the global error handler can decide what to do
143+ e := New ()
144+ req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (userJSON ))
145+ rec := httptest .NewRecorder ()
146+ c := e .NewContext (req , rec )
147+
148+ tmpl := & Template {
149+ templates : template .Must (template .New ("hello" ).Parse ("Hello, {{.}}!" )),
150+ }
151+ c .Echo ().Renderer = tmpl
152+ err := c .Render (http .StatusOK , "not_existing" , "Jon Snow" )
153+
154+ assert .EqualError (t , err , `template: no template "not_existing" associated with template "hello"` )
155+ assert .Equal (t , http .StatusOK , rec .Code ) // status code must not be sent to the client
156+ assert .Empty (t , rec .Body .String ()) // body must not be sent to the client
157+ }
158+
141159func TestContextRenderErrorsOnNoRenderer (t * testing.T ) {
142160 e := New ()
143161 req := httptest .NewRequest (http .MethodPost , "/" , strings .NewReader (userJSON ))
@@ -222,6 +240,9 @@ func TestContextJSONErrorsOut(t *testing.T) {
222240
223241 err := c .JSON (http .StatusOK , make (chan bool ))
224242 assert .EqualError (t , err , "json: unsupported type: chan bool" )
243+
244+ assert .Equal (t , http .StatusOK , rec .Code ) // status code must not be sent to the client
245+ assert .Empty (t , rec .Body .String ()) // body must not be sent to the client
225246}
226247
227248func TestContextJSONPretty (t * testing.T ) {
0 commit comments