forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_timeout_test.go
103 lines (80 loc) · 2.08 KB
/
e2e_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
package httpexpect
import (
"math/rand"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func createTimeoutHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/sleep", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second)
})
mux.HandleFunc("/small", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`"`))
_, _ = w.Write([]byte(randomString(10)))
_, _ = w.Write([]byte(`"`))
})
mux.HandleFunc("/large", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`"`))
_, _ = w.Write([]byte(randomString(1024 * 10)))
_, _ = w.Write([]byte(`"`))
})
return mux
}
func TestE2ETimeoutDeadlineExpired(t *testing.T) {
handler := createTimeoutHandler()
server := httptest.NewServer(handler)
defer server.Close()
r := newMockReporter(t)
e := WithConfig(Config{
BaseURL: server.URL,
Reporter: r,
})
e.GET("/sleep").
WithTimeout(10 * time.Millisecond).
Expect()
assert.True(t, r.reported)
}
func TestE2ETimeoutSmallBody(t *testing.T) {
handler := createTimeoutHandler()
server := httptest.NewServer(handler)
defer server.Close()
e := Default(t, server.URL)
for i := 0; i < 100; i++ {
e.GET("/small").
WithTimeout(20 * time.Minute).
Expect().
Status(http.StatusOK).
JSON().
String()
}
}
func TestE2ETimeoutLargeBody(t *testing.T) {
handler := createTimeoutHandler()
server := httptest.NewServer(handler)
defer server.Close()
e := Default(t, server.URL)
for i := 0; i < 100; i++ {
e.GET("/large").
WithTimeout(20 * time.Minute).
Expect().
Status(http.StatusOK).
JSON().
String()
}
}