github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/request_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "math/rand" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "gotest.tools/assert" 15 is "gotest.tools/assert/cmp" 16 ) 17 18 // TestSetHostHeader should set fake host for local communications, set real host 19 // for normal communications. 20 func TestSetHostHeader(t *testing.T) { 21 testURL := "/test" 22 testCases := []struct { 23 host string 24 expectedHost string 25 expectedURLHost string 26 }{ 27 { 28 "unix:///var/run/docker.sock", 29 "docker", 30 "/var/run/docker.sock", 31 }, 32 { 33 "npipe:////./pipe/docker_engine", 34 "docker", 35 "//./pipe/docker_engine", 36 }, 37 { 38 "tcp://0.0.0.0:4243", 39 "", 40 "0.0.0.0:4243", 41 }, 42 { 43 "tcp://localhost:4243", 44 "", 45 "localhost:4243", 46 }, 47 } 48 49 for c, test := range testCases { 50 hostURL, err := ParseHostURL(test.host) 51 assert.NilError(t, err) 52 53 client := &Client{ 54 client: newMockClient(func(req *http.Request) (*http.Response, error) { 55 if !strings.HasPrefix(req.URL.Path, testURL) { 56 return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL) 57 } 58 if req.Host != test.expectedHost { 59 return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host) 60 } 61 if req.URL.Host != test.expectedURLHost { 62 return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host) 63 } 64 return &http.Response{ 65 StatusCode: http.StatusOK, 66 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 67 }, nil 68 }), 69 70 proto: hostURL.Scheme, 71 addr: hostURL.Host, 72 basePath: hostURL.Path, 73 } 74 75 _, err = client.sendRequest(context.Background(), "GET", testURL, nil, nil, nil) 76 assert.NilError(t, err) 77 } 78 } 79 80 // TestPlainTextError tests the server returning an error in plain text for 81 // backwards compatibility with API versions <1.24. All other tests use 82 // errors returned as JSON 83 func TestPlainTextError(t *testing.T) { 84 client := &Client{ 85 client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")), 86 } 87 _, err := client.ContainerList(context.Background(), types.ContainerListOptions{}) 88 if err == nil || err.Error() != "Error response from daemon: Server error" { 89 t.Fatalf("expected a Server Error, got %v", err) 90 } 91 } 92 93 func TestInfiniteError(t *testing.T) { 94 infinitR := rand.New(rand.NewSource(42)) 95 client := &Client{ 96 client: newMockClient(func(req *http.Request) (*http.Response, error) { 97 resp := &http.Response{StatusCode: http.StatusInternalServerError} 98 resp.Header = http.Header{} 99 resp.Body = ioutil.NopCloser(infinitR) 100 return resp, nil 101 }), 102 } 103 104 _, err := client.Ping(context.Background()) 105 assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error")) 106 }