MCPcopy
hub / github.com/moby/moby / TestPingHeadFallback

Function TestPingHeadFallback

client/ping_test.go:84–134  ·  view source on GitHub ↗

TestPingHeadFallback tests that the client falls back to GET if HEAD fails.

(t *testing.T)

Source from the content-addressed store, hash-verified

82
83// TestPingHeadFallback tests that the client falls back to GET if HEAD fails.
84func TestPingHeadFallback(t *testing.T) {
85 const expectedPath = "/_ping"
86 expMethods := []string{http.MethodHead, http.MethodGet}
87
88 tests := []struct {
89 status int
90 expected []string
91 }{
92 {
93 status: http.StatusOK,
94 expected: []string{http.MethodHead},
95 },
96 {
97 status: http.StatusInternalServerError,
98 expected: []string{http.MethodHead, http.MethodGet},
99 },
100 {
101 status: http.StatusNotFound,
102 expected: []string{http.MethodHead, http.MethodGet},
103 },
104 {
105 status: http.StatusMethodNotAllowed,
106 expected: []string{http.MethodHead, http.MethodGet},
107 },
108 }
109
110 for _, tc := range tests {
111 t.Run(http.StatusText(tc.status), func(t *testing.T) {
112 var reqs []string
113 client, err := New(WithBaseMockClient(func(req *http.Request) (*http.Response, error) {
114 if !strings.HasPrefix(req.URL.Path, expectedPath) {
115 return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedPath, req.URL.Path)
116 }
117 if !slices.Contains(expMethods, req.Method) {
118 return nil, fmt.Errorf("expected one of '%v', got '%s'", expMethods, req.Method)
119 }
120 reqs = append(reqs, req.Method)
121 resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}}
122 if req.Method == http.MethodHead {
123 resp.StatusCode = tc.status
124 }
125 resp.Header.Add("Api-Version", "1.2.3")
126 return resp, nil
127 }))
128 assert.NilError(t, err)
129 ping, _ := client.Ping(t.Context(), PingOptions{})
130 assert.Check(t, is.Equal(ping.APIVersion, "1.2.3"))
131 assert.Check(t, is.DeepEqual(reqs, tc.expected))
132 })
133 }
134}

Callers

nothing calls this directly

Calls 10

WithBaseMockClientFunction · 0.85
ErrorfMethod · 0.80
CheckMethod · 0.80
EqualMethod · 0.80
NewFunction · 0.70
RunMethod · 0.65
AddMethod · 0.65
PingMethod · 0.65
ContextMethod · 0.65
ContainsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…