|
1 | 1 | package kiali |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "net/http" |
6 | | - "net/http/httptest" |
7 | | - "net/url" |
8 | | - "testing" |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "testing" |
9 | 9 |
|
10 | | - "github.com/containers/kubernetes-mcp-server/pkg/config" |
11 | | - internalk8s "github.com/containers/kubernetes-mcp-server/pkg/kubernetes" |
| 10 | + "github.com/containers/kubernetes-mcp-server/pkg/config" |
12 | 11 | ) |
13 | 12 |
|
14 | 13 | func TestValidateAndGetURL_JoinsProperly(t *testing.T) { |
15 | | - m := NewManager(&config.StaticConfig{KialiURL: "https://kiali.example/"}) |
16 | | - k := m.GetKiali() |
| 14 | + m := NewManager(&config.StaticConfig{KialiOptions: config.KialiOptions{Url: "https://kiali.example/"}}) |
| 15 | + k := m.GetKiali() |
17 | 16 |
|
18 | | - full, err := k.validateAndGetURL("/api/path") |
19 | | - if err != nil { |
20 | | - t.Fatalf("unexpected error: %v", err) |
21 | | - } |
22 | | - if full != "https://kiali.example/api/path" { |
23 | | - t.Fatalf("unexpected url: %s", full) |
24 | | - } |
| 17 | + full, err := k.validateAndGetURL("/api/path") |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("unexpected error: %v", err) |
| 20 | + } |
| 21 | + if full != "https://kiali.example/api/path" { |
| 22 | + t.Fatalf("unexpected url: %s", full) |
| 23 | + } |
25 | 24 |
|
26 | | - m.KialiURL = "https://kiali.example" |
27 | | - full, err = k.validateAndGetURL("api/path") |
28 | | - if err != nil { |
29 | | - t.Fatalf("unexpected error: %v", err) |
30 | | - } |
31 | | - if full != "https://kiali.example/api/path" { |
32 | | - t.Fatalf("unexpected url: %s", full) |
33 | | - } |
| 25 | + m.KialiURL = "https://kiali.example" |
| 26 | + full, err = k.validateAndGetURL("api/path") |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("unexpected error: %v", err) |
| 29 | + } |
| 30 | + if full != "https://kiali.example/api/path" { |
| 31 | + t.Fatalf("unexpected url: %s", full) |
| 32 | + } |
34 | 33 |
|
35 | | - // preserve query |
36 | | - m.KialiURL = "https://kiali.example" |
37 | | - full, err = k.validateAndGetURL("/api/path?x=1&y=2") |
38 | | - if err != nil { |
39 | | - t.Fatalf("unexpected error: %v", err) |
40 | | - } |
41 | | - u, _ := url.Parse(full) |
42 | | - if u.Path != "/api/path" || u.Query().Get("x") != "1" || u.Query().Get("y") != "2" { |
43 | | - t.Fatalf("unexpected parsed url: %s", full) |
44 | | - } |
| 34 | + // preserve query |
| 35 | + m.KialiURL = "https://kiali.example" |
| 36 | + full, err = k.validateAndGetURL("/api/path?x=1&y=2") |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("unexpected error: %v", err) |
| 39 | + } |
| 40 | + u, _ := url.Parse(full) |
| 41 | + if u.Path != "/api/path" || u.Query().Get("x") != "1" || u.Query().Get("y") != "2" { |
| 42 | + t.Fatalf("unexpected parsed url: %s", full) |
| 43 | + } |
45 | 44 | } |
46 | 45 |
|
47 | | -func TestCurrentAuthorizationHeader_FromContext(t *testing.T) { |
48 | | - m := NewManager(&config.StaticConfig{KialiURL: "https://kiali.example"}) |
49 | | - k := m.GetKiali() |
50 | | - ctx := context.WithValue(context.Background(), internalk8s.OAuthAuthorizationHeader, "bearer abc") |
51 | | - got := k.CurrentAuthorizationHeader(ctx) |
52 | | - if got != "Bearer abc" { |
53 | | - t.Fatalf("expected normalized bearer header, got '%s'", got) |
54 | | - } |
55 | | -} |
56 | | - |
57 | | -func TestCurrentAuthorizationHeader_FromManagerToken(t *testing.T) { |
58 | | - m := NewManager(&config.StaticConfig{KialiURL: "https://kiali.example"}) |
59 | | - m.BearerToken = "abc" |
60 | | - k := m.GetKiali() |
61 | | - got := k.CurrentAuthorizationHeader(context.Background()) |
62 | | - if got != "Bearer abc" { |
63 | | - t.Fatalf("expected 'Bearer abc', got '%s'", got) |
64 | | - } |
65 | | -} |
| 46 | +// CurrentAuthorizationHeader behavior is now implicit via executeRequest using Manager.BearerToken |
66 | 47 |
|
67 | 48 | func TestExecuteRequest_SetsAuthAndCallsServer(t *testing.T) { |
68 | | - // setup test server to assert path and auth header |
69 | | - var seenAuth string |
70 | | - var seenPath string |
71 | | - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
72 | | - seenAuth = r.Header.Get("Authorization") |
73 | | - seenPath = r.URL.String() |
74 | | - _, _ = w.Write([]byte("ok")) |
75 | | - })) |
76 | | - defer srv.Close() |
| 49 | + // setup test server to assert path and auth header |
| 50 | + var seenAuth string |
| 51 | + var seenPath string |
| 52 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 53 | + seenAuth = r.Header.Get("Authorization") |
| 54 | + seenPath = r.URL.String() |
| 55 | + _, _ = w.Write([]byte("ok")) |
| 56 | + })) |
| 57 | + defer srv.Close() |
77 | 58 |
|
78 | | - m := NewManager(&config.StaticConfig{KialiURL: srv.URL}) |
79 | | - k := m.GetKiali() |
80 | | - ctx := context.WithValue(context.Background(), internalk8s.OAuthAuthorizationHeader, "Bearer token-xyz") |
81 | | - |
82 | | - out, err := k.executeRequest(ctx, "/api/ping?q=1") |
83 | | - if err != nil { |
84 | | - t.Fatalf("unexpected error: %v", err) |
85 | | - } |
86 | | - if out != "ok" { |
87 | | - t.Fatalf("unexpected body: %s", out) |
88 | | - } |
89 | | - if seenAuth != "Bearer token-xyz" { |
90 | | - t.Fatalf("expected auth header to be set, got '%s'", seenAuth) |
91 | | - } |
92 | | - if seenPath != "/api/ping?q=1" { |
93 | | - t.Fatalf("unexpected path: %s", seenPath) |
94 | | - } |
| 59 | + m := NewManager(&config.StaticConfig{KialiOptions: config.KialiOptions{Url: srv.URL}}) |
| 60 | + m.BearerToken = "token-xyz" |
| 61 | + k := m.GetKiali() |
| 62 | + out, err := k.executeRequest(context.Background(), "/api/ping?q=1") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("unexpected error: %v", err) |
| 65 | + } |
| 66 | + if out != "ok" { |
| 67 | + t.Fatalf("unexpected body: %s", out) |
| 68 | + } |
| 69 | + if seenAuth != "Bearer token-xyz" { |
| 70 | + t.Fatalf("expected auth header to be set, got '%s'", seenAuth) |
| 71 | + } |
| 72 | + if seenPath != "/api/ping?q=1" { |
| 73 | + t.Fatalf("unexpected path: %s", seenPath) |
| 74 | + } |
95 | 75 | } |
96 | | - |
97 | | - |
0 commit comments