Skip to content

Commit

Permalink
Merge pull request #85 from xmidt-org/hotfix/fix-httpmock-tests
Browse files Browse the repository at this point in the history
chore: removed the Logs checking, since that can change with each rel…
  • Loading branch information
johnabass authored Jul 16, 2024
2 parents cded175 + 1f991a4 commit c9eb3bf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 68 deletions.
17 changes: 5 additions & 12 deletions httpmock/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import "github.com/stretchr/testify/mock"
// track of how many of each test call was made.
type testingT struct {
// T is the delegate that receive all method calls
T mock.TestingT
mock.TestingT

Logs int
Errors int
Failures int
}
Expand All @@ -21,23 +20,17 @@ var _ mock.TestingT = (*testingT)(nil)

func wrapTestingT(next mock.TestingT) *testingT {
return &testingT{
T: next,
TestingT: next,
}
}

func (t *testingT) Logf(format string, args ...interface{}) {
t.Logs++
t.T.Logf("TEST LOGF: "+format, args...)
t.T.Logf("END TEST LOGF")
}

func (t *testingT) Errorf(format string, args ...interface{}) {
t.Errors++
t.T.Logf("TEST ERRORF: "+format, args...)
t.T.Logf("END TEST ERRORF")
t.Logf("TEST ERRORF: "+format, args...)
t.Logf("END TEST ERRORF")
}

