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