github.com/jingleWang/moby@v1.13.1/client/client_mock_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 9 "github.com/docker/docker/api/types" 10 ) 11 12 func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Client { 13 return &http.Client{ 14 Transport: transportFunc(doer), 15 } 16 } 17 18 func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) { 19 return func(req *http.Request) (*http.Response, error) { 20 header := http.Header{} 21 header.Set("Content-Type", "application/json") 22 23 body, err := json.Marshal(&types.ErrorResponse{ 24 Message: message, 25 }) 26 if err != nil { 27 return nil, err 28 } 29 30 return &http.Response{ 31 StatusCode: statusCode, 32 Body: ioutil.NopCloser(bytes.NewReader(body)), 33 Header: header, 34 }, nil 35 } 36 } 37 38 func plainTextErrorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) { 39 return func(req *http.Request) (*http.Response, error) { 40 return &http.Response{ 41 StatusCode: statusCode, 42 Body: ioutil.NopCloser(bytes.NewReader([]byte(message))), 43 }, nil 44 } 45 }