func (t *testingT) FailNow() {
t.Failures++
t.T.Logf("TEST FAILNOW")
t.Logf("TEST FAILNOW")
}
13 changes: 0 additions & 13 deletions httpmock/requestChecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (suite *RequestCheckerTestSuite) TestNopRequestChecker() {
suite.Run("Assert", func() {
t := wrapTestingT(suite.T())
NopRequestChecker{}.Assert(assert.New(t), nil)
suite.Zero(t.Logs)
suite.Zero(t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -91,7 +90,6 @@ func (suite *RequestCheckerTestSuite) TestMethods() {

t := wrapTestingT(suite.T())
record.checker.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Zero(t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -129,7 +127,6 @@ func (suite *RequestCheckerTestSuite) TestMethods() {

t := wrapTestingT(suite.T())
record.checker.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand All @@ -146,7 +143,6 @@ func (suite *RequestCheckerTestSuite) TestPath() {

t := wrapTestingT(suite.T())
p.Assert(assert.New(t), &http.Request{URL: &url.URL{Path: "/test"}})
suite.Zero(t.Logs)
suite.Zero(t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -174,7 +170,6 @@ func (suite *RequestCheckerTestSuite) TestPath() {

t := wrapTestingT(suite.T())
record.checker.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -220,7 +215,6 @@ func (suite *RequestCheckerTestSuite) TestHeader() {

t := wrapTestingT(suite.T())
record.checker.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Zero(t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -282,7 +276,6 @@ func (suite *RequestCheckerTestSuite) TestHeader() {

t := wrapTestingT(suite.T())
record.checker.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -312,7 +305,6 @@ func (suite *RequestCheckerTestSuite) TestBody() {
suite.Run(strconv.Itoa(i), func() {
t := wrapTestingT(suite.T())
record.asserter.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Zero(t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -352,7 +344,6 @@ func (suite *RequestCheckerTestSuite) TestBody() {
suite.Run(strconv.Itoa(i), func() {
t := wrapTestingT(suite.T())
record.asserter.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand All @@ -376,7 +367,6 @@ func (suite *RequestCheckerTestSuite) TestBody() {
b := Body("test")
b.Assert(assert.New(t), request)

suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -404,7 +394,6 @@ func (suite *RequestCheckerTestSuite) TestBodyJSON() {
suite.Run(strconv.Itoa(i), func() {
t := wrapTestingT(suite.T())
record.asserter.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Zero(t.Errors)
suite.Zero(t.Failures)
})
Expand Down Expand Up @@ -444,7 +433,6 @@ func (suite *RequestCheckerTestSuite) TestBodyJSON() {
suite.Run(strconv.Itoa(i), func() {
t := wrapTestingT(suite.T())
record.asserter.Assert(assert.New(t), record.request)
suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand All @@ -468,7 +456,6 @@ func (suite *RequestCheckerTestSuite) TestBodyJSON() {
b := BodyJSON(`{"this": "won't matter"}`)
b.Assert(assert.New(t), request)

suite.Zero(t.Logs)
suite.Equal(1, t.Errors)
suite.Zero(t.Failures)
})
Expand Down
48 changes: 5 additions & 43 deletions httpmock/roundTripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ func (suite *RoundTripperSuite) TestSuite() {
actual, actualErr := transport.RoundTrip(new(http.Request))
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

transport.AssertExpectations()
}

func (suite *RoundTripperSuite) testSimpleReturn() {
var (
testingT = wrapTestingT(suite.T())
transport = NewRoundTripper(testingT)
transport = NewRoundTripperSuite(suite)
expected = new(http.Response)
expectedErr = errors.New("expected")
)
Expand All @@ -96,46 +94,32 @@ func (suite *RoundTripperSuite) testSimpleReturn() {
actual, actualErr := transport.RoundTrip(new(http.Request))
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
}

func (suite *RoundTripperSuite) testSimpleResponse() {
var (
testingT = wrapTestingT(suite.T())
transport = NewRoundTripper(testingT)
transport = NewRoundTripperSuite(suite)
expected = new(http.Response)
)

transport.OnAny().Response(expected).Once()
actual, actualErr := transport.RoundTrip(new(http.Request))
suite.True(expected == actual)
suite.NoError(actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
}

func (suite *RoundTripperSuite) testSimpleError() {
var (
testingT = wrapTestingT(suite.T())
transport = NewRoundTripper(testingT)
transport = NewRoundTripperSuite(suite)
expected = errors.New("expected")
)

transport.OnAny().Error(expected).Once()
response, actual := transport.RoundTrip(new(http.Request))
suite.Nil(response)
suite.Same(expected, actual)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
}

Expand All @@ -148,8 +132,7 @@ func (suite *RoundTripperSuite) TestSimple() {
func (suite *RoundTripperSuite) TestMockRequestAssertions() {
suite.Run("Pass", func() {
var (
testingT = wrapTestingT(suite.T())
transport = NewRoundTripper(testingT)
transport = NewRoundTripperSuite(suite)
request = &http.Request{
Method: "POST",
URL: &url.URL{
Expand All @@ -174,10 +157,6 @@ func (suite *RoundTripperSuite) TestMockRequestAssertions() {
actual, actualErr := transport.RoundTrip(request)
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
})

Expand All @@ -199,8 +178,6 @@ func (suite *RoundTripperSuite) TestMockRequestAssertions() {
actual, actualErr := transport.RoundTrip(request)
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Equal(1, testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand Down Expand Up @@ -239,7 +216,6 @@ func (suite *RoundTripperSuite) TestCallRequestAssertions() {
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand All @@ -266,7 +242,6 @@ func (suite *RoundTripperSuite) TestCallRequestAssertions() {
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Equal(1, testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand All @@ -289,7 +264,6 @@ func (suite *RoundTripperSuite) TestOnRequest() {
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand All @@ -311,7 +285,6 @@ func (suite *RoundTripperSuite) TestOnRequest() {
transport.RoundTrip(new(http.Request)) // different instance
})

suite.Zero(testingT.Logs)
suite.Equal(1, testingT.Errors)
suite.Equal(1, testingT.Failures)
})
Expand Down Expand Up @@ -345,7 +318,6 @@ func (suite *RoundTripperSuite) TestOnMatchAll() {
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand Down Expand Up @@ -379,7 +351,6 @@ func (suite *RoundTripperSuite) TestOnMatchAll() {
transport.RoundTrip(request)
})

suite.Zero(testingT.Logs)
suite.Equal(1, testingT.Errors)
suite.Equal(1, testingT.Failures)
})
Expand Down Expand Up @@ -407,7 +378,6 @@ func (suite *RoundTripperSuite) TestOnMatchAny() {
suite.True(expected == actual)
suite.True(expectedErr == actualErr)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand Down Expand Up @@ -435,7 +405,6 @@ func (suite *RoundTripperSuite) TestOnMatchAny() {
transport.RoundTrip(request)
})

suite.Zero(testingT.Logs)
suite.Equal(1, testingT.Errors)
suite.Equal(1, testingT.Failures)
})
Expand Down Expand Up @@ -464,7 +433,6 @@ func (suite *RoundTripperSuite) TestCustomRun() {
suite.True(expectedErr == actualErr)
suite.True(runCalled)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand Down Expand Up @@ -494,7 +462,6 @@ func (suite *RoundTripperSuite) TestCustomRun() {
suite.True(expectedErr == actualErr)
suite.True(runCalled)

suite.Zero(testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand Down Expand Up @@ -525,7 +492,6 @@ func (suite *RoundTripperSuite) TestCustomRun() {
suite.True(expectedErr == actualErr)
suite.True(runCalled)

suite.Zero(testingT.Logs)
suite.Equal(1, testingT.Errors)
suite.Zero(testingT.Failures)
transport.AssertExpectations()
Expand All @@ -535,7 +501,7 @@ func (suite *RoundTripperSuite) TestCustomRun() {
func (suite *RoundTripperSuite) TestNext() {
suite.Run("Nil", func() {
var (
testingT = wrapTestingT(suite.T())
testingT = suite.T()
rt = NewRoundTripper(testingT)
)

Expand All @@ -547,9 +513,6 @@ func (suite *RoundTripperSuite) TestNext() {
suite.assertResponse(response)

rt.AssertExpectations()
suite.Equal(1, testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
})

suite.Run("Custom", func() {
Expand All @@ -566,7 +529,6 @@ func (suite *RoundTripperSuite) TestNext() {
suite.assertResponse(response)

rt.AssertExpectations()
suite.Equal(1, testingT.Logs)
suite.Zero(testingT.Errors)
suite.Zero(testingT.Failures)
})
Expand Down

0 comments on commit c9eb3bf

Please sign in to comment.