MCPcopy Index your code
hub / github.com/docker/docker-agent / httpDo

Function httpDo

pkg/server/server_test.go:124–167  ·  view source on GitHub ↗
(t *testing.T, ctx context.Context, method, socketPath, path string, payload any)

Source from the content-addressed store, hash-verified

122}
123
124func httpDo(t *testing.T, ctx context.Context, method, socketPath, path string, payload any) []byte {
125 t.Helper()
126
127 var (
128 body io.Reader
129 contentType string
130 )
131 switch v := payload.(type) {
132 case nil:
133 body = http.NoBody
134 case []byte:
135 body = bytes.NewReader(v)
136 case string:
137 body = strings.NewReader(v)
138 default:
139 buf, err := json.Marshal(payload)
140 require.NoError(t, err)
141 body = bytes.NewReader(buf)
142 contentType = "application/json"
143 }
144
145 req, err := http.NewRequestWithContext(ctx, method, "http://_"+path, body)
146 require.NoError(t, err)
147
148 req.Header.Set("Content-Type", contentType)
149
150 client := &http.Client{
151 Transport: &http.Transport{
152 DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
153 var d net.Dialer
154 return d.DialContext(ctx, "unix", strings.TrimPrefix(socketPath, "unix://"))
155 },
156 },
157 }
158 resp, err := client.Do(req)
159 require.NoError(t, err)
160 defer resp.Body.Close()
161
162 buf, err := io.ReadAll(resp.Body)
163 require.NoError(t, err)
164
165 require.Less(t, resp.StatusCode, 400, string(buf))
166 return buf
167}
168
169func unmarshal(t *testing.T, buf []byte, v any) {
170 t.Helper()

Callers 3

httpGETFunction · 0.85
TestServer_ForkSessionFunction · 0.85

Calls 3

DoMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected