client_mock_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "github.com/docker/docker/api/types"
  8. )
  9. // transportFunc allows us to inject a mock transport for testing. We define it
  10. // here so we can detect the tlsconfig and return nil for only this type.
  11. type transportFunc func(*http.Request) (*http.Response, error)
  12. func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
  13. return tf(req)
  14. }
  15. func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Client {
  16. return &http.Client{
  17. Transport: transportFunc(doer),
  18. }
  19. }
  20. func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
  21. return func(req *http.Request) (*http.Response, error) {
  22. header := http.Header{}
  23. header.Set("Content-Type", "application/json")
  24. body, err := json.Marshal(&types.ErrorResponse{
  25. Message: message,
  26. })
  27. if err != nil {
  28. return nil, err
  29. }
  30. return &http.Response{
  31. StatusCode: statusCode,
  32. Body: ioutil.NopCloser(bytes.NewReader(body)),
  33. Header: header,
  34. }, nil
  35. }
  36. }
  37. func plainTextErrorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
  38. return func(req *http.Request) (*http.Response, error) {
  39. return &http.Response{
  40. StatusCode: statusCode,
  41. Body: ioutil.NopCloser(bytes.NewReader([]byte(message))),
  42. }, nil
  43. }
  44. }