MCPcopy
hub / github.com/docker/docker-agent / TestCreateHTTPClient_PersistsCookies

Function TestCreateHTTPClient_PersistsCookies

pkg/tools/mcp/remote_test.go:442–484  ·  view source on GitHub ↗

TestCreateHTTPClient_PersistsCookies verifies that the *http.Client returned by createHTTPClient has a cookie jar, so sticky-session cookies set by the remote MCP ingress are echoed back on subsequent requests.

(t *testing.T)

Source from the content-addressed store, hash-verified

440// by createHTTPClient has a cookie jar, so sticky-session cookies set by the
441// remote MCP ingress are echoed back on subsequent requests.
442func TestCreateHTTPClient_PersistsCookies(t *testing.T) {
443 t.Parallel()
444
445 var requestCount atomic.Int32
446
447 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
448 n := requestCount.Add(1)
449 switch n {
450 case 1:
451 if _, err := r.Cookie("mcp_session"); err == nil {
452 t.Errorf("first request must not carry mcp_session cookie, got one")
453 }
454 w.Header().Set("Set-Cookie", "mcp_session=abc123; Path=/")
455 w.WriteHeader(http.StatusOK)
456 default:
457 cookie := r.Header.Get("Cookie")
458 if !strings.Contains(cookie, "mcp_session=abc123") {
459 t.Errorf("subsequent request must carry mcp_session=abc123, got Cookie=%q", cookie)
460 }
461 w.WriteHeader(http.StatusOK)
462 }
463 }))
464 defer server.Close()
465
466 client := newRemoteClient(server.URL, "streamable", nil, NewInMemoryTokenStore(), nil, false)
467 httpClient, _, err := client.createHTTPClient()
468 require.NoError(t, err)
469 require.NotNil(t, httpClient.Jar, "createHTTPClient must attach a cookie jar so sticky sessions stick")
470
471 req1, err := http.NewRequestWithContext(t.Context(), http.MethodGet, server.URL, http.NoBody)
472 require.NoError(t, err)
473 resp1, err := httpClient.Do(req1)
474 require.NoError(t, err)
475 _ = resp1.Body.Close()
476
477 req2, err := http.NewRequestWithContext(t.Context(), http.MethodGet, server.URL, http.NoBody)
478 require.NoError(t, err)
479 resp2, err := httpClient.Do(req2)
480 require.NoError(t, err)
481 _ = resp2.Body.Close()
482
483 require.Equal(t, int32(2), requestCount.Load(), "handler should have served both requests")
484}
485
486func TestNewRemoteToolsetWithAllowPrivateIPsPropagatesToClient(t *testing.T) {
487 t.Parallel()

Callers

nothing calls this directly

Calls 10

newRemoteClientFunction · 0.85
NewInMemoryTokenStoreFunction · 0.85
createHTTPClientMethod · 0.80
ContextMethod · 0.80
LoadMethod · 0.80
AddMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65
DoMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected