2024-09-24 23:07:42 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-09-25 13:52:12 +02:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2024-09-24 23:07:42 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-09-24 23:07:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimeout(t *testing.T) {
|
|
|
|
t.Run("Test timeout handler", func(t *testing.T) {
|
|
|
|
timeoutHandler := Timeout(1 * time.Millisecond)
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
longHandler := func(c *gin.Context) {
|
2024-09-25 13:52:12 +02:00
|
|
|
time.Sleep(2 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2024-09-25 14:09:29 +02:00
|
|
|
res := prepareMiddlewareTest(t, timeoutHandler, longHandler)
|
2024-09-25 13:52:12 +02:00
|
|
|
|
2024-09-25 14:09:29 +02:00
|
|
|
assertCode(t, res.StatusCode, http.StatusRequestTimeout)
|
2024-09-28 11:13:18 +02:00
|
|
|
assertBody(t, res.Body, "\"time out\"")
|
2024-09-25 14:09:29 +02:00
|
|
|
})
|
|
|
|
t.Run("Test no timeout", func(t *testing.T) {
|
|
|
|
timeoutHandler := Timeout(2 * time.Millisecond)
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
quickHandler := func(c *gin.Context) {
|
2024-09-25 14:09:29 +02:00
|
|
|
// time.Sleep(1 * time.Millisecond)
|
2024-09-28 11:13:18 +02:00
|
|
|
c.JSON(http.StatusOK, "ok")
|
2024-09-25 13:52:12 +02:00
|
|
|
}
|
2024-09-25 14:09:29 +02:00
|
|
|
|
|
|
|
res := prepareMiddlewareTest(t, timeoutHandler, quickHandler)
|
|
|
|
|
|
|
|
assertCode(t, res.StatusCode, http.StatusOK)
|
2024-09-24 23:07:42 +02:00
|
|
|
})
|
|
|
|
}
|
2024-09-25 13:52:12 +02:00
|
|
|
|
|
|
|
func TestRecover(t *testing.T) {
|
2024-09-25 14:09:29 +02:00
|
|
|
t.Run("Test panic", func(t *testing.T) {
|
|
|
|
recoverer := Recovery()
|
2024-09-25 13:52:12 +02:00
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
panicHandler := func(c *gin.Context) {
|
2024-09-25 14:09:29 +02:00
|
|
|
panic("panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
res := prepareMiddlewareTest(t, recoverer, panicHandler)
|
|
|
|
|
|
|
|
assertCode(t, res.StatusCode, http.StatusInternalServerError)
|
|
|
|
})
|
|
|
|
t.Run("Test no panic", func(t *testing.T) {
|
|
|
|
recoverer := Recovery()
|
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
normalHandler := func(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, "ok")
|
2024-09-25 14:09:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res := prepareMiddlewareTest(t, recoverer, normalHandler)
|
|
|
|
|
|
|
|
assertCode(t, res.StatusCode, http.StatusOK)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareMiddlewareTest(
|
|
|
|
t testing.TB,
|
2024-09-28 11:13:18 +02:00
|
|
|
mid gin.HandlerFunc,
|
|
|
|
in gin.HandlerFunc,
|
2024-09-25 14:09:29 +02:00
|
|
|
) *http.Response {
|
|
|
|
t.Helper()
|
2024-09-25 13:52:12 +02:00
|
|
|
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
response := httptest.NewRecorder()
|
2024-09-28 11:13:18 +02:00
|
|
|
_, r := gin.CreateTestContext(response)
|
2024-09-25 13:52:12 +02:00
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
r.Use(mid)
|
|
|
|
r.GET("/", in)
|
2024-09-25 13:52:12 +02:00
|
|
|
|
2024-09-28 11:13:18 +02:00
|
|
|
r.ServeHTTP(response, request)
|
2024-09-25 14:09:29 +02:00
|
|
|
|
2024-09-25 13:52:12 +02:00
|
|
|
res := response.Result()
|
2024-09-25 14:09:29 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertCode(t testing.TB, got int, want int) {
|
|
|
|
t.Helper()
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("status code got %d, want %d", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertBody(t testing.TB, got io.Reader, want string) {
|
|
|
|
t.Helper()
|
|
|
|
buf, _ := io.ReadAll(got)
|
|
|
|
if cmp := bytes.Compare(buf, []byte(want)); cmp != 0 {
|
|
|
|
t.Errorf("got %q, want %q", string(buf), want)
|
2024-09-25 13:52:12 +02:00
|
|
|
}
|
|
|
|
}
|