mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: rename options.go to client_options.go
Make sure the options are next to the client.go file, which use the consumer of these options. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
136
client/client_options_test.go
Normal file
136
client/client_options_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
||||
func TestOptionWithHostFromEnv(t *testing.T) {
|
||||
c, err := New(WithHostFromEnv())
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Check(t, is.Equal(c.basePath, ""))
|
||||
if runtime.GOOS == "windows" {
|
||||
assert.Check(t, is.Equal(c.host, "npipe:////./pipe/docker_engine"))
|
||||
assert.Check(t, is.Equal(c.proto, "npipe"))
|
||||
assert.Check(t, is.Equal(c.addr, "//./pipe/docker_engine"))
|
||||
} else {
|
||||
assert.Check(t, is.Equal(c.host, "unix:///var/run/docker.sock"))
|
||||
assert.Check(t, is.Equal(c.proto, "unix"))
|
||||
assert.Check(t, is.Equal(c.addr, "/var/run/docker.sock"))
|
||||
}
|
||||
|
||||
t.Setenv("DOCKER_HOST", "tcp://foo.example.com:2376/test/")
|
||||
|
||||
c, err = New(WithHostFromEnv())
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Check(t, is.Equal(c.basePath, "/test/"))
|
||||
assert.Check(t, is.Equal(c.host, "tcp://foo.example.com:2376/test/"))
|
||||
assert.Check(t, is.Equal(c.proto, "tcp"))
|
||||
assert.Check(t, is.Equal(c.addr, "foo.example.com:2376"))
|
||||
}
|
||||
|
||||
func TestOptionWithTimeout(t *testing.T) {
|
||||
timeout := 10 * time.Second
|
||||
c, err := New(WithTimeout(timeout))
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Check(t, is.Equal(c.client.Timeout, timeout))
|
||||
}
|
||||
|
||||
func TestOptionWithVersionFromEnv(t *testing.T) {
|
||||
c, err := New(WithVersionFromEnv())
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Check(t, is.Equal(c.version, MaxAPIVersion))
|
||||
assert.Check(t, is.Equal(c.manualOverride, false))
|
||||
|
||||
t.Setenv("DOCKER_API_VERSION", "2.9999")
|
||||
|
||||
c, err = New(WithVersionFromEnv())
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Check(t, is.Equal(c.version, "2.9999"))
|
||||
assert.Check(t, is.Equal(c.manualOverride, true))
|
||||
}
|
||||
|
||||
func TestWithUserAgent(t *testing.T) {
|
||||
const userAgent = "Magic-Client/v1.2.3"
|
||||
t.Run("user-agent", func(t *testing.T) {
|
||||
c, err := New(
|
||||
WithUserAgent(userAgent),
|
||||
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent))
|
||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||
}),
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
_, err = c.Ping(t.Context(), PingOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.NilError(t, c.Close())
|
||||
})
|
||||
t.Run("user-agent and custom headers", func(t *testing.T) {
|
||||
c, err := New(
|
||||
WithUserAgent(userAgent),
|
||||
WithHTTPHeaders(map[string]string{"User-Agent": "should-be-ignored/1.0.0", "Other-Header": "hello-world"}),
|
||||
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), userAgent))
|
||||
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||
}),
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
_, err = c.Ping(t.Context(), PingOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.NilError(t, c.Close())
|
||||
})
|
||||
t.Run("custom headers", func(t *testing.T) {
|
||||
c, err := New(
|
||||
WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}),
|
||||
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), "from-custom-headers/1.0.0"))
|
||||
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||
}),
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
_, err = c.Ping(t.Context(), PingOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.NilError(t, c.Close())
|
||||
})
|
||||
t.Run("no user-agent set", func(t *testing.T) {
|
||||
c, err := New(
|
||||
WithHTTPHeaders(map[string]string{"Other-Header": "hello-world"}),
|
||||
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), ""))
|
||||
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||
}),
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
_, err = c.Ping(t.Context(), PingOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.NilError(t, c.Close())
|
||||
})
|
||||
t.Run("reset custom user-agent", func(t *testing.T) {
|
||||
c, err := New(
|
||||
WithUserAgent(""),
|
||||
WithHTTPHeaders(map[string]string{"User-Agent": "from-custom-headers/1.0.0", "Other-Header": "hello-world"}),
|
||||
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
assert.Check(t, is.Equal(req.Header.Get("User-Agent"), ""))
|
||||
assert.Check(t, is.Equal(req.Header.Get("Other-Header"), "hello-world"))
|
||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||
}),
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
_, err = c.Ping(t.Context(), PingOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.NilError(t, c.Close())
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user