-
Notifications
You must be signed in to change notification settings - Fork 6
/
middleware_timeout_test.go
106 lines (81 loc) · 3.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package fiber
import (
"io"
"net/http"
"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("http.drivers.fiber.prefork", false).Return(false).Once()
mockConfig.EXPECT().GetInt("http.drivers.fiber.body_limit", 4096).Return(4096).Once()
mockConfig.EXPECT().GetInt("http.drivers.fiber.header_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 nil
})
route.Middleware(Timeout(1*time.Second)).Get("/normal", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().String("normal")
})
route.Middleware(Timeout(5*time.Second)).Get("/panic", func(ctx contractshttp.Context) contractshttp.Response {
panic("test panic")
})
t.Run("timeout", func(t *testing.T) {
req, err := http.NewRequest("GET", "/timeout", nil)
require.NoError(t, err)
resp, err := route.instance.Test(req, -1)
require.NoError(t, err)
require.NotNil(t, resp)
assert.Equal(t, contractshttp.StatusRequestTimeout, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "Request Timeout", string(body))
})
t.Run("normal", func(t *testing.T) {
req, err := http.NewRequest("GET", "/normal", nil)
require.NoError(t, err)
resp, err := route.instance.Test(req, -1)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "normal", string(body))
})
t.Run("panic with default recover", func(t *testing.T) {
mockLog := mockslog.NewLog(t)
mockLog.EXPECT().WithContext(mock.Anything).Return(mockLog).Once()
mockLog.EXPECT().Request(mock.Anything).Return(mockLog).Once()
mockLog.EXPECT().Error("test panic").Once()
LogFacade = mockLog
req, err := http.NewRequest("GET", "/panic", nil)
require.NoError(t, err)
resp, err := route.instance.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "Internal Server Error", string(body))
})
t.Run("panic with custom recover", func(t *testing.T) {
globalRecover := func(ctx contractshttp.Context, err any) {
ctx.Request().Abort(http.StatusInternalServerError)
}
route.Recover(globalRecover)
req, err := http.NewRequest("GET", "/panic", nil)
require.NoError(t, err)
resp, err := route.instance.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "Internal Server Error", string(body))
})
}