-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout_test.go
More file actions
99 lines (84 loc) · 2.67 KB
/
timeout_test.go
File metadata and controls
99 lines (84 loc) · 2.67 KB
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
package rmhttp
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// ------------------------------------------------------------------------------------------------
// TIMEOUT TESTS
// ------------------------------------------------------------------------------------------------
// Test_Timeout_applyTimeout checks that route timeouts act as expected
func Test_Timeout_applyTimeout(t *testing.T) {
testPattern := "/timeout"
timeoutMessage := "Timeout!!!"
testBody := "Timeout body"
url := fmt.Sprintf("http://%s%s", testAddress, testPattern)
t.Run("timeout handler intercepts long running handler and throws error", func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
_, _ = w.Write([]byte("Hello"))
})
// Create a request that would trigger our test handler
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Errorf("failed to create request: %v", err)
}
w := httptest.NewRecorder()
route := NewRoute(
http.MethodGet,
testPattern,
handler,
).WithTimeout(1*time.Second, timeoutMessage)
middleware := []func(http.Handler) http.Handler{TimeoutMiddleware(route.ComputedTimeout())}
h := applyMiddleware(
route.Handler,
middleware,
)
h.ServeHTTP(w, req)
res := w.Result()
defer func() {
err := res.Body.Close()
if err != nil {
t.Errorf("failed to close response body: %v", err)
}
}()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("failed to read response body: %v", err)
}
assert.Equal(t, http.StatusServiceUnavailable, res.StatusCode, "they should be equal")
assert.Equal(t, timeoutMessage, string(body), "they should be equal")
})
t.Run("timeout handler passes through handler without error", func(t *testing.T) {
handler := http.HandlerFunc(createTestHandlerFunc(http.StatusOK, testBody))
route := NewRoute(
http.MethodGet,
testPattern,
handler,
).WithTimeout(1*time.Second, timeoutMessage)
// Create a request that would trigger our test handler
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Errorf("failed to create request: %v", err)
}
w := httptest.NewRecorder()
route.Handler.ServeHTTP(w, req)
res := w.Result()
defer func() {
err := res.Body.Close()
if err != nil {
t.Errorf("failed to close response body: %v", err)
}
}()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("failed to read response body: %v", err)
}
assert.Equal(t, testBody, string(body), "they should be equal")
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal")
})
}