-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-client-with-stats_test.go
122 lines (96 loc) · 3.95 KB
/
http-client-with-stats_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
package tools
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type fakeClock struct {
currentTime time.Time
}
func (f *fakeClock) Now() time.Time {
f.currentTime = f.currentTime.Add(100 * time.Millisecond)
return f.currentTime
}
func TestHTTPClientWithStats_Do(t *testing.T) {
fc := &fakeClock{time.Now()}
msd := &MockStatsD{}
hc := http.DefaultClient
wc := &httpClientWithStats{statsd: msd, httpClient: hc, clock: fc}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "Hello World")
}))
defer ts.Close()
req, _ := http.NewRequest("GET", ts.URL, nil)
assert.Len(t, msd.Calls, 0)
resp, _ := wc.Do(req, "http_callee:my-remote-service", "operation:my-operation")
expectedTags := []string{"http_callee:my-remote-service", "operation:my-operation", "method:GET", "resp_status:200"}
assert.Len(t, msd.Calls, 4)
assert.NotNil(t, resp)
assert.Equal(t, "Histogram", msd.Calls[0].Method)
assert.Equal(t, 100.0, msd.Calls[0].Args.Value)
assert.Equal(t, expectedTags, msd.Calls[0].Args.Tags)
assert.Equal(t, "Incr", msd.Calls[1].Method)
assert.Equal(t, "http_client.response_success", msd.Calls[1].Args.Name)
assert.Equal(t, "Incr", msd.Calls[2].Method)
assert.Equal(t, "http_client.response_code.200", msd.Calls[2].Args.Name)
assert.Equal(t, "Incr", msd.Calls[3].Method)
assert.Equal(t, "http_client.response_code.all", msd.Calls[3].Args.Name)
assert.Equal(t, expectedTags, msd.Calls[1].Args.Tags)
}
func TestHTTPClientWithStats_Do_Error(t *testing.T) {
msd := &MockStatsD{}
hc := http.DefaultClient
wc := NewHTTPClientWithStats(hc, msd)
req, _ := http.NewRequest("GET", "http://nil", nil)
req.URL = nil
assert.Len(t, msd.Calls, 0)
resp, err := wc.Do(req, "http_callee:my-remote-service", "operation:my-operation")
assert.Len(t, msd.Calls, 1)
assert.NotNil(t, err)
assert.Nil(t, resp)
assert.Equal(t, "Incr", msd.Calls[0].Method)
assert.Equal(t, "http_client.response_error", msd.Calls[0].Args.Name)
assert.Equal(t, []string{"http_callee:my-remote-service", "operation:my-operation", "method:GET"}, msd.Calls[0].Args.Tags)
}
func TestHTTPClientWithStats_Get(t *testing.T) {
msd := &MockStatsD{}
hc := http.DefaultClient
wc := NewHTTPClientWithStats(hc, msd)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "Hello World")
}))
defer ts.Close()
assert.Len(t, msd.Calls, 0)
resp, _ := wc.Get(ts.URL, "http_callee:my-remote-service", "operation:my-operation")
assert.Len(t, msd.Calls, 4)
assert.NotNil(t, resp)
assert.Equal(t, "Histogram", msd.Calls[0].Method)
assert.Equal(t, []string{"http_callee:my-remote-service", "operation:my-operation", "method:GET", "resp_status:200"}, msd.Calls[0].Args.Tags)
assert.Equal(t, "Incr", msd.Calls[2].Method)
assert.Equal(t, "http_client.response_code.200", msd.Calls[2].Args.Name)
assert.Equal(t, "Incr", msd.Calls[3].Method)
assert.Equal(t, "http_client.response_code.all", msd.Calls[3].Args.Name)
}
func TestHttpClientWithStats_Post(t *testing.T) {
msd := &MockStatsD{}
hc := http.DefaultClient
wc := NewHTTPClientWithStats(hc, msd)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "Hello World")
}))
defer ts.Close()
assert.Len(t, msd.Calls, 0)
resp, _ := wc.Post(ts.URL, "application/json", strings.NewReader(`{"hello":"world"}`), "http_callee:my-remote-service", "operation:my-operation")
assert.Len(t, msd.Calls, 4)
assert.NotNil(t, resp)
assert.Equal(t, "Histogram", msd.Calls[0].Method)
assert.Equal(t, []string{"http_callee:my-remote-service", "operation:my-operation", "method:POST", "resp_status:200"}, msd.Calls[0].Args.Tags)
assert.Equal(t, "Incr", msd.Calls[2].Method)
assert.Equal(t, "http_client.response_code.200", msd.Calls[2].Args.Name)
assert.Equal(t, "Incr", msd.Calls[3].Method)
assert.Equal(t, "http_client.response_code.all", msd.Calls[3].Args.Name)
}