-
Notifications
You must be signed in to change notification settings - Fork 7
/
middleware_timeout_test.go
64 lines (51 loc) · 1.92 KB
/
middleware_timeout_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package gin
import (
"net/http"
"net/http/httptest"
"testing"
"time"
contractshttp "github.com/goravel/framework/contracts/http"
mocksconfig "github.com/goravel/framework/mocks/config"
mockslog "github.com/goravel/framework/mocks/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestTimeoutMiddleware(t *testing.T) {
mockConfig := mocksconfig.NewConfig(t)
mockConfig.EXPECT().GetBool("app.debug").Return(true).Once()
mockConfig.EXPECT().GetInt("http.drivers.gin.body_limit", 4096).Return(4096).Once()
route, err := NewRoute(mockConfig, nil)
require.NoError(t, err)
route.Middleware(Timeout(1*time.Second)).Get("/timeout", func(ctx contractshttp.Context) contractshttp.Response {
time.Sleep(2 * time.Second)
return ctx.Response().Success().String("timeout")
})
route.Middleware(Timeout(1*time.Second)).Get("/normal", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().String("normal")
})
route.Middleware(Timeout(1*time.Second)).Get("/panic", func(ctx contractshttp.Context) contractshttp.Response {
panic(1)
})
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/timeout", nil)
require.NoError(t, err)
route.ServeHTTP(w, req)
assert.Equal(t, http.StatusGatewayTimeout, w.Code)
w = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/normal", nil)
require.NoError(t, err)
route.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "normal", w.Body.String())
w = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/panic", nil)
require.NoError(t, err)
mockLog := mockslog.NewLog(t)
mockLog.EXPECT().Request(mock.Anything).Return(mockLog).Once()
mockLog.EXPECT().Error(mock.Anything).Once()
LogFacade = mockLog
route.ServeHTTP(w, req)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "Internal Server Error", w.Body.String())
}