Skip to content

Commit 9d51409

Browse files
authored
fix: no cluster name (#242)
On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent c1d17aa commit 9d51409

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

gateway/manager/export_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package manager
22

33
import (
4+
"net/http"
5+
46
"github.com/openmfp/golang-commons/logger/testlogger"
57
appConfig "github.com/openmfp/kubernetes-graphql-gateway/common/config"
68
)
@@ -21,3 +23,11 @@ func NewManagerForTest() *Service {
2123

2224
return s
2325
}
26+
27+
func (s *Service) SetHandlerForTest(workspace string, handler http.Handler) {
28+
s.handlers.mu.Lock()
29+
defer s.handlers.mu.Unlock()
30+
s.handlers.registry[workspace] = &graphqlHandler{
31+
handler: handler,
32+
}
33+
}

gateway/manager/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6868
return
6969
}
7070

71-
s.setContexts(r, workspace, token)
71+
r = s.setContexts(r, workspace, token)
7272

7373
if r.Header.Get("Accept") == "text/event-stream" {
7474
s.handleSubscription(w, r, h.schema)

gateway/manager/handler_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package manager_test
22

33
import (
4+
"context"
45
"net/http"
56
"net/http/httptest"
7+
"strings"
68
"testing"
79

10+
"github.com/kcp-dev/logicalcluster/v3"
811
"github.com/openmfp/kubernetes-graphql-gateway/gateway/manager"
12+
"sigs.k8s.io/controller-runtime/pkg/kontext"
913
)
1014

1115
func TestServeHTTP_CORSPreflight(t *testing.T) {
@@ -41,3 +45,32 @@ func TestServeHTTP_AuthRequired_NoToken(t *testing.T) {
4145
t.Errorf("expected 401 for missing token, got %d", w.Code)
4246
}
4347
}
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

Comments
 (0)