Skip to content

Commit 2572d94

Browse files
committed
feat: reset all func
1 parent 768719b commit 2572d94

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
99
github.com/julienschmidt/httprouter v1.3.0 // indirect
1010
github.com/pmezard/go-difflib v1.0.0 // indirect
11-
github.com/slzhffktm/httprouter v0.0.2 // indirect
11+
github.com/slzhffktm/httprouter v0.0.3 // indirect
1212
gopkg.in/yaml.v3 v3.0.1 // indirect
1313
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
77
github.com/slzhffktm/httprouter v0.0.2 h1:alUI45TxyQkJpLOL0EmYmO+9KzH8L5JV5yyq/nbxFEA=
88
github.com/slzhffktm/httprouter v0.0.2/go.mod h1:WbWqTm2WQoxF5XQm7wLrLgGEWCSBrvLv0x3CF3THxnk=
9+
github.com/slzhffktm/httprouter v0.0.3 h1:GJPKfirTnAqe+L/yzFW/fMRvlykbqhbRMjFwzs0qRbM=
10+
github.com/slzhffktm/httprouter v0.0.3/go.mod h1:WbWqTm2WQoxF5XQm7wLrLgGEWCSBrvLv0x3CF3THxnk=
911
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1012
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
1113
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

httptest.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ func (s *Server) Close() error {
6565

6666
// GetNCalls returns the number of calls for a path.
6767
func (s *Server) GetNCalls(method, path string) int {
68-
return s.calls[method][path]
68+
calls, ok := s.calls[method][path]
69+
if !ok {
70+
return 0
71+
}
72+
73+
return calls
6974
}
7075

7176
// ResetNCalls resets the number of calls for all paths.
@@ -92,3 +97,9 @@ func (s *Server) RegisterHandler(method string, path string, handler ServerHandl
9297
})
9398
})
9499
}
100+
101+
// ResetAll resets all the calls and handlers.
102+
func (s *Server) ResetAll() {
103+
s.router.ClearHandlers()
104+
s.calls = map[string]map[string]int{}
105+
}

httptest_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,32 @@ func (s *serverTestSuite) TestSimulateTimeout() {
267267

268268
s.Equal(1, server.GetNCalls(http.MethodGet, path))
269269
}
270+
271+
func (s *serverTestSuite) TestResetAll() {
272+
server, err := httptest.NewServer(address, httptest.ServerConfig{})
273+
s.NoError(err)
274+
defer server.Close()
275+
276+
path := "/some-path/subpath"
277+
expectedResBody := []byte(`{"res":"ponse"}`)
278+
279+
server.RegisterHandler(http.MethodGet, path, func(w httptest.ResponseWriter, r *httptest.Request) {
280+
w.SetStatusCode(http.StatusOK)
281+
w.SetBodyBytes(expectedResBody)
282+
})
283+
284+
server.ResetAll()
285+
286+
res, _, err := s.httpClient.Do(
287+
ctx,
288+
http.MethodGet,
289+
path,
290+
nil,
291+
nil,
292+
nil,
293+
)
294+
s.NoError(err)
295+
s.Equal(http.StatusNotFound, res.StatusCode)
296+
297+
s.Equal(0, server.GetNCalls(http.MethodGet, path))
298+
}

0 commit comments

Comments
 (0)