-
Notifications
You must be signed in to change notification settings - Fork 14
/
client_monitor_test.go
65 lines (54 loc) · 1.68 KB
/
client_monitor_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
package restful
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClientMonitorAddHeader(t *testing.T) {
assert := assert.New(t)
var preCount, postCount int
pre := func(req *http.Request) (*http.Response, error) {
req.Header.Add("X-My-Req-Header", strconv.FormatInt(int64(preCount), 10))
preCount++
return nil, nil
}
post := func(req *http.Request, resp *http.Response, err error) *http.Response {
resp.Header.Add("X-My-Resp-Header", strconv.FormatInt(int64(postCount), 10))
postCount++
if postCount > 0 {
return resp // Practically the same as return nil.
}
return nil
}
// Server
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal([]string{"0", "1"}, r.Header.Values("X-My-Req-Header"))
}))
defer srv.Close()
client := NewClient().Monitor(pre, post).Monitor(pre, nil).Monitor(nil, post)
resp, err := client.SendRecv2xx(context.Background(), http.MethodGet, srv.URL, nil, nil, nil)
assert.NoError(err)
assert.Equal([]string{"0", "1"}, resp.Header.Values("X-My-Resp-Header"))
assert.Equal(2, preCount)
assert.Equal(2, postCount)
}
func TestClientMonitorPreBlocks(t *testing.T) {
assert := assert.New(t)
preCount := 0
pre := func(req *http.Request) (*http.Response, error) {
preCount++
return nil, errors.New("blocking")
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
client := NewClient().Monitor(pre, nil).Monitor(pre, nil)
err := client.Get(context.Background(), srv.URL, nil)
assert.Error(err)
assert.Equal(1, preCount)
}