package timeout import ( "context" "net/http" "net/http/httptest" "strings" "testing" "time" "git.company.lan/gopkg/gin" "github.com/stretchr/testify/assert" ) // nolint:unparam func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder { req, _ := http.NewRequestWithContext(context.Background(), method, path, nil) w := httptest.NewRecorder() r.ServeHTTP(w, req) return w } func emptySuccessResponse(c *gin.Context) { time.Sleep(200 * time.Microsecond) c.String(http.StatusOK, "") } func TestTimeout(t *testing.T) { r := gin.New() r.GET("/", New(WithTimeout(50*time.Microsecond), WithHandler(emptySuccessResponse))) w := performRequest(r, "GET", "/") assert.Equal(t, http.StatusRequestTimeout, w.Code) assert.Equal(t, http.StatusText(http.StatusRequestTimeout), w.Body.String()) } func TestWithoutTimeout(t *testing.T) { r := gin.New() r.GET("/", New(WithTimeout(-1*time.Microsecond), WithHandler(emptySuccessResponse))) w := performRequest(r, "GET", "/") assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "", w.Body.String()) } func testResponse(c *gin.Context) { c.String(http.StatusRequestTimeout, "test response") } func TestCustomResponse(t *testing.T) { r := gin.New() r.GET("/", New( WithTimeout(100*time.Microsecond), WithHandler(emptySuccessResponse), WithResponse(testResponse), )) w := performRequest(r, "GET", "/") assert.Equal(t, http.StatusRequestTimeout, w.Code) assert.Equal(t, "test response", w.Body.String()) } func emptySuccessResponse2(c *gin.Context) { time.Sleep(50 * time.Microsecond) c.String(http.StatusOK, "") } func TestSuccess(t *testing.T) { r := gin.New() r.GET("/", New( WithTimeout(1*time.Second), WithHandler(emptySuccessResponse2), WithResponse(testResponse), )) w := performRequest(r, "GET", "/") assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "", w.Body.String()) } func TestPanic(t *testing.T) { buffer := new(strings.Builder) r := gin.New() r.Use(gin.RecoveryWithWriter(buffer)) r.GET("/", New( WithTimeout(1*time.Second), WithHandler(func(_ *gin.Context) { panic("Oupps, Houston, we have a problem") }), )) w := performRequest(r, "GET", "/") assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Contains(t, buffer.String(), "panic recovered") assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem") assert.Contains(t, buffer.String(), t.Name()) }