Skip to content

Commit 0b2eaa7

Browse files
committed
cli/command: don't use client.CustomHTTPHeaders(), and simplify asserts
It's the only use of this function, and it's better to check that the client actually sends the header. This also simplifies some asserts, and makes sure that "actual" and "expected" are in the correct order. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent c9f8473 commit 0b2eaa7

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

cli/command/cli_test.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"crypto/x509"
77
"fmt"
88
"io/ioutil"
9+
"net/http"
10+
"net/http/httptest"
911
"os"
1012
"runtime"
13+
"strings"
1114
"testing"
1215

1316
cliconfig "github.com/docker/cli/cli/config"
@@ -18,7 +21,6 @@ import (
1821
"github.com/docker/docker/client"
1922
"github.com/pkg/errors"
2023
"gotest.tools/v3/assert"
21-
is "gotest.tools/v3/assert/cmp"
2224
"gotest.tools/v3/env"
2325
"gotest.tools/v3/fs"
2426
)
@@ -29,42 +31,54 @@ func TestNewAPIClientFromFlags(t *testing.T) {
2931
host = "npipe://./"
3032
}
3133
opts := &flags.CommonOptions{Hosts: []string{host}}
32-
configFile := &configfile.ConfigFile{
33-
HTTPHeaders: map[string]string{
34-
"My-Header": "Custom-Value",
35-
},
36-
}
37-
apiclient, err := NewAPIClientFromFlags(opts, configFile)
34+
apiClient, err := NewAPIClientFromFlags(opts, &configfile.ConfigFile{})
3835
assert.NilError(t, err)
39-
assert.Check(t, is.Equal(host, apiclient.DaemonHost()))
40-
41-
expectedHeaders := map[string]string{
42-
"My-Header": "Custom-Value",
43-
"User-Agent": UserAgent(),
44-
}
45-
assert.Check(t, is.DeepEqual(expectedHeaders, apiclient.(*client.Client).CustomHTTPHeaders()))
46-
assert.Check(t, is.Equal(api.DefaultVersion, apiclient.ClientVersion()))
47-
assert.DeepEqual(t, configFile.HTTPHeaders, map[string]string{"My-Header": "Custom-Value"})
36+
assert.Equal(t, apiClient.DaemonHost(), host)
37+
assert.Equal(t, apiClient.ClientVersion(), api.DefaultVersion)
4838
}
4939

5040
func TestNewAPIClientFromFlagsForDefaultSchema(t *testing.T) {
5141
host := ":2375"
5242
opts := &flags.CommonOptions{Hosts: []string{host}}
43+
apiClient, err := NewAPIClientFromFlags(opts, &configfile.ConfigFile{})
44+
assert.NilError(t, err)
45+
assert.Equal(t, apiClient.DaemonHost(), "tcp://localhost"+host)
46+
assert.Equal(t, apiClient.ClientVersion(), api.DefaultVersion)
47+
}
48+
49+
func TestNewAPIClientFromFlagsWithCustomHeaders(t *testing.T) {
50+
var received map[string]string
51+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
received = map[string]string{
53+
"My-Header": r.Header.Get("My-Header"),
54+
"User-Agent": r.Header.Get("User-Agent"),
55+
}
56+
_, _ = w.Write([]byte("OK"))
57+
}))
58+
defer ts.Close()
59+
host := strings.Replace(ts.URL, "http://", "tcp://", 1)
60+
opts := &flags.CommonOptions{Hosts: []string{host}}
5361
configFile := &configfile.ConfigFile{
5462
HTTPHeaders: map[string]string{
5563
"My-Header": "Custom-Value",
5664
},
5765
}
58-
apiclient, err := NewAPIClientFromFlags(opts, configFile)
66+
67+
apiClient, err := NewAPIClientFromFlags(opts, configFile)
5968
assert.NilError(t, err)
60-
assert.Check(t, is.Equal("tcp://localhost"+host, apiclient.DaemonHost()))
69+
assert.Equal(t, apiClient.DaemonHost(), host)
70+
assert.Equal(t, apiClient.ClientVersion(), api.DefaultVersion)
71+
72+
// verify User-Agent is not appended to the configfile. see https://github.com/docker/cli/pull/2756
73+
assert.DeepEqual(t, configFile.HTTPHeaders, map[string]string{"My-Header": "Custom-Value"})
6174

6275
expectedHeaders := map[string]string{
6376
"My-Header": "Custom-Value",
6477
"User-Agent": UserAgent(),
6578
}
66-
assert.Check(t, is.DeepEqual(expectedHeaders, apiclient.(*client.Client).CustomHTTPHeaders()))
67-
assert.Check(t, is.Equal(api.DefaultVersion, apiclient.ClientVersion()))
79+
_, err = apiClient.Ping(context.Background())
80+
assert.NilError(t, err)
81+
assert.DeepEqual(t, received, expectedHeaders)
6882
}
6983

7084
func TestNewAPIClientFromFlagsWithAPIVersionFromEnv(t *testing.T) {
@@ -76,7 +90,7 @@ func TestNewAPIClientFromFlagsWithAPIVersionFromEnv(t *testing.T) {
7690
configFile := &configfile.ConfigFile{}
7791
apiclient, err := NewAPIClientFromFlags(opts, configFile)
7892
assert.NilError(t, err)
79-
assert.Check(t, is.Equal(customVersion, apiclient.ClientVersion()))
93+
assert.Equal(t, apiclient.ClientVersion(), customVersion)
8094
}
8195

8296
type fakeClient struct {
@@ -142,8 +156,8 @@ func TestInitializeFromClient(t *testing.T) {
142156

143157
cli := &DockerCli{client: apiclient}
144158
cli.initializeFromClient()
145-
assert.Check(t, is.DeepEqual(testcase.expectedServer, cli.serverInfo))
146-
assert.Check(t, is.Equal(testcase.negotiated, apiclient.negotiated))
159+
assert.DeepEqual(t, cli.serverInfo, testcase.expectedServer)
160+
assert.Equal(t, apiclient.negotiated, testcase.negotiated)
147161
})
148162
}
149163
}
@@ -186,7 +200,7 @@ func TestExperimentalCLI(t *testing.T) {
186200
err := cli.Initialize(flags.NewClientOptions())
187201
assert.NilError(t, err)
188202
// For backward-compatibility, HasExperimental will always be "true"
189-
assert.Check(t, is.Equal(true, cli.ClientInfo().HasExperimental))
203+
assert.Equal(t, cli.ClientInfo().HasExperimental, true)
190204
})
191205
}
192206
}

0 commit comments

Comments
 (0)