Skip to content

Commit

Permalink
Merge pull request #86 from maxatome/v1
Browse files Browse the repository at this point in the history
Add ZeroCallCounters()
  • Loading branch information
maxatome committed Mar 17, 2020
2 parents 8cbd394 + cba99e9 commit b71b92c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ matrix:
env:
- USE_LINTER=1
install:
- wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.18.0
- wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.24.0
after_success:
- go get github.com/mattn/goveralls
- goveralls -coverprofile=coverage.out -service=travis-ci
Expand All @@ -30,7 +30,7 @@ script:
- go test -race -covermode=atomic -coverprofile=coverage.out ./...
- >
if [ "$USE_LINTER" = 1 ]; then
golangci-lint run -E gofmt -E golint -E maligned -E misspell -E prealloc -E unconvert ./...;
golangci-lint run -E gofmt -E golint -E maligned -E misspell -E prealloc -E unconvert -E whitespace ./...;
fi
notifications:
Expand Down
1 change: 0 additions & 1 deletion response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ func TestRewindResponse(t *testing.T) {
}

for _, response := range responses {

data, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
Expand Down
24 changes: 21 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ func (m *MockTransport) RegisterNoResponder(responder Responder) {
m.mu.Unlock()
}

// Reset removes all registered responders (including the no responder) from the MockTransport
// Reset removes all registered responders (including the no
// responder) from the MockTransport. It zeroes call counters too.
func (m *MockTransport) Reset() {
m.mu.Lock()
m.responders = make(map[routeKey]Responder)
Expand All @@ -569,6 +570,16 @@ func (m *MockTransport) Reset() {
m.mu.Unlock()
}

// ZeroCallCounters zeroes call counters without touching registered responders.
func (m *MockTransport) ZeroCallCounters() {
m.mu.Lock()
for k := range m.callCountInfo {
m.callCountInfo[k] = 0
}
m.totalCallCount = 0
m.mu.Unlock()
}

// GetCallCountInfo gets the info on all the calls httpmock has caught
// since it was activated or reset. The info is returned as a map of
// the calling keys with the number of calls made to them as their
Expand Down Expand Up @@ -711,12 +722,19 @@ func Deactivate() {
}
}

// Reset will remove any registered mocks and return the mock environment to it's initial state.
// Reset will remove any registered mocks and return the mock
// environment to it's initial state. It zeroes call counters too.
func Reset() {
DefaultTransport.Reset()
}

// DeactivateAndReset is just a convenience method for calling Deactivate() and then Reset()
// ZeroCallCounters zeroes call counters without touching registered responders.
func ZeroCallCounters() {
DefaultTransport.ZeroCallCounters()
}

// DeactivateAndReset is just a convenience method for calling Deactivate() and then Reset().
//
// Happy deferring!
func DeactivateAndReset() {
Deactivate()
Expand Down
79 changes: 74 additions & 5 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ func TestMockTransport(t *testing.T) {
}

// the http client wraps our NoResponderFound error, so we just try and match on text
if _, err := http.Get(testURL); !strings.Contains(err.Error(),
NoResponderFound.Error()) {

if _, err := http.Get(testURL); !strings.Contains(err.Error(), NoResponderFound.Error()) {
t.Fatal(err)
}
}()
Expand Down Expand Up @@ -567,7 +565,7 @@ func TestMockTransportRespectsTimeout(t *testing.T) {
}
}

func TestMockTransportCallCount(t *testing.T) {
func TestMockTransportCallCountReset(t *testing.T) {
Reset()
Activate()
defer Deactivate()
Expand Down Expand Up @@ -611,7 +609,7 @@ func TestMockTransportCallCount(t *testing.T) {
}

if !reflect.DeepEqual(info, expectedInfo) {
t.Fatalf("did not correctly track the call count info. expected it to be \n %+v \n but it was \n %+v \n", expectedInfo, info)
t.Fatalf("did not correctly track the call count info. expected it to be \n %+v\n but it was \n %+v", expectedInfo, info)
}

Reset()
Expand All @@ -620,6 +618,77 @@ func TestMockTransportCallCount(t *testing.T) {
if afterResetTotalCallCount != 0 {
t.Fatalf("did not reset the total count of calls correctly. expected it to be 0 after reset, but it was %v", afterResetTotalCallCount)
}

info = GetCallCountInfo()
if !reflect.DeepEqual(info, map[string]int{}) {
t.Fatalf("did not correctly reset the call count info. expected it to be \n {}\n but it was \n %+v", info)
}
}

func TestMockTransportCallCountZero(t *testing.T) {
Reset()
Activate()
defer Deactivate()

const (
url = "https://github.com/path?b=1&a=2"
url2 = "https://gitlab.com/"
)

RegisterResponder("GET", url, NewStringResponder(200, "body"))
RegisterResponder("POST", "=~gitlab", NewStringResponder(200, "body"))

_, err := http.Get(url)
if err != nil {
t.Fatal(err)
}

buff := new(bytes.Buffer)
json.NewEncoder(buff).Encode("{}") // nolint: errcheck
_, err = http.Post(url2, "application/json", buff)
if err != nil {
t.Fatal(err)
}

_, err = http.Get(url)
if err != nil {
t.Fatal(err)
}

totalCallCount := GetTotalCallCount()
if totalCallCount != 3 {
t.Fatalf("did not track the total count of calls correctly. expected it to be 3, but it was %v", totalCallCount)
}

info := GetCallCountInfo()
expectedInfo := map[string]int{
"GET " + url: 2,
// Regexp match generates 2 entries:
"POST " + url2: 1, // the matched call
"POST =~gitlab": 1, // the regexp responder
}

if !reflect.DeepEqual(info, expectedInfo) {
t.Fatalf("did not correctly track the call count info. expected it to be \n %+v\n but it was \n %+v", expectedInfo, info)
}

ZeroCallCounters()

afterResetTotalCallCount := GetTotalCallCount()
if afterResetTotalCallCount != 0 {
t.Fatalf("did not reset the total count of calls correctly. expected it to be 0 after reset, but it was %v", afterResetTotalCallCount)
}

info = GetCallCountInfo()
expectedInfo = map[string]int{
"GET " + url: 0,
// Regexp match generates 2 entries:
"POST " + url2: 0, // the matched call
"POST =~gitlab": 0, // the regexp responder
}
if !reflect.DeepEqual(info, expectedInfo) {
t.Fatalf("did not correctly reset the call count info. expected it to be \n %+v\n but it was \n %+v", expectedInfo, info)
}
}

func TestRegisterResponderWithQuery(t *testing.T) {
Expand Down

0 comments on commit b71b92c

Please sign in to comment.