Handle nil component in RenderGomponent: return NoContent response if component is nil; add corresponding unit test

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-08-21 21:46:37 -06:00
parent fe96475852
commit ac17ec7925
2 changed files with 16 additions and 0 deletions

View File

@@ -15,6 +15,10 @@ type renderer interface {
// RenderGomponent renders a gomponents component to the response of
// the echo context.
func RenderGomponent(c echo.Context, code int, component renderer) error {
if component == nil {
return c.NoContent(code)
}
buf := bytes.Buffer{}
err := component.Render(&buf)
if err != nil {

View File

@@ -53,3 +53,15 @@ func TestRenderGomponent(t *testing.T) {
assert.Equal(t, "render nok", rec.Body.String())
})
}
func TestRenderNilGomponent(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := RenderGomponent(c, http.StatusOK, nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "", rec.Body.String())
}