Skip to content

Commit

Permalink
added test for recover log error func
Browse files Browse the repository at this point in the history
  • Loading branch information
eklatzer committed May 28, 2024
1 parent b01b659 commit 12d9c28
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/api/middleware/recover_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":500,"title":"Internal Server Error","type":"generic"}

0 comments on commit 12d9c28

Please sign in to comment.