Skip to content

Commit b71b92c

Browse files
authored
Merge pull request #86 from maxatome/v1
Add ZeroCallCounters()
2 parents 8cbd394 + cba99e9 commit b71b92c

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ matrix:
1616
env:
1717
- USE_LINTER=1
1818
install:
19-
- wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.18.0
19+
- wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin v1.24.0
2020
after_success:
2121
- go get github.com/mattn/goveralls
2222
- goveralls -coverprofile=coverage.out -service=travis-ci
@@ -30,7 +30,7 @@ script:
3030
- go test -race -covermode=atomic -coverprofile=coverage.out ./...
3131
- >
3232
if [ "$USE_LINTER" = 1 ]; then
33-
golangci-lint run -E gofmt -E golint -E maligned -E misspell -E prealloc -E unconvert ./...;
33+
golangci-lint run -E gofmt -E golint -E maligned -E misspell -E prealloc -E unconvert -E whitespace ./...;
3434
fi
3535
3636
notifications:

response_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ func TestRewindResponse(t *testing.T) {
218218
}
219219

220220
for _, response := range responses {
221-
222221
data, err := ioutil.ReadAll(response.Body)
223222
if err != nil {
224223
t.Fatal(err)

transport.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ func (m *MockTransport) RegisterNoResponder(responder Responder) {
558558
m.mu.Unlock()
559559
}
560560

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

573+
// ZeroCallCounters zeroes call counters without touching registered responders.
574+
func (m *MockTransport) ZeroCallCounters() {
575+
m.mu.Lock()
576+
for k := range m.callCountInfo {
577+
m.callCountInfo[k] = 0
578+
}
579+
m.totalCallCount = 0
580+
m.mu.Unlock()
581+
}
582+
572583
// GetCallCountInfo gets the info on all the calls httpmock has caught
573584
// since it was activated or reset. The info is returned as a map of
574585
// the calling keys with the number of calls made to them as their
@@ -711,12 +722,19 @@ func Deactivate() {
711722
}
712723
}
713724

714-
// Reset will remove any registered mocks and return the mock environment to it's initial state.
725+
// Reset will remove any registered mocks and return the mock
726+
// environment to it's initial state. It zeroes call counters too.
715727
func Reset() {
716728
DefaultTransport.Reset()
717729
}
718730

719-
// DeactivateAndReset is just a convenience method for calling Deactivate() and then Reset()
731+
// ZeroCallCounters zeroes call counters without touching registered responders.
732+
func ZeroCallCounters() {
733+
DefaultTransport.ZeroCallCounters()
734+
}
735+
736+
// DeactivateAndReset is just a convenience method for calling Deactivate() and then Reset().
737+
//
720738
// Happy deferring!
721739
func DeactivateAndReset() {
722740
Deactivate()

transport_test.go

+74-5
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ func TestMockTransport(t *testing.T) {
7272
}
7373

7474
// the http client wraps our NoResponderFound error, so we just try and match on text
75-
if _, err := http.Get(testURL); !strings.Contains(err.Error(),
76-
NoResponderFound.Error()) {
77-
75+
if _, err := http.Get(testURL); !strings.Contains(err.Error(), NoResponderFound.Error()) {
7876
t.Fatal(err)
7977
}
8078
}()
@@ -567,7 +565,7 @@ func TestMockTransportRespectsTimeout(t *testing.T) {
567565
}
568566
}
569567

570-
func TestMockTransportCallCount(t *testing.T) {
568+
func TestMockTransportCallCountReset(t *testing.T) {
571569
Reset()
572570
Activate()
573571
defer Deactivate()
@@ -611,7 +609,7 @@ func TestMockTransportCallCount(t *testing.T) {
611609
}
612610

613611
if !reflect.DeepEqual(info, expectedInfo) {
614-
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)
612+
t.Fatalf("did not correctly track the call count info. expected it to be \n %+v\n but it was \n %+v", expectedInfo, info)
615613
}
616614

617615
Reset()
@@ -620,6 +618,77 @@ func TestMockTransportCallCount(t *testing.T) {
620618
if afterResetTotalCallCount != 0 {
621619
t.Fatalf("did not reset the total count of calls correctly. expected it to be 0 after reset, but it was %v", afterResetTotalCallCount)
622620
}
621+
622+
info = GetCallCountInfo()
623+
if !reflect.DeepEqual(info, map[string]int{}) {
624+
t.Fatalf("did not correctly reset the call count info. expected it to be \n {}\n but it was \n %+v", info)
625+
}
626+
}
627+
628+
func TestMockTransportCallCountZero(t *testing.T) {
629+
Reset()
630+
Activate()
631+
defer Deactivate()
632+
633+
const (
634+
url = "https://github.com/path?b=1&a=2"
635+
url2 = "https://gitlab.com/"
636+
)
637+
638+
RegisterResponder("GET", url, NewStringResponder(200, "body"))
639+
RegisterResponder("POST", "=~gitlab", NewStringResponder(200, "body"))
640+
641+
_, err := http.Get(url)
642+
if err != nil {
643+
t.Fatal(err)
644+
}
645+
646+
buff := new(bytes.Buffer)
647+
json.NewEncoder(buff).Encode("{}") // nolint: errcheck
648+
_, err = http.Post(url2, "application/json", buff)
649+
if err != nil {
650+
t.Fatal(err)
651+
}
652+
653+
_, err = http.Get(url)
654+
if err != nil {
655+
t.Fatal(err)
656+
}
657+
658+
totalCallCount := GetTotalCallCount()
659+
if totalCallCount != 3 {
660+
t.Fatalf("did not track the total count of calls correctly. expected it to be 3, but it was %v", totalCallCount)
661+
}
662+
663+
info := GetCallCountInfo()
664+
expectedInfo := map[string]int{
665+
"GET " + url: 2,
666+
// Regexp match generates 2 entries:
667+
"POST " + url2: 1, // the matched call
668+
"POST =~gitlab": 1, // the regexp responder
669+
}
670+
671+
if !reflect.DeepEqual(info, expectedInfo) {
672+
t.Fatalf("did not correctly track the call count info. expected it to be \n %+v\n but it was \n %+v", expectedInfo, info)
673+
}
674+
675+
ZeroCallCounters()
676+
677+
afterResetTotalCallCount := GetTotalCallCount()
678+
if afterResetTotalCallCount != 0 {
679+
t.Fatalf("did not reset the total count of calls correctly. expected it to be 0 after reset, but it was %v", afterResetTotalCallCount)
680+
}
681+
682+
info = GetCallCountInfo()
683+
expectedInfo = map[string]int{
684+
"GET " + url: 0,
685+
// Regexp match generates 2 entries:
686+
"POST " + url2: 0, // the matched call
687+
"POST =~gitlab": 0, // the regexp responder
688+
}
689+
if !reflect.DeepEqual(info, expectedInfo) {
690+
t.Fatalf("did not correctly reset the call count info. expected it to be \n %+v\n but it was \n %+v", expectedInfo, info)
691+
}
623692
}
624693

625694
func TestRegisterResponderWithQuery(t *testing.T) {

0 commit comments

Comments
 (0)