ping_test.go 2.6 KB

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