Skip to content

Commit

Permalink
Updated unit tests per PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
renaz6 committed May 25, 2022
1 parent 5c65198 commit 25d9d78
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 46 deletions.
7 changes: 4 additions & 3 deletions httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
67 changes: 37 additions & 30 deletions httpClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
},
}

Expand All @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"net/http"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion outboundSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 25d9d78

Please sign in to comment.