diff --git a/httpClient.go b/httpClient.go index bb877584..e6a96216 100644 --- a/httpClient.go +++ b/httpClient.go @@ -3,20 +3,21 @@ package main import ( "errors" "net/http" + "strconv" "time" "github.com/go-kit/kit/metrics" ) var ( - errNilHistogram = errors.New("histogram cannot be nil") + errNilHistogram = errors.New("histogram cannot be nil") ) type httpClient interface { Do(*http.Request) (*http.Response, error) } -func noHTTPClient(next httpClient) httpClient { +func nopHTTPClient(next httpClient) httpClient { return next } @@ -53,7 +54,7 @@ func (m *metricWrapper) roundTripper(next httpClient) httpClient { code := networkError if err == nil { - code = resp.Status + code = strconv.Itoa(resp.StatusCode) } // find time difference, add to metric diff --git a/httpClient_test.go b/httpClient_test.go index 116f3d93..bb1898f7 100644 --- a/httpClient_test.go +++ b/httpClient_test.go @@ -19,13 +19,14 @@ package main import ( "errors" + "io" + "io/ioutil" "net/http" "testing" "time" "github.com/go-kit/kit/metrics" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -38,33 +39,41 @@ func TestRoundTripper(t *testing.T) { description string startTime time.Time endTime time.Time - expectedResponse string + expectedCode string request *http.Request expectedErr error + expectedResponse *http.Response }{ { - description: "Success", - startTime: date1, - endTime: date2, - expectedResponse: "200 OK", - request: exampleRequest(1), - expectedErr: nil, + description: "Success", + startTime: date1, + endTime: date2, + expectedCode: "200", + request: exampleRequest(1), + expectedErr: nil, + expectedResponse: &http.Response{ + StatusCode: 200, + }, }, { - description: "503 Service Unavailable", - startTime: date1, - endTime: date2, - expectedResponse: "503 Service Unavailable", - request: exampleRequest(1), - expectedErr: nil, + description: "503 Service Unavailable", + startTime: date1, + endTime: date2, + expectedCode: "503", + request: exampleRequest(1), + expectedErr: nil, + expectedResponse: &http.Response{ + StatusCode: 503, + }, }, { description: "Network Error", startTime: date1, endTime: date2, - expectedResponse: "network_err", + expectedCode: "network_err", request: exampleRequest(1), expectedErr: errTest, + expectedResponse: nil, }, } @@ -75,37 +84,35 @@ func TestRoundTripper(t *testing.T) { fakeTime := mockTime(tc.startTime, tc.endTime) fakeHandler := new(mockHandler) fakeHist := new(mockHistogram) - histogramFunctionCall := []string{"code", tc.expectedResponse} + histogramFunctionCall := []string{"code", tc.expectedCode} + fakeLatency := date2.Sub(date1) fakeHist.On("With", histogramFunctionCall).Return().Once() - fakeHist.On("Observe", mock.AnythingOfType("float64")).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) - // Create an http response - expected := http.Response{ - Status: tc.expectedResponse, - } - client := doerFunc(func(*http.Request) (*http.Response, error) { - return &expected, tc.expectedErr + + return tc.expectedResponse, tc.expectedErr }) c := m.roundTripper(client) resp, err := c.Do(tc.request) - // Check Error - if tc.expectedErr != nil { - assert.ErrorIs(t, tc.expectedErr, err) - } else { + if tc.expectedErr == nil { + // Read and close response body + if resp.Body != nil { + io.Copy(ioutil.Discard, resp.Body) + resp.Body.Close() + } assert.NoError(t, err) + } else { + assert.ErrorIs(t, tc.expectedErr, err) } - // Check response - assert.Equal(t, resp.Status, tc.expectedResponse) - // Check the histogram and expectations fakeHandler.AssertExpectations(t) fakeHist.AssertExpectations(t) diff --git a/mocks_test.go b/mocks_test.go index ea8e1e41..1aeed03a 100644 --- a/mocks_test.go +++ b/mocks_test.go @@ -17,7 +17,6 @@ package main import ( - "net/http" "time" "unicode/utf8" @@ -148,14 +147,3 @@ func mockTime(one, two time.Time) func() time.Time { return one } } - -// mockHttpClient provides a mock implementation for the httpClient interface -type mockHttpClient struct { - MockDo func(*http.Request) (*http.Response, error) -} - -//type mockDoerFunc func(*http.Request) (*http.Response, error) - -func (d mockHttpClient) Do(req *http.Request) (*http.Response, error) { - return d.MockDo(req) -} diff --git a/outboundSender.go b/outboundSender.go index c5c0a241..7cc0e818 100644 --- a/outboundSender.go +++ b/outboundSender.go @@ -168,7 +168,7 @@ func (osf OutboundSenderFactory) New() (obs OutboundSender, err error) { } if nil == osf.ClientMiddleware { - osf.ClientMiddleware = noHTTPClient + osf.ClientMiddleware = nopHTTPClient } if nil == osf.Sender {