Skip to content

Commit

Permalink
Handle nil component in RenderGomponent: return NoContent response if…
Browse files Browse the repository at this point in the history
… component is nil; add corresponding unit test
  • Loading branch information
eduardolat committed Aug 22, 2024
1 parent fe96475 commit ac17ec7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/util/echoutil/render_gomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions internal/util/echoutil/render_gomponent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit ac17ec7

Please sign in to comment.