MCPcopy Index your code
hub / github.com/docker/cli / TestNewAPIClientFromFlagsWithCustomHeaders

Function TestNewAPIClientFromFlagsWithCustomHeaders

cli/command/cli_test.go:52–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

50}
51
52func TestNewAPIClientFromFlagsWithCustomHeaders(t *testing.T) {
53 var received map[string]string
54 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
55 received = map[string]string{
56 "My-Header": r.Header.Get("My-Header"),
57 "User-Agent": r.Header.Get("User-Agent"),
58 }
59 _, _ = w.Write([]byte("OK"))
60 }))
61 defer ts.Close()
62 host := strings.Replace(ts.URL, "http://", "tcp://", 1)
63 opts := &flags.ClientOptions{Hosts: []string{host}}
64 configFile := &configfile.ConfigFile{
65 HTTPHeaders: map[string]string{
66 "My-Header": "Custom-Value",
67 },
68 }
69
70 apiClient, err := NewAPIClientFromFlags(opts, configFile)
71 assert.NilError(t, err)
72 assert.Equal(t, apiClient.DaemonHost(), host)
73 assert.Equal(t, apiClient.ClientVersion(), client.MaxAPIVersion)
74
75 // verify User-Agent is not appended to the configfile. see https://github.com/docker/cli/pull/2756
76 assert.DeepEqual(t, configFile.HTTPHeaders, map[string]string{"My-Header": "Custom-Value"})
77
78 expectedHeaders := map[string]string{
79 "My-Header": "Custom-Value",
80 "User-Agent": UserAgent(),
81 }
82 _, err = apiClient.Ping(context.TODO(), client.PingOptions{})
83 assert.NilError(t, err)
84 assert.DeepEqual(t, received, expectedHeaders)
85}
86
87func TestNewAPIClientFromFlagsWithCustomHeadersFromEnv(t *testing.T) {
88 var received http.Header

Callers

nothing calls this directly

Calls 7

NewAPIClientFromFlagsFunction · 0.85
UserAgentFunction · 0.85
GetMethod · 0.65
WriteMethod · 0.45
CloseMethod · 0.45
ClientVersionMethod · 0.45
PingMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…