Skip to content

Commit 46560d1

Browse files
authored
Merge pull request #3 from FortnoxAB/bugfix/mock-validation-threadsafety
Make sure unmockedRequests are written in a threadsafe manner
2 parents a2bfa16 + fbd2746 commit 46560d1

File tree

3 files changed

+3
-12
lines changed

3 files changed

+3
-12
lines changed

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
21
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
32
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
43
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
65
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
76
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
87
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
9-
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
108
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
119
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
1210
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1311
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1412
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1613
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1714
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1815
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

mock.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,30 @@ func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3737
path := r.URL.Path
3838
var mr *mockResponse
3939
m.Lock()
40+
defer m.Unlock()
4041
for _, v := range m.mockResponses {
4142
if v.path == path && v.method == method && v.checkFilter(r) {
4243
mr = v
4344
break
4445
}
4546
}
46-
m.Unlock()
4747
if mr == nil {
4848
w.WriteHeader(http.StatusNotFound)
4949
fmt.Fprintf(w, "%s not found", path)
5050
m.unmockedRequests[method+path]++
5151
return
5252
}
5353

54-
mr.Lock()
5554
for k, v := range mr.headers {
5655
w.Header().Set(k, v)
5756
}
58-
mr.Unlock()
5957

6058
var status int
61-
m.Lock()
6259
if len(mr.callbacks) > 0 {
6360
status = mr.callbacks[m.callCount[method+path]](r)
6461
}
65-
m.Unlock()
6662

67-
m.Lock()
6863
m.callCount[method+path]++
69-
m.Unlock()
7064
if status != 0 {
7165
w.WriteHeader(status)
7266
}

mock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gohtmock
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"testing"
77

@@ -24,7 +24,7 @@ func TestMock(t *testing.T) {
2424
resp, err = http.Get(mock.URL() + "/test")
2525
assert.NoError(t, err)
2626

27-
body, err := ioutil.ReadAll(resp.Body)
27+
body, err := io.ReadAll(resp.Body)
2828
assert.NoError(t, err)
2929
assert.Equal(t, "ok", string(body))
3030
mock.AssertCallCount(t, "GET", "/test", 1)

0 commit comments

Comments
 (0)