@@ -2,7 +2,9 @@ package mcp
22
33import  (
44	"context" 
5+ 	"github.com/mark3labs/mcp-go/client" 
56	"github.com/mark3labs/mcp-go/mcp" 
7+ 	"net/http" 
68	"os" 
79	"path/filepath" 
810	"runtime" 
@@ -88,3 +90,81 @@ func TestDisableDestructive(t *testing.T) {
8890		})
8991	})
9092}
93+ 
94+ func  TestSseHeaders (t  * testing.T ) {
95+ 	mockServer  :=  NewMockServer ()
96+ 	defer  mockServer .Close ()
97+ 	before  :=  func (c  * mcpContext ) {
98+ 		c .withKubeConfig (mockServer .config )
99+ 		c .clientOptions  =  append (c .clientOptions , client .WithHeaders (map [string ]string {"kubernetes-authorization-bearer-token" : "a-token-from-mcp-client" }))
100+ 	}
101+ 	pathHeaders  :=  make (map [string ]http.Header , 0 )
102+ 	mockServer .Handle (http .HandlerFunc (func (w  http.ResponseWriter , req  * http.Request ) {
103+ 		pathHeaders [req .URL .Path ] =  req .Header .Clone ()
104+ 		// Request Performed by DiscoveryClient to Kube API (Get API Groups legacy -core-) 
105+ 		if  req .URL .Path  ==  "/api"  {
106+ 			w .Header ().Set ("Content-Type" , "application/json" )
107+ 			_ , _  =  w .Write ([]byte (`{"kind":"APIVersions","versions":["v1"],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0"}]}` ))
108+ 			return 
109+ 		}
110+ 		// Request Performed by DiscoveryClient to Kube API (Get API Groups) 
111+ 		if  req .URL .Path  ==  "/apis"  {
112+ 			w .Header ().Set ("Content-Type" , "application/json" )
113+ 			//w.Write([]byte(`{"kind":"APIGroupList","apiVersion":"v1","groups":[{"name":"apps","versions":[{"groupVersion":"apps/v1","version":"v1"}],"preferredVersion":{"groupVersion":"apps/v1","version":"v1"}}]}`)) 
114+ 			_ , _  =  w .Write ([]byte (`{"kind":"APIGroupList","apiVersion":"v1","groups":[]}` ))
115+ 			return 
116+ 		}
117+ 		// Request Performed by DiscoveryClient to Kube API (Get API Resources) 
118+ 		if  req .URL .Path  ==  "/api/v1"  {
119+ 			w .Header ().Set ("Content-Type" , "application/json" )
120+ 			_ , _  =  w .Write ([]byte (`{"kind":"APIResourceList","apiVersion":"v1","resources":[{"name":"pods","singularName":"","namespaced":true,"kind":"Pod","verbs":["get","list","watch","create","update","patch","delete"]}]}` ))
121+ 			return 
122+ 		}
123+ 		// Request Performed by DynamicClient 
124+ 		if  req .URL .Path  ==  "/api/v1/namespaces/default/pods"  {
125+ 			w .Header ().Set ("Content-Type" , "application/json" )
126+ 			_ , _  =  w .Write ([]byte (`{"kind":"PodList","apiVersion":"v1","items":[]}` ))
127+ 			return 
128+ 		}
129+ 		// Request Performed by kubernetes.Interface 
130+ 		if  req .URL .Path  ==  "/api/v1/namespaces/default/pods/a-pod-to-delete"  {
131+ 			w .WriteHeader (200 )
132+ 			return 
133+ 		}
134+ 		w .WriteHeader (404 )
135+ 	}))
136+ 	testCaseWithContext (t , & mcpContext {before : before }, func (c  * mcpContext ) {
137+ 		c .callTool ("pods_list" , map [string ]interface {}{})
138+ 		t .Run ("DiscoveryClient propagates headers to Kube API" , func (t  * testing.T ) {
139+ 			if  len (pathHeaders ) ==  0  {
140+ 				t .Fatalf ("No requests were made to Kube API" )
141+ 			}
142+ 			if  pathHeaders ["/api" ] ==  nil  ||  pathHeaders ["/api" ].Get ("Authorization" ) !=  "Bearer a-token-from-mcp-client"  {
143+ 				t .Fatalf ("Overridden header Authorization not found in request to /api" )
144+ 			}
145+ 			if  pathHeaders ["/apis" ] ==  nil  ||  pathHeaders ["/apis" ].Get ("Authorization" ) !=  "Bearer a-token-from-mcp-client"  {
146+ 				t .Fatalf ("Overridden header Authorization not found in request to /apis" )
147+ 			}
148+ 			if  pathHeaders ["/api/v1" ] ==  nil  ||  pathHeaders ["/api/v1" ].Get ("Authorization" ) !=  "Bearer a-token-from-mcp-client"  {
149+ 				t .Fatalf ("Overridden header Authorization not found in request to /api/v1" )
150+ 			}
151+ 		})
152+ 		t .Run ("DynamicClient propagates headers to Kube API" , func (t  * testing.T ) {
153+ 			if  len (pathHeaders ) ==  0  {
154+ 				t .Fatalf ("No requests were made to Kube API" )
155+ 			}
156+ 			if  pathHeaders ["/api/v1/namespaces/default/pods" ] ==  nil  ||  pathHeaders ["/api/v1/namespaces/default/pods" ].Get ("Authorization" ) !=  "Bearer a-token-from-mcp-client"  {
157+ 				t .Fatalf ("Overridden header Authorization not found in request to /api/v1/namespaces/default/pods" )
158+ 			}
159+ 		})
160+ 		c .callTool ("pods_delete" , map [string ]interface {}{"name" : "a-pod-to-delete" })
161+ 		t .Run ("kubernetes.Interface propagates headers to Kube API" , func (t  * testing.T ) {
162+ 			if  len (pathHeaders ) ==  0  {
163+ 				t .Fatalf ("No requests were made to Kube API" )
164+ 			}
165+ 			if  pathHeaders ["/api/v1/namespaces/default/pods/a-pod-to-delete" ] ==  nil  ||  pathHeaders ["/api/v1/namespaces/default/pods/a-pod-to-delete" ].Get ("Authorization" ) !=  "Bearer a-token-from-mcp-client"  {
166+ 				t .Fatalf ("Overridden header Authorization not found in request to /api/v1/namespaces/default/pods/a-pod-to-delete" )
167+ 			}
168+ 		})
169+ 	})
170+ }
0 commit comments