-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout_test.go
130 lines (104 loc) · 4.29 KB
/
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2023 Sylvain Müller. All rights reserved.
// Mount of this source code is governed by a MIT license that can be found
// at https://github.com/tigerwill90/foxtimeout/blob/master/LICENSE.txt.
package foxtimeout
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/tigerwill90/fox"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func success201response(c fox.Context) {
time.Sleep(10 * time.Millisecond)
_ = c.String(http.StatusCreated, "%s\n", http.StatusText(http.StatusCreated))
}
func TestMiddleware_WithTimeout(t *testing.T) {
f := fox.New(fox.WithMiddleware(Middleware(50 * time.Microsecond)))
f.MustHandle(http.MethodGet, "/foo", success201response)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
assert.Equal(t, fmt.Sprintf("%s\n", http.StatusText(http.StatusServiceUnavailable)), w.Body.String())
}
func TestMiddleware_WithoutTimeout(t *testing.T) {
f := fox.New(fox.WithMiddleware(Middleware(1 * time.Second)))
f.MustHandle(http.MethodGet, "/foo", success201response)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
assert.Equal(t, fmt.Sprintf("%s\n", http.StatusText(http.StatusCreated)), w.Body.String())
}
func timeoutResponse(c fox.Context) {
http.Error(c.Writer(), http.StatusText(http.StatusRequestTimeout), http.StatusRequestTimeout)
}
func TestMiddleware_WithResponse(t *testing.T) {
f := fox.New(fox.WithMiddleware(Middleware(50*time.Microsecond, WithResponse(timeoutResponse))))
f.MustHandle(http.MethodGet, "/foo", success201response)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.Equal(t, http.StatusRequestTimeout, w.Code)
assert.Equal(t, fmt.Sprintf("%s\n", http.StatusText(http.StatusRequestTimeout)), w.Body.String())
}
func panicResponse(c fox.Context) {
panic("test")
}
func TestMiddleware_WithPanic(t *testing.T) {
f := fox.New(
fox.WithMiddleware(
fox.CustomRecoveryWithLogHandler(slog.NewTextHandler(io.Discard, nil), func(c fox.Context, err any) {
if !c.Writer().Written() {
http.Error(c.Writer(), http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}),
Middleware(1*time.Second, WithResponse(timeoutResponse)),
),
)
f.MustHandle(http.MethodGet, "/foo", panicResponse)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, fmt.Sprintf("%s\n", http.StatusText(http.StatusInternalServerError)), w.Body.String())
}
func TestMiddleware_NoTimeout(t *testing.T) {
f := fox.New(fox.WithMiddleware(Middleware(0)))
f.MustHandle(http.MethodGet, "/foo", success201response)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
assert.Equal(t, fmt.Sprintf("%s\n", http.StatusText(http.StatusCreated)), w.Body.String())
}
func TestMiddleware_ErrNotSupported(t *testing.T) {
f := fox.New(fox.WithMiddleware(Middleware(1 * time.Second)))
f.MustHandle(http.MethodGet, "/foo", func(c fox.Context) {
assert.ErrorIs(t, c.Writer().FlushError(), http.ErrNotSupported)
_, _, hijErr := c.Writer().Hijack()
assert.ErrorIs(t, hijErr, http.ErrNotSupported)
assert.ErrorIs(t, c.Writer().SetReadDeadline(time.Now()), http.ErrNotSupported)
assert.ErrorIs(t, c.Writer().SetWriteDeadline(time.Now()), http.ErrNotSupported)
})
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
}
func TestMiddleware_WithTimeoutResolver(t *testing.T) {
resolver := WithTimeoutResolver(TimeoutResolverFunc(func(c fox.Context) (dt time.Duration, ok bool) {
return 2 * time.Second, true
}))
f := fox.New(fox.WithMiddleware(Middleware(1*time.Millisecond, resolver)))
f.MustHandle(http.MethodGet, "/foo", success201response)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
assert.Equal(t, fmt.Sprintf("%s\n", http.StatusText(http.StatusCreated)), w.Body.String())
}