From 12d9c2818eabe998f881848f918bd73747dbc4d3 Mon Sep 17 00:00:00 2001 From: Eric Klatzer Date: Tue, 28 May 2024 16:12:24 +0000 Subject: [PATCH] added test for recover log error func --- internal/api/middleware/recover_test.go | 42 +++++++++++++++++++ .../TestLogErrorFuncWithRequestInfo.golden | 1 + 2 files changed, 43 insertions(+) create mode 100644 internal/api/middleware/recover_test.go create mode 100644 test/testdata/snapshots/TestLogErrorFuncWithRequestInfo.golden diff --git a/internal/api/middleware/recover_test.go b/internal/api/middleware/recover_test.go new file mode 100644 index 00000000..799b0927 --- /dev/null +++ b/internal/api/middleware/recover_test.go @@ -0,0 +1,42 @@ +package middleware_test + +import ( + "fmt" + "io" + "net/http" + "testing" + + "allaboutapps.dev/aw/go-starter/internal/api" + "allaboutapps.dev/aw/go-starter/internal/api/middleware" + "allaboutapps.dev/aw/go-starter/internal/test" + "github.com/labstack/echo/v4" + echoMiddleware "github.com/labstack/echo/v4/middleware" + + "github.com/stretchr/testify/require" +) + +func TestLogErrorFuncWithRequestInfo(t *testing.T) { + test.WithTestServer(t, func(s *api.Server) { + path := "/testing-e87bc94c-2d1f-4342-9ec2-f158c63ac6da" + + s.Echo.Use(echoMiddleware.RecoverWithConfig(echoMiddleware.RecoverConfig{ + LogErrorFunc: middleware.LogErrorFuncWithRequestInfo, + })) + + s.Echo.POST(path, func(c echo.Context) error { + + var val *int + fmt.Println(*val) + + return c.NoContent(http.StatusNoContent) + }) + + res := test.PerformRequest(t, s, "POST", path, nil, nil) + require.Equal(t, http.StatusInternalServerError, res.Result().StatusCode) + + body, err := io.ReadAll(res.Body) + require.NoError(t, err) + + test.Snapshoter.SaveString(t, string(body)) + }) +} diff --git a/test/testdata/snapshots/TestLogErrorFuncWithRequestInfo.golden b/test/testdata/snapshots/TestLogErrorFuncWithRequestInfo.golden new file mode 100644 index 00000000..4aabea39 --- /dev/null +++ b/test/testdata/snapshots/TestLogErrorFuncWithRequestInfo.golden @@ -0,0 +1 @@ +{"status":500,"title":"Internal Server Error","type":"generic"}