github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/ping_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "context" 5 "errors" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 "testing" 10 11 "gotest.tools/assert" 12 is "gotest.tools/assert/cmp" 13 ) 14 15 // TestPingFail tests that when a server sends a non-successful response that we 16 // can still grab API details, when set. 17 // Some of this is just exercising the code paths to make sure there are no 18 // panics. 19 func TestPingFail(t *testing.T) { 20 var withHeader bool 21 client := &Client{ 22 client: newMockClient(func(req *http.Request) (*http.Response, error) { 23 resp := &http.Response{StatusCode: http.StatusInternalServerError} 24 if withHeader { 25 resp.Header = http.Header{} 26 resp.Header.Set("API-Version", "awesome") 27 resp.Header.Set("Docker-Experimental", "true") 28 } 29 resp.Body = ioutil.NopCloser(strings.NewReader("some error with the server")) 30 return resp, nil 31 }), 32 } 33 34 ping, err := client.Ping(context.Background()) 35 assert.Check(t, is.ErrorContains(err, "")) 36 assert.Check(t, is.Equal(false, ping.Experimental)) 37 assert.Check(t, is.Equal("", ping.APIVersion)) 38 39 withHeader = true 40 ping2, err := client.Ping(context.Background()) 41 assert.Check(t, is.ErrorContains(err, "")) 42 assert.Check(t, is.Equal(true, ping2.Experimental)) 43 assert.Check(t, is.Equal("awesome", ping2.APIVersion)) 44 } 45 46 // TestPingWithError tests the case where there is a protocol error in the ping. 47 // This test is mostly just testing that there are no panics in this code path. 48 func TestPingWithError(t *testing.T) { 49 client := &Client{ 50 client: newMockClient(func(req *http.Request) (*http.Response, error) { 51 resp := &http.Response{StatusCode: http.StatusInternalServerError} 52 resp.Header = http.Header{} 53 resp.Header.Set("API-Version", "awesome") 54 resp.Header.Set("Docker-Experimental", "true") 55 resp.Body = ioutil.NopCloser(strings.NewReader("some error with the server")) 56 return resp, errors.New("some error") 57 }), 58 } 59 60 ping, err := client.Ping(context.Background()) 61 assert.Check(t, is.ErrorContains(err, "")) 62 assert.Check(t, is.Equal(false, ping.Experimental)) 63 assert.Check(t, is.Equal("", ping.APIVersion)) 64 } 65 66 // TestPingSuccess tests that we are able to get the expected API headers/ping 67 // details on success. 68 func TestPingSuccess(t *testing.T) { 69 client := &Client{ 70 client: newMockClient(func(req *http.Request) (*http.Response, error) { 71 resp := &http.Response{StatusCode: http.StatusInternalServerError} 72 resp.Header = http.Header{} 73 resp.Header.Set("API-Version", "awesome") 74 resp.Header.Set("Docker-Experimental", "true") 75 resp.Body = ioutil.NopCloser(strings.NewReader("some error with the server")) 76 return resp, nil 77 }), 78 } 79 ping, err := client.Ping(context.Background()) 80 assert.Check(t, is.ErrorContains(err, "")) 81 assert.Check(t, is.Equal(true, ping.Experimental)) 82 assert.Check(t, is.Equal("awesome", ping.APIVersion)) 83 }