client: mockResponse: prevent sharing body reader

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-13 12:32:57 +01:00
parent 45c9f460b8
commit acb5c5a390

View File

@@ -85,20 +85,17 @@ func mockJSONResponse[T any](statusCode int, headers http.Header, resp T) func(r
}
func mockResponse(statusCode int, headers http.Header, respBody string) func(req *http.Request) (*http.Response, error) {
if headers == nil {
headers = make(http.Header)
}
var body io.ReadCloser
if respBody == "" {
body = http.NoBody
} else {
body = io.NopCloser(strings.NewReader(respBody))
}
return func(req *http.Request) (*http.Response, error) {
var body io.ReadCloser
if respBody == "" || req.Method == http.MethodHead {
body = http.NoBody
} else {
body = io.NopCloser(strings.NewReader(respBody))
}
return &http.Response{
Status: fmt.Sprintf("%d %s", statusCode, http.StatusText(statusCode)),
StatusCode: statusCode,
Header: headers,
Header: headers.Clone(),
Body: body,
Request: req,
}, nil