-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimeout_option_test.go
More file actions
76 lines (64 loc) · 2.14 KB
/
timeout_option_test.go
File metadata and controls
76 lines (64 loc) · 2.14 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
package httpc
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestTimeoutOptionPassesThroughBeforeDeadline(t *testing.T) {
opt := &TimeoutOption{Timeout: 200 * time.Millisecond}
handler := opt.Next(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Test", "ok")
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte("done"))
})
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
if recorder.Code != http.StatusCreated {
t.Fatalf("unexpected status: got %d want %d", recorder.Code, http.StatusCreated)
}
if recorder.Header().Get("X-Test") != "ok" {
t.Fatalf("unexpected header: got %q want %q", recorder.Header().Get("X-Test"), "ok")
}
if recorder.Body.String() != "done" {
t.Fatalf("unexpected body: got %q want %q", recorder.Body.String(), "done")
}
}
func TestTimeoutOptionReturns408AndCancelsHandler(t *testing.T) {
opt := &TimeoutOption{Timeout: 20 * time.Millisecond}
done := make(chan error, 1)
handler := opt.Next(func(w http.ResponseWriter, r *http.Request) {
<-r.Context().Done()
_, err := w.Write([]byte("too late"))
done <- err
})
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
if recorder.Code != http.StatusRequestTimeout {
t.Fatalf("unexpected status: got %d want %d", recorder.Code, http.StatusRequestTimeout)
}
if recorder.Body.Len() != 0 {
t.Fatalf("unexpected body: got %q want empty", recorder.Body.String())
}
select {
case err := <-done:
if !errors.Is(err, ErrHandlerTimeout) {
t.Fatalf("unexpected write error: got %v want %v", err, ErrHandlerTimeout)
}
case <-time.After(time.Second):
t.Fatal("handler did not observe timeout cancellation")
}
}
func TestTimeoutOptionPropagatesPanicBeforeTimeout(t *testing.T) {
opt := &TimeoutOption{Timeout: time.Second}
handler := opt.Next(func(http.ResponseWriter, *http.Request) {
panic("boom")
})
defer func() {
if p := recover(); p == nil {
t.Fatal("expected panic to propagate")
}
}()
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
}