From 7d8ed8317890d474ebf57af66efecb748a8dea34 Mon Sep 17 00:00:00 2001 From: Stein Date: Fri, 30 Aug 2024 21:05:15 +0100 Subject: [PATCH] Fix vet errors copying mutex (#156) --- mocks.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/mocks.go b/mocks.go index bf97bbd..717c04e 100644 --- a/mocks.go +++ b/mocks.go @@ -249,8 +249,7 @@ func (m *Mock) copy() *Mock { req := *m.request newMock.request = &req - res := *m.response - newMock.response = &res + newMock.response = m.response.deepCopy() return &newMock } @@ -296,6 +295,31 @@ type MockResponse struct { mu sync.RWMutex // Add a mutex for thread-safe access } +func (r *MockResponse) deepCopy() *MockResponse { + newResponse := &MockResponse{ + timeout: r.timeout, + headers: make(map[string][]string), + cookies: make([]*Cookie, len(r.cookies)), + body: r.body, + statusCode: r.statusCode, + fixedDelayMillis: r.fixedDelayMillis, + mu: sync.RWMutex{}, + } + + for k, v := range r.headers { + newHeader := make([]string, len(v)) + copy(newHeader, v) + newResponse.headers[k] = newHeader + } + + for i, cookie := range r.cookies { + newCookie := *cookie + newResponse.cookies[i] = &newCookie + } + + return newResponse +} + // StandaloneMocks for using mocks outside of API tests context type StandaloneMocks struct { mocks []*Mock