2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-08-24 06:10:50 -04:00
|
|
|
"io"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
)
|
|
|
|
|
2017-08-21 17:51:45 -04:00
|
|
|
// transportFunc allows us to inject a mock transport for testing. We define it
|
|
|
|
// here so we can detect the tlsconfig and return nil for only this type.
|
|
|
|
type transportFunc func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return tf(req)
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:44:25 -04:00
|
|
|
func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transportFunc(doer),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
|
|
|
|
return func(req *http.Request) (*http.Response, error) {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
body, err := json.Marshal(&types.ErrorResponse{
|
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: statusCode,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader(body)),
|
2016-09-06 14:46:37 -04:00
|
|
|
Header: header,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func plainTextErrorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
|
|
|
|
return func(req *http.Request) (*http.Response, error) {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: statusCode,
|
2021-08-24 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(message))),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|