mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-01-17 01:30:14 -06:00
Handle nil component in RenderGomponent: return NoContent response if component is nil; add corresponding unit test
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user