github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/client/options_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "net/http" 6 "runtime" 7 "testing" 8 "time" 9 10 "github.com/Prakhar-Agarwal-byte/moby/api" 11 "gotest.tools/v3/assert" 12 is "gotest.tools/v3/assert/cmp" 13 ) 14 15 func TestOptionWithHostFromEnv(t *testing.T) { 16 c, err := NewClientWithOpts(WithHostFromEnv()) 17 assert.NilError(t, err) 18 assert.Check(t, c.client != nil) 19 assert.Check(t, is.Equal(c.basePath, "")) 20 if runtime.GOOS == "windows" { 21 assert.Check(t, is.Equal(c.host, "npipe:////./pipe/docker_engine")) 22 assert.Check(t, is.Equal(c.proto, "npipe")) 23 assert.Check(t, is.Equal(c.addr, "//./pipe/docker_engine")) 24 } else { 25 assert.Check(t, is.Equal(c.host, "unix:///var/run/docker.sock")) 26 assert.Check(t, is.Equal(c.proto, "unix")) 27 assert.Check(t, is.Equal(c.addr, "/var/run/docker.sock")) 28 } 29 30 t.Setenv("DOCKER_HOST", "tcp://foo.example.com:2376/test/") 31 32 c, err = NewClientWithOpts(WithHostFromEnv()) 33 assert.NilError(t, err) 34 assert.Check(t, c.client != nil) 35 assert.Check(t, is.Equal(c.basePath, "/test/")) 36 assert.Check(t, is.Equal(c.host, "tcp://foo.example.com:2376/test/")) 37 assert.Check(t, is.Equal(c.proto, "tcp")) 38 assert.Check(t, is.Equal(c.addr, "foo.example.com:2376")) 39 } 40 41 func TestOptionWithTimeout(t *testing.T) { 42 timeout := 10 * time.Second 43 c, err := NewClientWithOpts(WithTimeout(timeout)) 44 assert.NilError(t, err) 45 assert.Check(t, c.client != nil) 46 assert.Equal(t, c.client.Timeout, timeout) 47 } 48 49 func TestOptionWithVersionFromEnv(t *testing.T) { 50 c, err := NewClientWithOpts(WithVersionFromEnv()) 51 assert.NilError(t, err) 52 assert.Check(t, c.client != nil) 53 assert.Equal(t, c.version, api.DefaultVersion) 54 assert.Equal(t, c.manualOverride, false) 55 56 t.Setenv("DOCKER_API_VERSION", "2.9999") 57 58 c, err = NewClientWithOpts(WithVersionFromEnv()) 59 assert.NilError(t, err) 60 assert.Check(t, c.client != nil) 61 assert.Equal(t, c.version, "2.9999") 62 assert.Equal(t, c.manualOverride, true) 63 } 64 65 func TestWithUserAgent(t *testing.T) { 66 const userAgent = "Magic-Client/v1.2.3" 67 t.Run("user-agent", func(t *testing.T) { 68 c, err := NewClientWithOpts( 69 WithUserAgent(userAgent), 70 WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) { 71 assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent)) 72 return &http.Response{StatusCode: http.StatusOK}, nil 73 })), 74 ) 75 assert.Check(t, err) 76 _, err = c.Ping(context.Background()) 77 assert.Check(t, err) 78 assert.Check(t, c.Close()) 79 }) 80 t.Run("user-agent and custom headers", func(t *testing.T) { 81 c, err := NewClientWithOpts( 82 WithUserAgent(userAgent), 83 WithHTTPHeaders(map[string]string{"User-Agent": "should-be-ignored/1.0.0", "Other-Header": "hello-world"}), 84 WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) { 85 assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent)) 86 assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world")) 87 return &http.Response{StatusCode: http.StatusOK}, nil 88 })), 89 ) 90 assert.Check(t, err) 91 _, err = c.Ping(context.Background()) 92 assert.Check(t, err) 93 assert.Check(t, c.Close()) 94 }) 95 t.Run("custom headers", func(t *testing.T) { 96 c, err := NewClientWithOpts( 97 WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}), 98 WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) { 99 assert.Check(t, is.Equal(req.Header.Get("User-Agent"), "from-custom-headers/1.0.0")) 100 assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world")) 101 return &http.Response{StatusCode: http.StatusOK}, nil 102 })), 103 ) 104 assert.Check(t, err) 105 _, err = c.Ping(context.Background()) 106 assert.Check(t, err) 107 assert.Check(t, c.Close()) 108 }) 109 t.Run("no user-agent set", func(t *testing.T) { 110 c, err := NewClientWithOpts( 111 WithHTTPHeaders(map[string]string{"Other-Header": "hello-world"}), 112 WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) { 113 assert.Check(t, is.Equal(req.Header.Get("User-Agent"), "")) 114 assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world")) 115 return &http.Response{StatusCode: http.StatusOK}, nil 116 })), 117 ) 118 assert.Check(t, err) 119 _, err = c.Ping(context.Background()) 120 assert.Check(t, err) 121 assert.Check(t, c.Close()) 122 }) 123 t.Run("reset custom user-agent", func(t *testing.T) { 124 c, err := NewClientWithOpts( 125 WithUserAgent(""), 126 WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}), 127 WithHTTPClient(newMockClient(func(req *http.Request) (*http.Response, error) { 128 assert.Check(t, is.Equal(req.Header.Get("User-Agent"), "")) 129 assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world")) 130 return &http.Response{StatusCode: http.StatusOK}, nil 131 })), 132 ) 133 assert.Check(t, err) 134 _, err = c.Ping(context.Background()) 135 assert.Check(t, err) 136 assert.Check(t, c.Close()) 137 }) 138 }