github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/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 // transportFunc allows us to inject a mock transport for testing. We define it 13 // here so we can detect the tlsconfig and return nil for only this type. 14 type transportFunc func(*http.Request) (*http.Response, error) 15 16 func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) { 17 return tf(req) 18 } 19 20 func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Client { 21 return &http.Client{ 22 Transport: transportFunc(doer), 23 } 24 } 25 26 func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) { 27 return func(req *http.Request) (*http.Response, error) { 28 header := http.Header{} 29 header.Set("Content-Type", "application/json") 30 31 body, err := json.Marshal(&types.ErrorResponse{ 32 Message: message, 33 }) 34 if err != nil { 35 return nil, err 36 } 37 38 return &http.Response{ 39 StatusCode: statusCode, 40 Body: ioutil.NopCloser(bytes.NewReader(body)), 41 Header: header, 42 }, nil 43 } 44 } 45 46 func plainTextErrorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) { 47 return func(req *http.Request) (*http.Response, error) { 48 return &http.Response{ 49 StatusCode: statusCode, 50 Body: ioutil.NopCloser(bytes.NewReader([]byte(message))), 51 }, nil 52 } 53 }