Skip to content

Commit 6e1e1cd

Browse files
authored
Merge pull request #6 from FortnoxAB/bugfix/prettier-logging
Make logoutput more helpful
2 parents 89128f7 + 93bad6e commit 6e1e1cd

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/fortnoxab/gohtmock
22

3-
go 1.24
3+
go 1.21
44

55
require github.com/stretchr/testify v1.8.0
66

mock.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"slices"
9+
"strings"
910
"sync"
1011
"testing"
1112

@@ -32,6 +33,7 @@ func New() *Mock {
3233
func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3334
method := r.Method
3435
path := r.URL.Path
36+
mapKey := method + " " + path
3537
var mr *mockResponse
3638
m.Lock()
3739
defer m.Unlock()
@@ -65,7 +67,7 @@ func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6567
if mr == nil {
6668
w.WriteHeader(http.StatusNotFound)
6769
fmt.Fprintf(w, "%s not found", path)
68-
m.unmockedRequests[method+path]++
70+
m.unmockedRequests[mapKey]++
6971
return
7072
}
7173

@@ -283,6 +285,7 @@ func (m *Mock) AssertCallCountAsserted(tb testing.TB) {
283285
for _, mr := range m.mockResponses {
284286
if !mr.asserted {
285287
tb.Errorf("url: %s is mocked but never asserted. It was called %d times", mr.path, mr.callCount)
288+
tb.Errorf(`create a mock with: .AssertCallCount(t, "%s", "%s", %d)`, mr.method, mr.path, mr.callCount)
286289
}
287290
}
288291
}
@@ -293,8 +296,17 @@ func (m *Mock) AssertNoMissingMocks(tb testing.TB) {
293296
m.Lock()
294297
defer m.Unlock()
295298

296-
for url, cnt := range m.unmockedRequests {
297-
tb.Errorf("url: %s is called but not mocked. It was called %d times", url, cnt)
299+
for request, cnt := range m.unmockedRequests {
300+
method := strings.Split(request, " ")[0]
301+
url := strings.Split(request, " ")[1]
302+
tb.Errorf("url: %s is called but not mocked. It was called %d times", request, cnt)
303+
if method == "GET" {
304+
tb.Errorf(`create a mock with: .Mock("%s", "response")`, url)
305+
continue
306+
307+
}
308+
tb.Errorf(`create a mock with: .Mock("%s", "response").SetMethod("%s")`, url, method)
309+
298310
}
299311
}
300312

mock_test.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func TestMockResponderWithFilters(t *testing.T) {
9696
return r.URL.Query().Get("id") == "2"
9797
})
9898

99-
assertGetResults(t, mock.URL()+"/test?id=1", "user 1", http.StatusOK)
100-
assertGetResults(t, mock.URL()+"/test?id=3", "user not found", http.StatusNotFound)
101-
assertGetResults(t, mock.URL()+"/test?id=2", "user 2", http.StatusOK)
99+
assertBodyAndStatus(t, mock.URL()+"/test?id=1", "user 1", http.StatusOK)
100+
assertBodyAndStatus(t, mock.URL()+"/test?id=3", "user not found", http.StatusNotFound)
101+
assertBodyAndStatus(t, mock.URL()+"/test?id=2", "user 2", http.StatusOK)
102102
mock.AssertCallCount(t, http.MethodGet, "/test", 3)
103103
mock.AssertCallCountAsserted(t)
104104
}
@@ -127,9 +127,9 @@ func TestMockResponderWithFiltersAsserts(t *testing.T) {
127127
w.Write([]byte("user not found"))
128128
})
129129

130-
assertGetResults(t, mock.URL()+"/test?id=1", "user 1", http.StatusOK)
131-
assertGetResults(t, mock.URL()+"/test?id=3", "user not found", http.StatusNotFound)
132-
assertGetResults(t, mock.URL()+"/test?id=2", "user 2", http.StatusOK)
130+
assertBodyAndStatus(t, mock.URL()+"/test?id=1", "user 1", http.StatusOK)
131+
assertBodyAndStatus(t, mock.URL()+"/test?id=3", "user not found", http.StatusNotFound)
132+
assertBodyAndStatus(t, mock.URL()+"/test?id=2", "user 2", http.StatusOK)
133133
mr1.AssertCallCount(t, 1)
134134
mr2.AssertCallCount(t, 1)
135135
mr3.AssertCallCount(t, 1)
@@ -143,9 +143,9 @@ func TestAllMocksLimits(t *testing.T) {
143143
mock.Mock("/test", "once").Once()
144144
mock.Mock("/test", "times").Times(2)
145145

146-
assertGetResults(t, mock.URL()+"/test", "once", http.StatusOK)
147-
assertGetResults(t, mock.URL()+"/test", "times", http.StatusOK)
148-
assertGetResults(t, mock.URL()+"/test", "times", http.StatusOK)
146+
assertBodyAndStatus(t, mock.URL()+"/test", "once", http.StatusOK)
147+
assertBodyAndStatus(t, mock.URL()+"/test", "times", http.StatusOK)
148+
assertBodyAndStatus(t, mock.URL()+"/test", "times", http.StatusOK)
149149
mock.AssertCallCount(t, "GET", "/test", 3)
150150
mock.AssertCallCountAsserted(t)
151151
mock.AssertNoMissingMocks(t)
@@ -192,18 +192,14 @@ func TestNotAssertNoMissingMocks(t *testing.T) {
192192
assert.True(t, newT.Failed())
193193
}
194194

195-
func assertGetResults(t *testing.T, path, expBody string, expStatus int) bool {
195+
func assertBodyAndStatus(t *testing.T, path, expBody string, expStatus int) bool {
196196
resp, err := http.Get(path)
197197
assert.NoError(t, err)
198-
if !assert.Equal(t, expStatus, resp.StatusCode) {
199-
return assert.Fail(t, "Expected status %s but got %s", expStatus, resp.StatusCode)
200-
}
198+
assert.Equal(t, expStatus, resp.StatusCode, "Expected status %d but got %d", expStatus, resp.StatusCode)
201199

202200
body, err := io.ReadAll(resp.Body)
203201
assert.NoError(t, err)
204-
if !assert.Equal(t, expBody, string(body)) {
205-
return assert.Fail(t, "Expected body %s but got %s", expBody, string(body))
206-
}
202+
assert.Equal(t, expBody, string(body), "Expected body %s but got %s", expBody, string(body))
207203

208204
return true
209205
}

0 commit comments

Comments
 (0)