Merge pull request #44382 from thaJeztah/client_rewrite
client: defaultHTTPClient() accept URL
This commit is contained in:
commit
751888979c
5 changed files with 35 additions and 21 deletions
|
@ -126,7 +126,12 @@ func CheckRedirect(req *http.Request, via []*http.Request) error {
|
|||
// client.WithAPIVersionNegotiation(),
|
||||
// )
|
||||
func NewClientWithOpts(ops ...Opt) (*Client, error) {
|
||||
client, err := defaultHTTPClient(DefaultDockerHost)
|
||||
hostURL, err := ParseHostURL(DefaultDockerHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := defaultHTTPClient(hostURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -134,8 +139,8 @@ func NewClientWithOpts(ops ...Opt) (*Client, error) {
|
|||
host: DefaultDockerHost,
|
||||
version: api.DefaultVersion,
|
||||
client: client,
|
||||
proto: defaultProto,
|
||||
addr: defaultAddr,
|
||||
proto: hostURL.Scheme,
|
||||
addr: hostURL.Host,
|
||||
}
|
||||
|
||||
for _, op := range ops {
|
||||
|
@ -161,13 +166,12 @@ func NewClientWithOpts(ops ...Opt) (*Client, error) {
|
|||
return c, nil
|
||||
}
|
||||
|
||||
func defaultHTTPClient(host string) (*http.Client, error) {
|
||||
hostURL, err := ParseHostURL(host)
|
||||
func defaultHTTPClient(hostURL *url.URL) (*http.Client, error) {
|
||||
transport := &http.Transport{}
|
||||
err := sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := &http.Transport{}
|
||||
_ = sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host)
|
||||
return &http.Client{
|
||||
Transport: transport,
|
||||
CheckRedirect: CheckRedirect,
|
||||
|
|
|
@ -165,6 +165,14 @@ func TestParseHostURL(t *testing.T) {
|
|||
host: "tcp://localhost:2476/path",
|
||||
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"},
|
||||
},
|
||||
{
|
||||
host: "unix:///var/run/docker.sock",
|
||||
expected: &url.URL{Scheme: "unix", Host: "/var/run/docker.sock"},
|
||||
},
|
||||
{
|
||||
host: "npipe:////./pipe/docker_engine",
|
||||
expected: &url.URL{Scheme: "npipe", Host: "//./pipe/docker_engine"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
|
|
|
@ -6,6 +6,3 @@ package client // import "github.com/docker/docker/client"
|
|||
// DefaultDockerHost defines OS-specific default host if the DOCKER_HOST
|
||||
// (EnvOverrideHost) environment variable is unset or empty.
|
||||
const DefaultDockerHost = "unix:///var/run/docker.sock"
|
||||
|
||||
const defaultProto = "unix"
|
||||
const defaultAddr = "/var/run/docker.sock"
|
||||
|
|
|
@ -3,6 +3,3 @@ package client // import "github.com/docker/docker/client"
|
|||
// DefaultDockerHost defines OS-specific default host if the DOCKER_HOST
|
||||
// (EnvOverrideHost) environment variable is unset or empty.
|
||||
const DefaultDockerHost = "npipe:////./pipe/docker_engine"
|
||||
|
||||
const defaultProto = "npipe"
|
||||
const defaultAddr = "//./pipe/docker_engine"
|
||||
|
|
|
@ -1,31 +1,39 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
||||
func TestOptionWithHostFromEnv(t *testing.T) {
|
||||
c, err := NewClientWithOpts(WithHostFromEnv())
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Equal(t, c.host, DefaultDockerHost)
|
||||
assert.Equal(t, c.proto, defaultProto)
|
||||
assert.Equal(t, c.addr, defaultAddr)
|
||||
assert.Equal(t, c.basePath, "")
|
||||
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 = NewClientWithOpts(WithHostFromEnv())
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, c.client != nil)
|
||||
assert.Equal(t, c.host, "tcp://foo.example.com:2376/test/")
|
||||
assert.Equal(t, c.proto, "tcp")
|
||||
assert.Equal(t, c.addr, "foo.example.com:2376")
|
||||
assert.Equal(t, c.basePath, "/test/")
|
||||
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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue