Download internal/logging/gin_logger_test.go from shimen/ShinAPI: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/spaces/shimen/ShinAPI/resolve/main/internal/logging/gin_logger_test.go
- Command line
-
hf download hf://spaces/shimen/ShinAPI/internal/logging/gin_logger_test.go
-
curl -L -o gin_logger_test.go https://huggingface.co/spaces/shimen/ShinAPI/resolve/main/internal/logging/gin_logger_test.go
1.34 kB
| package logging | |
| import ( | |
| "errors" | |
| "net/http" | |
| "net/http/httptest" | |
| "testing" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| func TestGinLogrusRecoveryRepanicsErrAbortHandler(t *testing.T) { | |
| gin.SetMode(gin.TestMode) | |
| engine := gin.New() | |
| engine.Use(GinLogrusRecovery()) | |
| engine.GET("/abort", func(c *gin.Context) { | |
| panic(http.ErrAbortHandler) | |
| }) | |
| req := httptest.NewRequest(http.MethodGet, "/abort", nil) | |
| recorder := httptest.NewRecorder() | |
| defer func() { | |
| recovered := recover() | |
| if recovered == nil { | |
| t.Fatalf("expected panic, got nil") | |
| } | |
| err, ok := recovered.(error) | |
| if !ok { | |
| t.Fatalf("expected error panic, got %T", recovered) | |
| } | |
| if !errors.Is(err, http.ErrAbortHandler) { | |
| t.Fatalf("expected ErrAbortHandler, got %v", err) | |
| } | |
| if err != http.ErrAbortHandler { | |
| t.Fatalf("expected exact ErrAbortHandler sentinel, got %v", err) | |
| } | |
| }() | |
| engine.ServeHTTP(recorder, req) | |
| } | |
| func TestGinLogrusRecoveryHandlesRegularPanic(t *testing.T) { | |
| gin.SetMode(gin.TestMode) | |
| engine := gin.New() | |
| engine.Use(GinLogrusRecovery()) | |
| engine.GET("/panic", func(c *gin.Context) { | |
| panic("boom") | |
| }) | |
| req := httptest.NewRequest(http.MethodGet, "/panic", nil) | |
| recorder := httptest.NewRecorder() | |
| engine.ServeHTTP(recorder, req) | |
| if recorder.Code != http.StatusInternalServerError { | |
| t.Fatalf("expected 500, got %d", recorder.Code) | |
| } | |
| } | |