mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
client: mockResponse: prevent sharing body reader
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user