Skip to content

Commit 74dd17d

Browse files
committed
Make logoutput more helpful
Give and example for the mock needed
1 parent 46560d1 commit 74dd17d

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

mock.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"net/http"
77
"net/http/httptest"
8+
"strings"
89
"sync"
910
"testing"
1011

@@ -35,6 +36,7 @@ func New() *Mock {
3536
func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3637
method := r.Method
3738
path := r.URL.Path
39+
mapKey := method + " " + path
3840
var mr *mockResponse
3941
m.Lock()
4042
defer m.Unlock()
@@ -47,7 +49,7 @@ func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4749
if mr == nil {
4850
w.WriteHeader(http.StatusNotFound)
4951
fmt.Fprintf(w, "%s not found", path)
50-
m.unmockedRequests[method+path]++
52+
m.unmockedRequests[mapKey]++
5153
return
5254
}
5355

@@ -57,10 +59,10 @@ func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5759

5860
var status int
5961
if len(mr.callbacks) > 0 {
60-
status = mr.callbacks[m.callCount[method+path]](r)
62+
status = mr.callbacks[m.callCount[mapKey]](r)
6163
}
6264

63-
m.callCount[method+path]++
65+
m.callCount[mapKey]++
6466
if status != 0 {
6567
w.WriteHeader(status)
6668
}
@@ -133,34 +135,46 @@ func (m *Mock) Mock(path, resp string, callback ...func(*http.Request) int) *moc
133135

134136
func (m *Mock) AssertCallCount(tb testing.TB, method, path string, expected int) {
135137
m.Lock()
136-
cnt, ok := m.callCount[method+path]
138+
cnt, ok := m.callCount[method+" "+path]
137139
if !ok {
138140
tb.Errorf("mocked but never called path: %s method: %s", path, method)
139141
m.Unlock()
140142
return
141143
}
142-
m.assertCallCountCalled[method+path] = true
144+
m.assertCallCountCalled[method+" "+path] = true
143145
m.Unlock()
144146
assert.Equal(tb, expected, cnt, path)
145147
}
146148

147149
func (m *Mock) AssertCallCountAsserted(tb testing.TB) {
148-
for url, cnt := range m.callCount {
149-
if _, ok := m.assertCallCountCalled[url]; !ok {
150+
for request, cnt := range m.callCount {
151+
if _, ok := m.assertCallCountCalled[request]; !ok {
152+
method := strings.Split(request, " ")[0]
153+
url := strings.Split(request, " ")[1]
150154
tb.Errorf("url: %s is mocked but never asserted. It was called %d times", url, cnt)
155+
tb.Errorf(`httpMock.AssertCallCount(t, "%s", "%s", %d)`, method, url, cnt)
156+
151157
}
152158
}
153159
}
154160

155161
func (m *Mock) AssertNoMissingMocks(tb testing.TB) {
156-
for url, cnt := range m.unmockedRequests {
157-
tb.Errorf("url: %s is called but not mocked. It was called %d times", url, cnt)
162+
for request, cnt := range m.unmockedRequests {
163+
method := strings.Split(request, " ")[0]
164+
url := strings.Split(request, " ")[1]
165+
tb.Errorf("url: %s is called but not mocked. It was called %d times", request, cnt)
166+
if method == "GET" {
167+
tb.Errorf(`httpMock.Mock("%s", "response")`, url)
168+
return
169+
170+
}
171+
tb.Errorf(`httpMock.Mock("%s", "response").SetMethod("%s")`, url, method)
158172
}
159173
}
160174

161175
func (m *Mock) AssertMocksCalled(tb testing.TB) {
162176
for _, mr := range m.mockResponses {
163-
if _, ok := m.callCount[mr.method+mr.path]; !ok {
177+
if _, ok := m.callCount[mr.method+" "+mr.path]; !ok {
164178
tb.Errorf("%s %s mocked but never called.", mr.method, mr.path)
165179
}
166180
}

mock_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestMock(t *testing.T) {
1212
a := 0
1313
mock := New()
1414
mock.Mock("/test", "ok")
15-
mock.Mock("/callback", "ok", func(*http.Request) int {
15+
mock.Mock("/callback", "ok", func(http.ResponseWriter, *http.Request) int {
1616
a++
1717
return 201
1818
})
@@ -73,3 +73,31 @@ func TestNotAssertNoMissingMocks(t *testing.T) {
7373
mock.AssertNoMissingMocks(newT)
7474
assert.True(t, newT.Failed())
7575
}
76+
77+
func TestWriteResponseFromCallback(t *testing.T) {
78+
a := 0
79+
mock := New()
80+
mock.Mock("/callback", "", func(w http.ResponseWriter, r *http.Request) int {
81+
82+
w.Header().Set("X-BLAHA", "ok")
83+
w.WriteHeader(400)
84+
_, err := w.Write([]byte("inte ok"))
85+
assert.NoError(t, err)
86+
a++
87+
return 400
88+
})
89+
90+
resp, err := http.Get(mock.URL() + "/callback")
91+
assert.NoError(t, err)
92+
body, err := io.ReadAll(resp.Body)
93+
assert.NoError(t, err)
94+
assert.Equal(t, 400, resp.StatusCode)
95+
assert.Equal(t, "inte ok", string(body))
96+
assert.Equal(t, "ok", resp.Header.Get("X-BLAHA"))
97+
98+
mock.AssertCallCount(t, "GET", "/callback", 1)
99+
mock.AssertCallCountAsserted(t)
100+
mock.AssertNoMissingMocks(t)
101+
mock.AssertMocksCalled(t)
102+
assert.Equal(t, 1, a)
103+
}

0 commit comments

Comments
 (0)