-
Notifications
You must be signed in to change notification settings - Fork 20
/
httpClient_test.go
164 lines (139 loc) · 3.82 KB
/
httpClient_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
163
164
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"io"
"net/http"
"strconv"
"testing"
"time"
"github.com/go-kit/kit/metrics"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRoundTripper(t *testing.T) {
date1 := time.Date(2021, time.Month(2), 21, 1, 10, 30, 0, time.UTC)
date2 := time.Date(2021, time.Month(2), 21, 1, 10, 30, 45, time.UTC)
tests := []struct {
description string
startTime time.Time
endTime time.Time
expectedCode string
request *http.Request
expectedErr error
expectedResponse *http.Response
}{
{
description: "Success",
startTime: date1,
endTime: date2,
expectedCode: strconv.Itoa(http.StatusOK),
request: exampleRequest(1),
expectedErr: nil,
expectedResponse: &http.Response{
StatusCode: http.StatusOK,
},
},
{
description: "503 Service Unavailable",
startTime: date1,
endTime: date2,
expectedCode: strconv.Itoa(http.StatusServiceUnavailable),
request: exampleRequest(1),
expectedErr: nil,
expectedResponse: &http.Response{
StatusCode: http.StatusServiceUnavailable,
},
},
{
description: "Network Error",
startTime: date1,
endTime: date2,
expectedCode: strconv.Itoa(http.StatusServiceUnavailable),
request: exampleRequest(1),
expectedErr: errors.New(genericDoReason),
expectedResponse: &http.Response{
StatusCode: http.StatusServiceUnavailable,
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
fakeTime := mockTime(tc.startTime, tc.endTime)
fakeHandler := new(mockHandler)
fakeHist := new(mockHistogram)
histogramFunctionCall := []string{urlLabel, tc.request.URL.String(), reasonLabel, getDoErrReason(tc.expectedErr), codeLabel, tc.expectedCode}
fakeLatency := date2.Sub(date1)
fakeHist.On("With", histogramFunctionCall).Return().Once()
fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()
// Create a roundtripper with mock time and mock histogram
m, err := newMetricWrapper(fakeTime, fakeHist)
require.NoError(t, err)
require.NotNil(t, m)
client := doerFunc(func(*http.Request) (*http.Response, error) {
return tc.expectedResponse, tc.expectedErr
})
c := m.roundTripper(client)
resp, err := c.Do(tc.request)
if tc.expectedErr == nil {
// Read and close response body
if resp.Body != nil {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
assert.NoError(t, err)
} else {
assert.ErrorIs(t, tc.expectedErr, err)
}
// Check the histogram and expectations
fakeHandler.AssertExpectations(t)
fakeHist.AssertExpectations(t)
})
}
}
func TestNewMetricWrapper(t *testing.T) {
tests := []struct {
description string
expectedErr error
fakeTime func() time.Time
fakeHistogram metrics.Histogram
}{
{
description: "Success",
expectedErr: nil,
fakeTime: time.Now,
fakeHistogram: &mockHistogram{},
},
{
description: "Nil Histogram",
expectedErr: errNilHistogram,
fakeTime: time.Now,
fakeHistogram: nil,
},
{
description: "Nil Time",
expectedErr: nil,
fakeTime: nil,
fakeHistogram: &mockHistogram{},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
// Make function call
mw, err := newMetricWrapper(tc.fakeTime, tc.fakeHistogram)
if tc.expectedErr == nil {
// Check for no errors
assert.NoError(t, err)
require.NotNil(t, mw)
// Check that the time and histogram aren't nil
assert.NotNil(t, mw.now)
assert.NotNil(t, mw.queryLatency)
return
}
// with error checks
assert.Nil(t, mw)
assert.ErrorIs(t, err, tc.expectedErr)
})
}
}