Skip to content

Commit

Permalink
Merge pull request #3 from FortnoxAB/bugfix/mock-validation-threadsafety
Browse files Browse the repository at this point in the history
Make sure unmockedRequests are written in a threadsafe manner
  • Loading branch information
jonaz authored Jan 30, 2025
2 parents a2bfa16 + fbd2746 commit 46560d1
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 12 deletions.
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 1 addition & 7 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,30 @@ func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
var mr *mockResponse
m.Lock()
defer m.Unlock()
for _, v := range m.mockResponses {
if v.path == path && v.method == method && v.checkFilter(r) {
mr = v
break
}
}
m.Unlock()
if mr == nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s not found", path)
m.unmockedRequests[method+path]++
return
}

mr.Lock()
for k, v := range mr.headers {
w.Header().Set(k, v)
}
mr.Unlock()

var status int
m.Lock()
if len(mr.callbacks) > 0 {
status = mr.callbacks[m.callCount[method+path]](r)
}
m.Unlock()

m.Lock()
m.callCount[method+path]++
m.Unlock()
if status != 0 {
w.WriteHeader(status)
}
Expand Down
4 changes: 2 additions & 2 deletions mock_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gohtmock

import (
"io/ioutil"
"io"
"net/http"
"testing"

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

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "ok", string(body))
mock.AssertCallCount(t, "GET", "/test", 1)
Expand Down

0 comments on commit 46560d1

Please sign in to comment.