|
1 | 1 | package manager_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "net/http"
|
5 | 6 | "net/http/httptest"
|
| 7 | + "strings" |
6 | 8 | "testing"
|
7 | 9 |
|
| 10 | + "github.com/kcp-dev/logicalcluster/v3" |
8 | 11 | "github.com/openmfp/kubernetes-graphql-gateway/gateway/manager"
|
| 12 | + "sigs.k8s.io/controller-runtime/pkg/kontext" |
9 | 13 | )
|
10 | 14 |
|
11 | 15 | func TestServeHTTP_CORSPreflight(t *testing.T) {
|
@@ -41,3 +45,32 @@ func TestServeHTTP_AuthRequired_NoToken(t *testing.T) {
|
41 | 45 | t.Errorf("expected 401 for missing token, got %d", w.Code)
|
42 | 46 | }
|
43 | 47 | }
|
| 48 | + |
| 49 | +func TestServeHTTP_CheckClusterNameInRequest(t *testing.T) { |
| 50 | + s := manager.NewManagerForTest() |
| 51 | + s.AppCfg.EnableKcp = true |
| 52 | + s.AppCfg.LocalDevelopment = true |
| 53 | + |
| 54 | + var capturedCtx context.Context |
| 55 | + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 56 | + capturedCtx = r.Context() |
| 57 | + w.WriteHeader(http.StatusOK) |
| 58 | + }) |
| 59 | + s.SetHandlerForTest("testws", testHandler) |
| 60 | + |
| 61 | + req := httptest.NewRequest(http.MethodPost, "/testws/graphql", strings.NewReader(`{}`)) |
| 62 | + req.Header.Set("Authorization", "Bearer test-token") |
| 63 | + |
| 64 | + w := httptest.NewRecorder() |
| 65 | + s.ServeHTTP(w, req) |
| 66 | + |
| 67 | + cluster, ok := kontext.ClusterFrom(capturedCtx) |
| 68 | + if !ok || cluster != logicalcluster.Name("testws") { |
| 69 | + t.Errorf("expected workspace 'testws' in context, got %v (found: %t)", cluster, ok) |
| 70 | + } |
| 71 | + |
| 72 | + token, ok := capturedCtx.Value(manager.TokenKey{}).(string) |
| 73 | + if !ok || token != "test-token" { |
| 74 | + t.Errorf("expected token 'test-token' in context, got %v (found: %t)", token, ok) |
| 75 | + } |
| 76 | +} |
0 commit comments