github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/panic_recovery/handler_test.go (about)

     1  package panicrecovery_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	panicrecovery "github.com/kyma-incubator/compass/components/director/pkg/panic_recovery"
     9  	"github.com/pkg/errors"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPanicRecoveryMiddleware(t *testing.T) {
    14  	middleware := panicrecovery.NewPanicRecoveryMiddleware()
    15  	req, err := http.NewRequest(http.MethodGet, "http://example.com", nil)
    16  	require.NoError(t, err)
    17  
    18  	recorder := httptest.NewRecorder()
    19  
    20  	middleware(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
    21  		panic(errors.New("test"))
    22  	})).ServeHTTP(recorder, req)
    23  
    24  	require.Equal(t, http.StatusInternalServerError, recorder.Code)
    25  	require.Contains(t, recorder.Body.String(), "Unrecovered panic")
    26  }