-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse_handler_test.go
162 lines (153 loc) · 3.99 KB
/
response_handler_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package hx_test
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/izumin5210/hx"
)
func TestResponseHandlerCond(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/ping":
status, _ := strconv.Atoi(r.URL.Query().Get("status"))
if status == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(status)
w.Write([]byte("pong"))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("failed to get free port: %v", err)
}
defer l.Close()
freePort := l.Addr().(*net.TCPAddr).Port
ctx := context.Background()
t.Run("client error", func(t *testing.T) {
for _, st := range []int{400, 401, 498, 499} {
t.Run(fmt.Sprint(st), func(t *testing.T) {
err := hx.Get(ctx, ts.URL+"/ping",
hx.Query("status", fmt.Sprint(st)),
hx.WhenClientError(hx.AsError()),
)
if err == nil {
t.Error("returned nil, want an error")
}
})
}
for _, st := range []int{398, 399, 500, 501} {
t.Run(fmt.Sprint(st), func(t *testing.T) {
err := hx.Get(ctx, ts.URL+"/ping",
hx.Query("status", fmt.Sprint(st)),
hx.WhenClientError(hx.AsError()),
)
if err != nil {
t.Errorf("returned %v, want nil", err)
}
})
}
})
t.Run("server error", func(t *testing.T) {
for _, st := range []int{500, 501, 598, 599} {
t.Run(fmt.Sprint(st), func(t *testing.T) {
err := hx.Get(ctx, ts.URL+"/ping",
hx.Query("status", fmt.Sprint(st)),
hx.WhenServerError(hx.AsError()),
)
if err == nil {
t.Error("returned nil, want an error")
}
})
}
for _, st := range []int{498, 499, 600, 601} {
t.Run(fmt.Sprint(st), func(t *testing.T) {
err := hx.Get(ctx, ts.URL+"/ping",
hx.Query("status", fmt.Sprint(st)),
hx.WhenServerError(hx.AsError()),
)
if err != nil {
t.Errorf("returned %v, want nil", err)
}
})
}
})
t.Run("temporary error", func(t *testing.T) {
for _, st := range []int{400, 500} {
t.Run(fmt.Sprint(st), func(t *testing.T) {
var handled bool
err := hx.Get(ctx, ts.URL+"/ping",
hx.Timeout(10*time.Millisecond),
hx.Query("status", fmt.Sprint(st)),
hx.When(hx.IsTemporaryError, func(r *http.Response, e error) (*http.Response, error) {
handled = true
return r, e
}),
)
if err != nil {
t.Errorf("returned %v, want nil", err)
}
if handled {
t.Errorf("should not handle error as temporary: %v", err)
}
})
}
t.Run("when server stopped", func(t *testing.T) {
var handled bool
err := hx.Get(ctx, fmt.Sprintf("http://localhost:%d/ping", freePort),
hx.Timeout(10*time.Millisecond),
hx.When(hx.IsTemporaryError, func(r *http.Response, e error) (*http.Response, error) {
handled = true
return r, e
}),
)
if err == nil {
t.Error("returned nil, want an error")
}
if !handled {
t.Errorf("should handle error as temporary: %v", err)
}
})
})
t.Run("Any", func(t *testing.T) {
cond := hx.Any(hx.IsServerError, hx.IsTemporaryError)
t.Run(fmt.Sprint(500), func(t *testing.T) {
err := hx.Get(ctx, ts.URL+"/ping",
hx.Timeout(10*time.Millisecond),
hx.Query("status", fmt.Sprint(500)),
hx.When(cond, hx.AsError()),
)
if err == nil {
t.Error("returned nil, want an error")
}
})
t.Run(fmt.Sprint(400), func(t *testing.T) {
err := hx.Get(ctx, ts.URL+"/ping",
hx.Timeout(10*time.Millisecond),
hx.Query("status", fmt.Sprint(400)),
hx.When(cond, hx.AsError()),
)
if err != nil {
t.Errorf("returned %v, want nil", err)
}
})
t.Run("when server stopped", func(t *testing.T) {
err := hx.Get(ctx, fmt.Sprintf("http://localhost:%d/ping", freePort),
hx.Timeout(10*time.Millisecond),
hx.When(cond, hx.AsError()),
)
if err == nil {
t.Error("returned nil, want an error")
}
})
})
}